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.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#model ⇒ Object
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.
-
#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.
Class Method Summary collapse
-
.load_from_json(path) ⇒ Config
Load configuration from JSON file (useful for production deployments).
Instance Method Summary collapse
-
#apply_auth_to(req) ⇒ Object
Set Authorization header on a request when api_key is configured (e.g. for Ollama Cloud).
-
#http_connection_options(uri, read_timeout: timeout) ⇒ Hash
Net::HTTP connection options built from current config and target URI.
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #inspect ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ollama/config.rb', line 22 def initialize @base_url = "http://localhost:11434" @model = "llama3.2:3b" @timeout = 30 @retries = 2 @strict_json = true @temperature = 0.2 @top_p = 0.9 @num_ctx = 8192 @on_response = nil @api_key = nil end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def api_key @api_key end |
#base_url ⇒ Object
Returns the value of attribute base_url.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def base_url @base_url end |
#model ⇒ Object
Returns the value of attribute model.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def model @model end |
#num_ctx ⇒ Object
Returns the value of attribute num_ctx.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def num_ctx @num_ctx end |
#on_response ⇒ Object
Returns the value of attribute on_response.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def on_response @on_response end |
#retries ⇒ Object
Returns the value of attribute retries.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def retries @retries end |
#strict_json ⇒ Object
Returns the value of attribute strict_json.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def strict_json @strict_json end |
#temperature ⇒ Object
Returns the value of attribute temperature.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def temperature @temperature end |
#timeout ⇒ Object
Returns the value of attribute timeout.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def timeout @timeout end |
#top_p ⇒ Object
Returns the value of attribute top_p.
20 21 22 |
# File 'lib/ollama/config.rb', line 20 def top_p @top_p end |
Class Method Details
.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": "llama3.2:3b",
"timeout": 30,
"retries": 3,
"temperature": 0.2,
"top_p": 0.9,
"num_ctx": 8192
}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ollama/config.rb', line 93 def self.load_from_json(path) data = JSON.parse(File.read(path)) config = new config.base_url = data["base_url"] if data.key?("base_url") config.api_key = data["api_key"] if data.key?("api_key") config.model = data["model"] if data.key?("model") config.timeout = data["timeout"] if data.key?("timeout") config.retries = data["retries"] if data.key?("retries") config.strict_json = data["strict_json"] if data.key?("strict_json") config.temperature = data["temperature"] if data.key?("temperature") config.top_p = data["top_p"] if data.key?("top_p") config.num_ctx = data["num_ctx"] if data.key?("num_ctx") config rescue JSON::ParserError => e raise Error, "Failed to parse config JSON: #{e.}" rescue Errno::ENOENT raise Error, "Config file not found: #{path}" end |
Instance Method Details
#apply_auth_to(req) ⇒ Object
Set Authorization header on a request when api_key is configured (e.g. for Ollama Cloud). No-op when api_key is nil or empty.
39 40 41 42 43 |
# File 'lib/ollama/config.rb', line 39 def apply_auth_to(req) return if api_key.to_s.strip.empty? req["Authorization"] = "Bearer #{api_key}" end |
#http_connection_options(uri, read_timeout: timeout) ⇒ Hash
Net::HTTP connection options built from current config and target URI.
50 51 52 53 54 55 56 |
# File 'lib/ollama/config.rb', line 50 def (uri, read_timeout: timeout) { use_ssl: uri.scheme == "https", read_timeout: read_timeout, open_timeout: timeout } end |
#inspect ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ollama/config.rb', line 58 def inspect attributes = { base_url: base_url.inspect, model: model.inspect, timeout: timeout, retries: retries, strict_json: strict_json, temperature: temperature, top_p: top_p, num_ctx: num_ctx, api_key: "(redacted)" } "#<#{self.class.name} #{attributes.map { |k, v| "#{k}=#{v}" }.join(" ")}>" end |