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

#initializeConfig

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_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

#num_ctxObject

Returns the value of attribute num_ctx.



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

def num_ctx
  @num_ctx
end

#on_responseObject

Returns the value of attribute on_response.



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

def on_response
  @on_response
end

#retriesObject

Returns the value of attribute retries.



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

def retries
  @retries
end

#strict_jsonObject

Returns the value of attribute strict_json.



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

def strict_json
  @strict_json
end

#temperatureObject

Returns the value of attribute temperature.



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

def temperature
  @temperature
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#top_pObject

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
}

Parameters:

  • path (String)

    Path to JSON config file

Returns:

  • (Config)

    New Config instance



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.message}"
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.

Parameters:

  • req (Net::HTTP::Request)


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.

Parameters:

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

Returns:

  • (Hash)

    options suitable for Net::HTTP.start



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

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

#inspectObject



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