NeoGraph 0.10.0
A C++17 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
openai_provider.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <neograph/api.h>
11#include <neograph/provider.h>
12#include <asio/executor_work_guard.hpp>
13#include <asio/io_context.hpp>
14#include <memory>
15#include <optional>
16#include <string>
17#include <thread>
18
19namespace neograph::async { class ConnPool; }
20
21namespace neograph::llm {
22
38class NEOGRAPH_API OpenAIProvider : public Provider {
39 public:
41 struct Config {
42 std::string api_key;
43 std::string base_url = "https://api.openai.com";
44 std::string default_model = "gpt-4o-mini";
55 int timeout_seconds = 60;
56 };
57
63 static std::unique_ptr<OpenAIProvider> create(const Config& config);
64
73 static std::shared_ptr<Provider> create_shared(const Config& config);
74
78
85 asio::awaitable<ChatCompletion>
86 complete_async(const CompletionParams& params) override;
87
90
92 const StreamCallback& on_chunk) override;
93
101 asio::awaitable<ChatCompletion>
102 invoke(const CompletionParams& params, StreamCallback on_chunk) override;
103
104 std::string get_name() const override { return "openai"; }
105
106 private:
107 explicit OpenAIProvider(Config config);
108 json build_body(const CompletionParams& params) const;
109 Config config_;
110
111 // Long-lived HTTP loop + ConnPool. Same shape as SchemaProvider —
112 // see commit 6da4810 / feedback_schema_provider_no_pool. ConnPool
113 // can't live inside Provider::complete()'s run_sync io_context
114 // (one-shot), so the provider owns its own io_context + worker.
115 std::unique_ptr<asio::io_context> http_io_;
116 std::optional<asio::executor_work_guard<asio::io_context::executor_type>> http_work_;
117 std::thread http_thread_;
118 std::unique_ptr<async::ConnPool> conn_pool_;
119};
120
121} // namespace neograph::llm
NEOGRAPH_API export/import macro for shared-library builds.
Abstract base class for LLM providers.
Definition provider.h:127
LLM provider for OpenAI-compatible APIs.
asio::awaitable< ChatCompletion > invoke(const CompletionParams &params, StreamCallback on_chunk) override
v1.0 single-dispatch override (Candidate 6 PR6).
static std::unique_ptr< OpenAIProvider > create(const Config &config)
Create an OpenAI provider instance.
asio::awaitable< ChatCompletion > complete_async(const CompletionParams &params) override
Async completion — dispatches over the owned ConnPool so successive calls reuse a kept-alive TCP+TLS ...
ChatCompletion complete_stream(const CompletionParams &params, const StreamCallback &on_chunk) override
Sync completion is inherited from Provider::complete(), which drives complete_async via neograph::asy...
~OpenAIProvider()
Destructor — shuts down the long-lived HTTP loop + worker thread held alongside the ConnPool.
std::string get_name() const override
Get the provider name (e.g., "openai", "claude").
static std::shared_ptr< Provider > create_shared(const Config &config)
Same as create but returns a shared_ptr<Provider>.
Abstract LLM provider interface.
std::function< void(const std::string &chunk)> StreamCallback
Callback invoked per token during streaming completion.
Definition provider.h:52
LLM completion response including the message and token usage.
Definition types.h:60
Parameters for an LLM completion request.
Definition provider.h:57
Configuration for OpenAI-compatible API connections.
std::string api_key
API key for authentication.