71 std::string default_model =
"gpt-4o-mini";
72 int timeout_seconds = 60;
83 bool use_websocket =
false;
95 bool prefer_libcurl =
false;
120 asio::awaitable<ChatCompletion>
160 asio::awaitable<ChatCompletion>
171 asio::awaitable<ChatCompletion>
182 enum class SystemPromptStrategy { IN_MESSAGES, TOP_LEVEL, TOP_LEVEL_PARTS };
184 enum class ToolCallStrategy { TOOL_CALLS_ARRAY, CONTENT_ARRAY, PARTS_ARRAY, FLAT_ITEMS };
186 enum class ToolResultStrategy { FLAT, CONTENT_ARRAY, PARTS_ARRAY, FLAT_ITEM };
188 enum class ToolDefWrapper { FUNCTION, NONE, FUNCTION_DECLARATIONS, FLAT_FUNCTION };
190 enum class ResponseStrategy { CHOICES_MESSAGE, CONTENT_ARRAY, CANDIDATES_PARTS, OUTPUT_ARRAY };
191 enum class StreamFormat { SSE_DATA, SSE_EVENTS };
194 struct ConnectionConfig {
195 std::string base_url;
196 std::string endpoint;
197 std::string stream_endpoint;
198 std::string auth_header;
199 std::string auth_prefix;
200 std::string api_key_env;
201 std::string auth_query_param;
202 std::map<std::string, std::string> extra_headers;
205 struct RequestConfig {
206 std::string model_field;
207 std::string messages_field;
208 std::string tools_field;
209 std::string temperature_path;
210 std::string max_tokens_path;
211 bool max_tokens_required =
false;
212 int max_tokens_default = -1;
213 std::string stream_field;
224 std::set<std::string> per_call_fields;
227 struct SystemPromptConfig {
228 SystemPromptStrategy strategy;
230 std::string role_name;
231 std::string parts_field;
232 std::string text_field;
235 struct MessagesConfig {
236 std::string role_field;
237 std::string content_field;
238 std::map<std::string, std::string> role_map;
239 bool content_is_parts =
false;
240 json text_part_template;
243 struct ToolDefConfig {
244 ToolDefWrapper wrapper;
245 std::string name_field;
246 std::string description_field;
247 std::string parameters_field;
250 struct ToolCallConfig {
251 ToolCallStrategy strategy;
254 json text_item_template;
257 struct ToolResultConfig {
259 ToolResultStrategy strategy;
260 std::string id_field;
261 std::string content_field;
266 std::string strategy;
268 json text_part_template;
271 struct ResponseConfig {
272 ResponseStrategy strategy;
273 std::string message_path;
274 std::string content_field;
275 std::string role_field;
276 std::string tool_calls_field;
277 std::string tool_call_id_field;
278 std::string tool_call_name_path;
279 std::string tool_call_args_path;
280 bool tool_call_args_is_string =
true;
281 std::string content_path;
282 std::string text_type;
283 std::string text_field;
284 std::string tool_use_type;
285 std::string tool_call_name_field;
286 std::string tool_call_args_field;
287 std::string parts_path;
288 std::string function_call_field;
290 std::string output_path;
291 std::string message_item_type;
292 std::string function_call_item_type;
293 std::string message_content_field;
294 std::string function_call_id_field;
295 std::string usage_path;
296 std::string prompt_tokens_field;
297 std::string completion_tokens_field;
298 std::string total_tokens_field;
301 struct StreamConfig {
304 std::string done_signal;
305 std::string delta_path;
306 std::string content_field;
307 std::string tool_calls_field;
308 std::string tool_call_index_field;
309 std::string tool_call_id_field;
310 std::string tool_call_name_path;
311 std::string tool_call_args_path;
312 std::string delta_strategy;
313 std::string delta_parts_path;
314 std::string delta_text_field;
315 std::string delta_function_call_field;
316 std::string delta_tool_call_name_field;
317 std::string delta_tool_call_args_field;
328 mutable std::mutex schema_mutex_;
339 std::unique_ptr<asio::io_context> http_io_;
340 std::optional<asio::executor_work_guard<asio::io_context::executor_type>> http_work_;
341 std::thread http_thread_;
342 std::unique_ptr<async::ConnPool> conn_pool_;
360 std::unique_ptr<asio::io_context> bridge_io_;
361 std::optional<asio::executor_work_guard<asio::io_context::executor_type>> bridge_work_;
362 std::thread bridge_thread_;
367 std::unique_ptr<async::CurlH2Pool> curl_pool_;
372 std::string provider_name_;
373 ConnectionConfig conn_;
375 SystemPromptConfig sys_;
376 MessagesConfig msgs_;
377 ToolDefConfig tool_def_;
378 ToolCallConfig tool_call_;
379 ToolResultConfig tool_result_;
381 ResponseConfig resp_;
382 StreamConfig stream_;
387 json build_body(
const CompletionParams& params)
const;
388 json serialize_messages(
const std::vector<ChatMessage>& messages)
const;
389 json serialize_tools(
const std::vector<ChatTool>& tools)
const;
390 json serialize_single_message(
const ChatMessage& msg)
const;
395 asio::awaitable<ChatCompletion>
396 complete_stream_ws_responses(
const CompletionParams& params,
397 const StreamCallback& on_chunk);
399 ChatMessage parse_response(
const json& resp_json)
const;
400 ChatCompletion::Usage parse_usage(
const json& resp_json)
const;
402 std::string build_endpoint(
const std::string& model,
bool streaming)
const;
403 std::map<std::string, std::string> build_headers()
const;
404 std::string get_api_key()
const;
406 static std::pair<std::string, std::string> parse_data_url(
const std::string& url);
407 static json substitute(
const json& tmpl,
const std::map<std::string, json>& vars);
408 static std::string generate_tool_call_id();