Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
ai_ollama.c File Reference

Ollama driver for the AI unified interface. More...

#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "csilk/drivers/ai.h"
Include dependency graph for ai_ollama.c:

Data Structures

struct  ollama_state_t
 Per-instance state for the Ollama driver. More...
 
struct  curl_response
 Accumulates a complete HTTP response body in memory. More...
 

Functions

static void * ollama_init (const char *api_key, const char *base_url)
 Initialize the Ollama driver state.
 
static void ollama_free (void *state_ptr)
 Free the Ollama driver state and associated resources.
 
static size_t write_cb (void *contents, size_t size, size_t nmemb, void *userp)
 libcurl write callback that appends data to a growing buffer.
 
static int ollama_chat (void *state_ptr, const csilk_ai_chat_request_t *req, csilk_ai_chat_response_t *res)
 Execute a chat completion against the Ollama /api/chat endpoint.
 
static int ollama_embeddings (void *state_ptr, const char *model, const char **input, size_t count, csilk_ai_embeddings_response_t *res)
 Generate embeddings via Ollama (stub – not yet implemented).
 
void csilk_ai_ollama_init_driver (void)
 Register the Ollama driver with the AI subsystem. Called during startup to make "ollama" available to csilk_ai_new().
 

Variables

static const csilk_ai_driver_t ollama_driver
 Driver vtable for the Ollama AI backend.
 

Detailed Description

Ollama driver for the AI unified interface.

Implements the csilk_ai_driver_t vtable for local LLM inference via Ollama. Key differences from the OpenAI driver:

  • No API key required (Ollama runs locally by default).
  • Chat uses POST /api/chat with a nested "options" object for parameters.
  • Token counts use "prompt_eval_count" / "eval_count" instead of "usage".
  • No streaming support yet (simplified with "stream": false).

Data Structure Documentation

◆ ollama_state_t

struct ollama_state_t

Per-instance state for the Ollama driver.

Data Fields
char * base_url

Base URL of the Ollama server (e.g., http://localhost:11434).

◆ curl_response

struct curl_response

Accumulates a complete HTTP response body in memory.

Accumulates a complete HTTP response body (used for embeddings).

Data Fields
char * body

Heap-allocated body buffer (grown on each write callback).

Heap-allocated body buffer.

size_t size

Current length of data in body (excluding NUL terminator).

Current length of data in body.

Function Documentation

◆ csilk_ai_ollama_init_driver()

void csilk_ai_ollama_init_driver ( void  )

Register the Ollama driver with the AI subsystem. Called during startup to make "ollama" available to csilk_ai_new().

◆ ollama_chat()

static int ollama_chat ( void *  state_ptr,
const csilk_ai_chat_request_t req,
csilk_ai_chat_response_t res 
)
static

Execute a chat completion against the Ollama /api/chat endpoint.

Flow:

  1. Build a JSON request body with messages array + nested "options". Ollama differs from OpenAI by placing sampling params under "options": { "temperature": ..., "top_p": ... } instead of at the root.
  2. POST the JSON to <base_url>/api/chat with Content-Type: application/json.
  3. On HTTP success, parse the response extracting "message"."content" for the reply text and "prompt_eval_count" / "eval_count" for token usage.
  4. Returns 0 on success, -1 on transport or parse failure.
Note
Streaming is not yet implemented (always sends "stream": false).
No auth headers are set – Ollama's local server does not require one.

◆ ollama_embeddings()

static int ollama_embeddings ( void *  state_ptr,
const char *  model,
const char **  input,
size_t  count,
csilk_ai_embeddings_response_t res 
)
static

Generate embeddings via Ollama (stub – not yet implemented).

Todo:
Implement using Ollama's /api/embeddings endpoint.

◆ ollama_free()

static void ollama_free ( void *  state_ptr)
static

Free the Ollama driver state and associated resources.

◆ ollama_init()

static void * ollama_init ( const char *  api_key,
const char *  base_url 
)
static

Initialize the Ollama driver state.

Note
api_key is accepted but ignored – Ollama's local server requires no authentication. Allocates state, sets the server URL (defaulting to localhost:11434), and performs one-shot libcurl global init.

◆ write_cb()

static size_t write_cb ( void *  contents,
size_t  size,
size_t  nmemb,
void *  userp 
)
static

libcurl write callback that appends data to a growing buffer.

Returns
Number of bytes "consumed" (must equal realsize for the transfer to continue), or 0 to abort the transfer on OOM.

Variable Documentation

◆ ollama_driver

const csilk_ai_driver_t ollama_driver
static
Initial value:
= {
.name = "ollama",
.init = ollama_init,
.chat = ollama_chat,
.embeddings = ollama_embeddings,
.free = ollama_free,
}
static int ollama_embeddings(void *state_ptr, const char *model, const char **input, size_t count, csilk_ai_embeddings_response_t *res)
Generate embeddings via Ollama (stub – not yet implemented).
Definition ai_ollama.c:207
static void * ollama_init(const char *api_key, const char *base_url)
Initialize the Ollama driver state.
Definition ai_ollama.c:36
static void ollama_free(void *state_ptr)
Free the Ollama driver state and associated resources.
Definition ai_ollama.c:60
static int ollama_chat(void *state_ptr, const csilk_ai_chat_request_t *req, csilk_ai_chat_response_t *res)
Execute a chat completion against the Ollama /api/chat endpoint.
Definition ai_ollama.c:118

Driver vtable for the Ollama AI backend.