Class: Yorishiro::Provider::Ollama
- Defined in:
- lib/yorishiro/provider/ollama.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
"http://localhost:11434"- DEFAULT_NUM_CTX =
8192- OUTPUT_TOKEN_RESERVE =
2048- MIN_CONTEXT_BUDGET =
1024
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #chat(conversation, tools: []) ⇒ Object
-
#context_budget_tokens ⇒ Object
Token budget the conversation should be trimmed to before each request.
-
#initialize(api_key: nil, model: nil, base_url: nil, num_ctx: nil) ⇒ Ollama
constructor
A new instance of Ollama.
Methods inherited from Base
Constructor Details
#initialize(api_key: nil, model: nil, base_url: nil, num_ctx: nil) ⇒ Ollama
Returns a new instance of Ollama.
11 12 13 14 15 |
# File 'lib/yorishiro/provider/ollama.rb', line 11 def initialize(api_key: nil, model: nil, base_url: nil, num_ctx: nil) @base_url = base_url || ENV.fetch("OLLAMA_HOST", DEFAULT_BASE_URL) @num_ctx = num_ctx super(api_key: api_key || "unused", model: model) end |
Class Method Details
.supported_models ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/yorishiro/provider/ollama.rb', line 17 def self.supported_models base_url = ENV.fetch("OLLAMA_HOST", DEFAULT_BASE_URL) uri = URI("#{base_url}/api/tags") response = Net::HTTP.get_response(uri) return [] unless response.is_a?(Net::HTTPSuccess) data = JSON.parse(response.body) data.fetch("models", []).map { |m| m["name"] } rescue StandardError [] end |
Instance Method Details
#chat(conversation, tools: []) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/yorishiro/provider/ollama.rb', line 29 def chat(conversation, tools: [], &) uri = URI("#{@base_url}/api/chat") body = { model: @model_name, messages: (conversation.), keep_alive: ENV.fetch("OLLAMA_KEEP_ALIVE", "10m"), options: { num_ctx: num_ctx }, stream: true } headers = { "Content-Type" => "application/json" } body[:tools] = format_tools(tools) unless tools.empty? debug_log("Ollama request", body) @last_meta = {} result = post_stream(uri, headers: headers, body: body, &) result[:meta] = @last_meta result[:usage] = { input: @last_meta[:prompt_eval_count], output: @last_meta[:eval_count] } debug_log("Ollama response", result) result end |
#context_budget_tokens ⇒ Object
Token budget the conversation should be trimmed to before each request. Reserves headroom for the model's output within the configured context window.
56 57 58 |
# File 'lib/yorishiro/provider/ollama.rb', line 56 def context_budget_tokens [num_ctx - OUTPUT_TOKEN_RESERVE, MIN_CONTEXT_BUDGET].max end |