Class: Ollama::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/config.rb

Overview

Configuration class with safe defaults for agent-grade usage

⚠️ THREAD SAFETY WARNING: Global configuration access is mutex-protected, but modifying global config while clients are active can cause race conditions. For concurrent agents or multi-threaded applications, use per-client configuration (recommended):

config = Ollama::Config.new
config.model = "llama3.1"
client = Ollama::Client.new(config: config)

Each client instance with its own config is thread-safe.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_value = nil) ⇒ Config

Returns a new instance of Config.

Parameters:

  • value (String, nil)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ollama/config.rb', line 30

def initialize(_value = nil)
  @base_url = "http://localhost:11434"
  @model = "qwen3.5:4b"
  @timeout = 30
  @retries = 2
  @strict_json = true
  @temperature = 0.2
  @top_p = 0.9
  @num_ctx = 8192
  @on_response = nil

  @api_key_pool = ApiKeyPool.new([])
  @api_keys = []
  @api_key = nil
  load_env_api_keys
  @enable_multi_key_concurrency = self.class.truthy_env?(ENV.fetch("ENABLE_MULTI_KEY_CONCURRENCY", nil))

  @transport_adapter = :net_http
  @provider = :ollama
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



24
25
26
# File 'lib/ollama/config.rb', line 24

def api_key
  @api_key
end

#api_key_poolObject (readonly)

Returns the value of attribute api_key_pool.



24
25
26
# File 'lib/ollama/config.rb', line 24

def api_key_pool
  @api_key_pool
end

#api_keysObject

Returns the value of attribute api_keys.



24
25
26
# File 'lib/ollama/config.rb', line 24

def api_keys
  @api_keys
end

#base_urlObject

Returns the value of attribute base_url.



21
22
23
# File 'lib/ollama/config.rb', line 21

def base_url
  @base_url
end

#enable_multi_key_concurrencyObject

Returns the value of attribute enable_multi_key_concurrency.



24
25
26
# File 'lib/ollama/config.rb', line 24

def enable_multi_key_concurrency
  @enable_multi_key_concurrency
end

#modelObject Also known as: default_model

Returns the value of attribute model.



21
22
23
# File 'lib/ollama/config.rb', line 21

def model
  @model
end

#num_ctxObject

Returns the value of attribute num_ctx.



21
22
23
# File 'lib/ollama/config.rb', line 21

def num_ctx
  @num_ctx
end

#on_responseObject

Returns the value of attribute on_response.



21
22
23
# File 'lib/ollama/config.rb', line 21

def on_response
  @on_response
end

#providerObject

Returns the value of attribute provider.



21
22
23
# File 'lib/ollama/config.rb', line 21

def provider
  @provider
end

#retriesObject

Returns the value of attribute retries.



21
22
23
# File 'lib/ollama/config.rb', line 21

def retries
  @retries
end

#strict_jsonObject

Returns the value of attribute strict_json.



21
22
23
# File 'lib/ollama/config.rb', line 21

def strict_json
  @strict_json
end

#temperatureObject

Returns the value of attribute temperature.



21
22
23
# File 'lib/ollama/config.rb', line 21

def temperature
  @temperature
end

#timeoutObject

Returns the value of attribute timeout.



21
22
23
# File 'lib/ollama/config.rb', line 21

def timeout
  @timeout
end

#top_pObject

Returns the value of attribute top_p.



21
22
23
# File 'lib/ollama/config.rb', line 21

def top_p
  @top_p
end

#transport_adapterObject

Returns the value of attribute transport_adapter.



21
22
23
# File 'lib/ollama/config.rb', line 21

def transport_adapter
  @transport_adapter
end

Class Method Details

.env_api_keysArray<String>

Resolve API keys from OLLAMA_API_KEYS with OLLAMA_API_KEY fallback.

Returns:

  • (Array<String>)

    frozen key list



191
192
193
194
195
196
# File 'lib/ollama/config.rb', line 191

def self.env_api_keys
  keys = parse_api_keys(ENV.fetch("OLLAMA_API_KEYS", nil))
  return keys unless keys.empty?

  parse_api_keys(ENV.fetch("OLLAMA_API_KEY", nil))
end

.load_from_json(path) ⇒ Config

Load configuration from JSON file (useful for production deployments)

