Module: OllamaAgent::OllamaConnection

Defined in:
lib/ollama_agent/ollama_connection.rb

Overview

Maps OLLAMA_BASE_URL / OLLAMA_API_KEY / chat model ENV into Ollama::Config (local vs Ollama Cloud).

Class Method Summary collapse

Class Method Details

.apply_env_to_config(config) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/ollama_agent/ollama_connection.rb', line 8

def self.apply_env_to_config(config)
  url = ENV.fetch("OLLAMA_BASE_URL", nil)
  config.base_url = url if url && !url.strip.empty?

  key = ENV.fetch("OLLAMA_API_KEY", nil)
  config.api_key = key if key && !key.strip.empty?

  model = ModelEnv.resolved_model_from_env
  config.model = model if model
end

.retry_wrapped_client(timeout:, max_attempts:, base_url: nil, api_key: nil, hooks: nil, base_delay: nil) ⇒ Resilience::RetryMiddleware

Builds an Ollama::Client wrapped in Resilience::RetryMiddleware.

Parameters:

  • timeout (Numeric)

    read/open timeout seconds

  • max_attempts (Integer)

    retry attempts for the middleware

  • base_url (String, nil) (defaults to: nil)

    optional explicit API base URL

  • api_key (String, nil) (defaults to: nil)

    optional Bearer token (Ollama Cloud)

  • hooks (Streaming::Hooks, nil) (defaults to: nil)

    optional hooks for retry events

  • base_delay (Float, nil) (defaults to: nil)

    backoff base

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ollama_agent/ollama_connection.rb', line 28

def self.retry_wrapped_client(timeout:, max_attempts:, base_url: nil,
                              api_key: nil, hooks: nil, base_delay: nil)
  require "ollama_client"
  require_relative "resilience/retry_middleware"

  inner = Ollama::Client.new(config: config_for_client(
    base_url: base_url, timeout: timeout, api_key: api_key
  ))
  Resilience::RetryMiddleware.new(
    client: inner,
    max_attempts: max_attempts,
    hooks: hooks,
    base_delay: base_delay || Resilience::RetryMiddleware::DEFAULT_BASE_DELAY
  )
end