Skip to content

Feature: Implement Ollama Provider Functionality #65

Description

@ryuknull

Feature: Implement Ollama Provider Functionality

Category: Feature
Priority: High
Related: Providers module (crates/infrastructure/providers-ollama)


What

Implement the complete() and stream() methods for the Ollama provider to enable actual API requests to Ollama instances.


Why

The Ollama provider is currently configured in the gateway but returns "not yet implemented" errors. Users want to use local Ollama instances as providers through the Cortex gateway.

Current state:

async fn complete(&self, _req: &CompletionRequest) -> NuxaResult<CompletionResponse> {
    Err(NuxaError::provider("Ollama provider not yet implemented"))
}

Expected behavior:

  • Forward requests to Ollama's http://localhost:11434/api/generate (or /api/chat/generate for chat models)
  • Parse Ollama's response format
  • Convert to Cortex's CompletionResponse format
  • Handle streaming responses
  • Respect timeout configuration

What's Already There

  • OllamaProviderConfig with base_url, models, timeout_secs
  • Client initialization with timeout
  • Basic health check (returns "not supported")
  • Model validation in supported_models()

What Needs to Be Done

1. Update Provider Implementation

In crates/infrastructure/providers-ollama/src/lib.rs:

A. Implement complete()

async fn complete(&self, req: &CompletionRequest) -> NuxaResult<CompletionResponse> {
    // Convert Cortex CompletionRequest -> Ollama API request
    // Call Ollama API endpoint
    // Parse response
    // Convert to CompletionResponse
    // Return result
}

API endpoint: /api/generate (or /api/chat/generate for chat models)

Response format to handle:

{
  "model": "llama3.2",
  "created_at": "2024-01-15T...",
  "response": "Hello!",
  "done": true,
  "context": [...],
  "total_duration": 1234567890,
  "load_duration": 123456789,
  "prompt_eval_count": 10,
  "prompt_eval_duration": 123456789,
  "eval_count": 20,
  "eval_duration": 1111111101
}

B. Implement stream()

async fn stream(&self, req: &CompletionRequest) -> NuxaResult<futures::stream::BoxStream<'_, NuxaResult<StreamChunk>>> {
    // Similar to complete() but for streaming
    // Handle Ollama's chunked response
    // Convert to StreamChunk format
}

2. Request/Response Mapping

A. Input Mapping

  • Map CompletionRequest fields to Ollama fields
  • Handle system prompt, messages, temperature, max_tokens
  • Map model name (handle aliases if needed)

B. Output Mapping

  • Parse Ollama response into CompletionResponse
  • Token usage (prompt_tokens, completion_tokens)
  • Content and finish reason
  • Timing information (optional)

3. Error Handling

  • Handle connection timeouts (use configured timeout)
  • Handle invalid model names
  • Handle Ollama not running
  • Return appropriate Cortex errors (ProviderError, TimeoutError)

4. Testing

  • Mock Ollama server for tests
  • Test with real Ollama if available
  • Test streaming and non-streaming
  • Test various model types (base, chat, code)

Acceptance Criteria

  • Ollama provider can complete chat completion requests
  • Ollama provider can stream responses
  • Timouts work correctly (respects timeout_secs)
  • Invalid model names rejected gracefully
  • Ollama not running detected and reported
  • Parsing errors handled and reported
  • Tests pass for all scenarios

Notes

  • Ollama API docs: https://github.com/ollama/ollama/blob/main/docs/api.md
  • Consider using /api/chat/generate for chat models (if available)
  • Ollama supports both /api/generate and /api/chat/generate
  • Handle both JSON and chunked responses
  • Consider caching for repeated requests (if upstream supports it)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/providersProvider integrations (OpenAI, Anthropic, etc.)

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions