Class: Ollama::Config
- Inherits:
-
Object
- Object
- Ollama::Config
- 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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_key_pool ⇒ Object
readonly
Returns the value of attribute api_key_pool.
-
#api_keys ⇒ Object
Returns the value of attribute api_keys.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#enable_multi_key_concurrency ⇒ Object
Returns the value of attribute enable_multi_key_concurrency.
-
#model ⇒ Object
(also: #default_model)
Returns the value of attribute model.
-
#num_ctx ⇒ Object
Returns the value of attribute num_ctx.
-
#on_response ⇒ Object
Returns the value of attribute on_response.
-
#provider ⇒ Object
Returns the value of attribute provider.
-
#retries ⇒ Object
Returns the value of attribute retries.
-
#strict_json ⇒ Object
Returns the value of attribute strict_json.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#top_p ⇒ Object
Returns the value of attribute top_p.
-
#transport_adapter ⇒ Object
Returns the value of attribute transport_adapter.
Class Method Summary collapse
-
.env_api_keys ⇒ Array<String>
Resolve API keys from OLLAMA_API_KEYS with OLLAMA_API_KEY fallback.
-
.load_from_json(path) ⇒ Config
Load configuration from JSON file (useful for production deployments).
-
.parse_api_keys(value) ⇒ Array<String>
Parse a comma-separated String or Array of API keys into a frozen key list.
- .truthy_env?(value) ⇒ Boolean
Instance Method Summary collapse
- #apply_auth_to(req, api_key: self.api_key) ⇒ Object
-
#http_connection_options(uri, read_timeout: timeout) ⇒ Hash
Net::HTTP connection options built from current config and target URI.
-
#initialize(_value = nil) ⇒ Config
constructor
A new instance of Config.
- #initialize_copy(source) ⇒ Object
- #inspect ⇒ Object
-
#load_env_api_keys ⇒ Object
Load the API key pool from the environment: OLLAMA_API_KEYS takes precedence over OLLAMA_API_KEY.
Constructor Details
#initialize(_value = nil) ⇒ Config
Returns a new instance of Config.
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_key ⇒ Object
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_pool ⇒ Object (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_keys ⇒ Object
Returns the value of attribute api_keys.
24 25 26 |
# File 'lib/ollama/config.rb', line 24 def api_keys @api_keys end |
#base_url ⇒ Object
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_concurrency ⇒ Object
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 |
#model ⇒ Object 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_ctx ⇒ Object
Returns the value of attribute num_ctx.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def num_ctx @num_ctx end |
#on_response ⇒ Object
Returns the value of attribute on_response.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def on_response @on_response end |
#provider ⇒ Object
Returns the value of attribute provider.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def provider @provider end |
#retries ⇒ Object
Returns the value of attribute retries.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def retries @retries end |
#strict_json ⇒ Object
Returns the value of attribute strict_json.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def strict_json @strict_json end |
#temperature ⇒ Object
Returns the value of attribute temperature.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def temperature @temperature end |
#timeout ⇒ Object
Returns the value of attribute timeout.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def timeout @timeout end |
#top_p ⇒ Object
Returns the value of attribute top_p.
21 22 23 |
# File 'lib/ollama/config.rb', line 21 def top_p @top_p end |
#transport_adapter ⇒ Object
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_keys ⇒ Array<String>
Resolve API keys from OLLAMA_API_KEYS with OLLAMA_API_KEY fallback.
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
}
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.}" 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.
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
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.
101 102 103 104 105 106 107 |
# File 'lib/ollama/config.rb', line 101 def (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 |
#inspect ⇒ Object
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_keys ⇒ Object
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 |