The caller is responsible for ensuring the config path is trusted. Do not pass unvalidated user input directly to this method.

Example JSON:

{
"base_url": "http://localhost:11434",
"api_key": "optional-for-ollama-cloud",
"model": "qwen3.5:4b",
"provider": "ollama",
"timeout": 30,
"retries": 3,
"temperature": 0.2,
"top_p": 0.9,
"num_ctx": 8192
}

Parameters:

  • path (String)

    Path to JSON config file

Returns:

  • (Config)

    New Config instance



149
150
151
152
153
154
155
156
# File 'lib/ollama/config.rb', line 149

def self.load_from_json(path)
  data = JSON.parse(File.read(path))
  new.tap { |config| map_json_data(config, data) }
rescue JSON::ParserError => e
  raise Error, "Failed to parse config JSON: #{e.message}"
rescue Errno::ENOENT
  raise Error, "Config file not found: #{path}"
end

.parse_api_keys(value) ⇒ Array<String>

Parse a comma-separated String or Array of API keys into a frozen key list.

Parameters:

  • value (String, Array<String>, nil)

Returns:

  • (Array<String>)

    frozen key list



181
182
183
184
185
186
# File 'lib/ollama/config.rb', line 181

def self.parse_api_keys(value)
  Array(value).flat_map { |item| item.to_s.split(",") }
              .map(&:strip)
              .reject(&:empty?)
              .freeze
end

.truthy_env?(value) ⇒ Boolean

Parameters:

  • value (String, nil)

Returns:

  • (Boolean)


200
201
202
# File 'lib/ollama/config.rb', line 200

def self.truthy_env?(value)
  %w[1 true yes y on].include?(value.to_s.strip.downcase)
end

Instance Method Details

#apply_auth_to(req, api_key: self.api_key) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/ollama/config.rb', line 51

def apply_auth_to(req, api_key: self.api_key)
  headers = req.respond_to?(:headers) ? req.headers : req
  if api_key.to_s.strip.empty?
    headers.delete("Authorization")
  else
    headers["Authorization"] = "Bearer #{api_key}"
  end
end

#http_connection_options(uri, read_timeout: timeout) ⇒ Hash

Net::HTTP connection options built from current config and target URI.

Parameters:

  • uri (URI)
  • read_timeout (Integer) (defaults to: timeout)

Returns:

  • (Hash)

    options suitable for Net::HTTP.start



101
102
103
104
105
106
107
# File 'lib/ollama/config.rb', line 101

def http_connection_options(uri, read_timeout: timeout)
  {
    use_ssl: uri.scheme == "https",
    read_timeout: read_timeout,
    open_timeout: timeout
  }
end

#initialize_copy(source) ⇒ Object



204
205
206
207
208
209
# File 'lib/ollama/config.rb', line 204

def initialize_copy(source)
  super
  @api_keys = source.api_keys.dup.freeze
  @api_key = @api_keys.first
  rebuild_api_key_pool(@api_keys)
end

#inspectObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ollama/config.rb', line 109

def inspect
  attributes = {
    base_url: base_url.inspect,
    model: model.inspect,
    provider: provider.inspect,
    timeout: timeout,
    retries: retries,
    strict_json: strict_json,
    temperature: temperature,
    top_p: top_p,
    num_ctx: num_ctx,
    api_key: "(redacted)",
    api_keys: "(#{api_keys.size} configured)",
    enable_multi_key_concurrency: enable_multi_key_concurrency,
    transport_adapter: transport_adapter.inspect
  }

  "#<#{self.class.name} #{attributes.map { |k, v| "#{k}=#{v}" }.join(" ")}>"
end

#load_env_api_keysObject

Load the API key pool from the environment: OLLAMA_API_KEYS takes precedence over OLLAMA_API_KEY. When no env keys exist, the current pool stays as-is.



70
71
72
73
74
75
76
77
# File 'lib/ollama/config.rb', line 70

def load_env_api_keys
  keys = ENV.fetch("OLLAMA_API_KEYS", nil)
  keys = ENV.fetch("OLLAMA_API_KEY", nil) if keys.to_s.strip.empty?
  return if keys.to_s.strip.empty?

  parsed = self.class.parse_api_keys(keys)
  self.api_keys = parsed
end