Agents 0.0.2
Edge AI Agents SDK
Loading...
Searching...
No Matches
openai_llm.h
1
10#pragma once
11
12#include <agents-cpp/llm_interface.h>
13
14namespace agents {
15namespace llms {
16
20class OpenAILLM : public LLMInterface {
21public:
27 OpenAILLM(const String& api_key = "", const String& model = "gpt-4o-2024-05-13");
31 ~OpenAILLM() override = default;
32
37 std::vector<String> getAvailableModels() override;
38
43 void setModel(const String& model) override;
44
49 String getModel() const override;
50
55 void setApiKey(const String& api_key) override;
56
61 void setApiBase(const String& api_base) override;
62
67 void setOptions(const LLMOptions& options) override;
68
73 LLMOptions getOptions() const override;
74
80 LLMResponse complete(const String& prompt) override;
81
87 LLMResponse chat(const std::vector<Message>& messages) override;
88
96 const std::vector<Message>& messages,
97 const std::vector<std::shared_ptr<Tool>>& tools
98 ) override;
99
106 const std::vector<Message>& messages,
107 std::function<void(const String&, bool)> callback
108 ) override;
109
110private:
111 String api_key_;
112 String api_base_ = "https://api.openai.com/v1";
113 String model_;
114 LLMOptions options_;
115
121 JsonObject messagesToOpenAIFormat(const std::vector<Message>& messages);
122
128 JsonObject toolsToOpenAIFormat(const std::vector<std::shared_ptr<Tool>>& tools);
129
135 LLMResponse parseOpenAIResponse(const JsonObject& response);
136
144 JsonObject makeApiCall(const JsonObject& request_body, bool stream = false, const String& endpoint = "chat/completions");
145};
146
147} // namespace llms
148} // namespace agents
Interface for language model providers (OpenAI, Anthropic, Google, Ollama)
Definition llm_interface.h:68
LLMOptions getOptions() const override
Get current options.
OpenAILLM(const String &api_key="", const String &model="gpt-4o-2024-05-13")
Constructor.
void setOptions(const LLMOptions &options) override
Set options for API calls.
LLMResponse chat(const std::vector< Message > &messages) override
Generate completion from a list of messages.
void setApiBase(const String &api_base) override
Set API base URL (for self-hosted or proxied endpoints)
void streamChat(const std::vector< Message > &messages, std::function< void(const String &, bool)> callback) override
Stream results with callback.
void setModel(const String &model) override
Set the model to use.
LLMResponse chatWithTools(const std::vector< Message > &messages, const std::vector< std::shared_ptr< Tool > > &tools) override
Generate completion with available tools.
String getModel() const override
Get current model.
~OpenAILLM() override=default
Destructor.
LLMResponse complete(const String &prompt) override
Generate completion from a prompt.
void setApiKey(const String &api_key) override
Set API key.
std::vector< String > getAvailableModels() override
Get available models from OpenAI.
Large Language Models Namespace.
Definition anthropic_llm.h:19
Tools Namespace.
Definition file_tool.h:15
Framework Namespace.
Definition agent.h:18
nlohmann::json JsonObject
JSON object type.
Definition types.h:39
std::string String
String type.
Definition types.h:27
Options for LLM API calls.
Definition llm_interface.h:25
Response from an LLM.
Definition types.h:85