|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
OpenAI-compatible driver for the AI unified interface. More...
#include "csilk/drivers/ai.h"#include <curl/curl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "cJSON.h"
Data Structures | |
| struct | openai_state_t |
| Per-instance state for the OpenAI-compatible driver. More... | |
| struct | curl_response |
| Accumulates a complete HTTP response body in memory. More... | |
| struct | curl_context |
| Per-request context passed to the streaming write callback. Holds both the request (for on_chunk callback) and response (to accumulate final content), plus a line-buffer for SSE frame reassembly. More... | |
Functions | |
| static void * | openai_init (const char *api_key, const char *base_url) |
| Initialize the OpenAI driver state. | |
| static void | openai_free (void *state_ptr) |
| Free the OpenAI driver state and associated resources. | |
| static size_t | write_cb_simple (void *contents, size_t size, size_t nmemb, void *userp) |
| libcurl write callback for simple (non-streaming) response accumulation. Appends data to a growing buffer. | |
| static void | process_stream_line (struct curl_context *ctx, const char *line) |
| Process a single SSE line from the streaming response. | |
| static size_t | write_cb (void *contents, size_t size, size_t nmemb, void *userp) |
| libcurl write callback that handles both streaming and non-streaming responses. | |
| static int | openai_chat (void *state_ptr, const csilk_ai_chat_request_t *req, csilk_ai_chat_response_t *res) |
| Execute a chat completion against the OpenAI /chat/completions endpoint. | |
| static int | openai_embeddings (void *state_ptr, const char *model, const char **input, size_t count, csilk_ai_embeddings_response_t *res) |
| Generate embeddings for a batch of input strings. | |
| void | csilk_ai_openai_init_driver (void) |
| Register the OpenAI driver with the AI subsystem. Called during startup to make "openai" available to csilk_ai_new(). | |
Variables | |
| static const csilk_ai_driver_t | openai_driver |
| Driver vtable for the OpenAI-compatible AI backend. | |
OpenAI-compatible driver for the AI unified interface.
Implements the csilk_ai_driver_t vtable for the OpenAI Chat Completions and Embeddings APIs. Also compatible with any OpenAI-API-proxy (Azure, Together, local推理 servers) since it only relies on the standard endpoint shapes:
Key design points:
| struct openai_state_t |
Per-instance state for the OpenAI-compatible driver.
| Data Fields | ||
|---|---|---|
| char * | api_key |
Bearer token sent as Authorization header. |
| char * | base_url |
API root (e.g., https://api.openai.com/v1). |
| struct curl_response |
Accumulates a complete HTTP response body in memory.
Accumulates a complete HTTP response body (used for embeddings).
| struct curl_context |
Per-request context passed to the streaming write callback. Holds both the request (for on_chunk callback) and response (to accumulate final content), plus a line-buffer for SSE frame reassembly.

| Data Fields | ||
|---|---|---|
| char * | body |
Accumulated response body (non-streaming path). |
| char * | line_buf |
Partial SSE line buffer (streaming path). |
| size_t | line_size |
Current length of buffered SSE data. |
| const csilk_ai_chat_request_t * | req | |
| csilk_ai_chat_response_t * | res | |
| size_t | size |
Current length of accumulated body. |
| void csilk_ai_openai_init_driver | ( | void | ) |
Register the OpenAI driver with the AI subsystem. Called during startup to make "openai" available to csilk_ai_new().
|
static |
Execute a chat completion against the OpenAI /chat/completions endpoint.
Flow:
Tool calls are extracted from "message"."tool_calls" in the first choice.
|
static |
Generate embeddings for a batch of input strings.
Flow:
|
static |
Free the OpenAI driver state and associated resources.
|
static |
Initialize the OpenAI driver state.
|
static |
Process a single SSE line from the streaming response.
Expects the standard OpenAI SSE format: data: {"choices":[{"delta":{"content":"text"}}]} The special "[DONE]" token signals stream completion.
For each delta chunk, the function:
|
static |
libcurl write callback that handles both streaming and non-streaming responses.
Non-streaming path: Appends raw bytes to ctx->body (same as write_cb_simple).
Streaming path (SSE): Appends bytes to a line reassembly buffer (ctx->line_buf), then extracts complete lines delimited by '
'. Each complete line is forwarded to process_stream_line(). A trailing partial line is preserved in the buffer for the next callback invocation.
|
static |
libcurl write callback for simple (non-streaming) response accumulation. Appends data to a growing buffer.
|
static |
Driver vtable for the OpenAI-compatible AI backend.