Skip to content

feat(ollama): add OpenAI- and Anthropic-compatible clients - #2192

Open
bugprone wants to merge 1 commit into
0xPlaygrounds:mainfrom
bugprone:add-ollama-compat-clients
Open

feat(ollama): add OpenAI- and Anthropic-compatible clients#2192
bugprone wants to merge 1 commit into
0xPlaygrounds:mainfrom
bugprone:add-ollama-compat-clients

Conversation

@bugprone

Copy link
Copy Markdown
Contributor

Part of #2156, together with #2185 (which covers the native-payload max_tokens fix). This adds the API-switching half discussed there.

What

Ollama exposes three HTTP APIs: the native API, an OpenAI-compatible API, and an Anthropic-compatible API. rig previously only spoke the native one.

Following the same pattern zai and moonshot use for multiple API flavors (one typed client per flavor rather than a runtime switch), this adds:

  • ollama::OpenAiClient/v1/chat/completions, reusing OpenAICompatibleProvider + the generic OpenAI completion model
  • ollama::AnthropicClient/v1/messages, reusing AnthropicCompatibleProvider + the generic Anthropic completion model

The native ollama::Client is unchanged and documented as the default choice (it is the only flavor covering embeddings and model listing). No new request/response/streaming code — both clients ride the existing generic completion models.

Notes

  • The Messages API rejects requests without max_tokens (verified against a live Ollama), so the Anthropic-flavor client sends a default of 4096 when unset, matching zai/moonshot. The module docs call out this divergence from the other two flavors.
  • OpenAiClient::from_env tolerates an OLLAMA_API_BASE_URL that already carries the /v1 suffix, and a blank OLLAMA_API_KEY falls back to the placeholder value the Anthropic layer requires.

Verification

All three clients verified against a live Ollama (qwen2.5:0.5b), including the /v1-suffixed base URL and blank-API-key edge cases. cargo test -p rig-core --lib (1361 passed), cargo clippy --all-features --all-targets, and cargo fmt --check are clean.

Ollama exposes three HTTP APIs; rig previously only spoke the native
one. Following the zai/moonshot pattern of one typed client per API
flavor, this adds ollama::OpenAiClient (/v1/chat/completions) and
ollama::AnthropicClient (/v1/messages), both reusing the existing
OpenAICompatibleProvider and AnthropicCompatibleProvider generic
completion models. The native ollama::Client is unchanged and remains
the default choice.

The Messages API requires max_tokens, so the Anthropic-flavor client
sends a default of 4096 when unset; this divergence is called out in
the module docs. Base URL derivation tolerates OLLAMA_API_BASE_URL
values that already carry the /v1 suffix, and a blank OLLAMA_API_KEY
falls back to the placeholder the Anthropic layer requires.

Part of 0xPlaygrounds#2156.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant