Skip to main content

Module openai

Module openai 

Source
Expand description

OpenAI-compatible /chat/completions adapter with native tool-calling. RFC 0006 §canonical-wire.

Canonical because it covers vLLM / Ollama / LM Studio / most hosted gateways and gives the model first-class tools + tool_calls. This module is pure translation: neutral Request → OpenAI JSON, and OpenAI JSON → neutral Response. No I/O (that’s intel/client.rs).

Constants§

DEFAULT_PATH
The default endpoint path when the intelligence URL carries no explicit path (https://host[:port] with path /).
MODELS_PATH
The OpenAI-compatible model-list path (RFC 0018 §5.4 discovery probe). The /v1 API root has a sibling /models next to /chat/completions.

Functions§

build_request
Build the request body (JSON bytes) and the HTTP headers for a chat completion. token, if present, becomes Authorization: Bearer ….
models_path
Derive the model-discovery GET path (RFC 0018 §5.4) as the sibling of the configured chat path: swap the trailing …/chat/completions segment for …/models so a non-default API root (e.g. a gateway mounted at /proxy/v1) keeps its prefix. Anything that isn’t the canonical chat suffix falls back to the absolute MODELS_PATH — discovery is best-effort, never a hard failure.
parse_models
Parse an OpenAI-compatible /v1/models response body into the list of model ids (RFC 0018 §5.4). The shape is { "data": [ { "id": "…" }, … ] }. Any missing/empty/non-array data, or a non-JSON body, yields an empty list — discovery degrades silently (it is never a failover-class failure, §5.4).
parse_response
Parse an OpenAI /chat/completions response body into the neutral Response. Tolerant: missing usage → zero; unknown finish reason → StopReason::Other; tool-call arguments that aren’t valid JSON are wrapped as {"_raw": "…"} rather than dropped.