Skip to content

feat(ollama): allow setting base_url and api_key programmatically#1511

Open
majiayu000 wants to merge 1 commit into0xPlaygrounds:mainfrom
majiayu000:feat/issue-1070-ollama-base-url-api-key
Open

feat(ollama): allow setting base_url and api_key programmatically#1511
majiayu000 wants to merge 1 commit into0xPlaygrounds:mainfrom
majiayu000:feat/issue-1070-ollama-base-url-api-key

Conversation

@majiayu000
Copy link

Summary

Allow users to configure base_url and api_key for the Ollama provider programmatically, enabling support for multiple Ollama instances and proxied/authenticated deployments.

Fixes #1070

Changes

  • Added OllamaApiKey type with optional Bearer token auth (no auth by default)
  • Changed OllamaBuilder::ApiKey from Nothing to OllamaApiKey
  • from_env() now reads optional OLLAMA_API_KEY env var and defaults OLLAMA_API_BASE_URL to http://localhost:11434 (previously panicked when unset)
  • Implemented From<Nothing>, From<String>, From<&str> for full backward compatibility

Usage:

// No auth (unchanged)
let client = ollama::Client::new(Nothing).unwrap();

// With auth + custom URL
let client = ollama::Client::builder()
    .api_key("my-secret-key")
    .base_url("http://remote-ollama:11434")
    .build()
    .unwrap();

Test plan

  • All 14 existing Ollama tests pass (including test_client_initialization with Nothing)
  • Examples using .api_key(Nothing) compile without changes
  • cargo fmt clean
  • cargo clippy clean

Add OllamaApiKey type that supports both unauthenticated (default) and
Bearer token authenticated connections.  This enables users to connect
to multiple Ollama instances running on different servers with optional
API key authentication for proxied deployments.

The from_env() constructor now reads optional OLLAMA_API_KEY and
defaults OLLAMA_API_BASE_URL to http://localhost:11434 instead of
panicking when unset.

Backward compatible: Client::new(Nothing) and .api_key(Nothing) still
work via From<Nothing> for OllamaApiKey.

Fixes 0xPlaygrounds#1070

Signed-off-by: majiayu000 <1835304752@qq.com>
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.

feat: Allow setting base_url and api_key for Ollama provider

1 participant