Module: Kodo::LLM

Defined in:
lib/kodo/llm.rb

Constant Summary collapse

PROVIDER_SECRETS =

Map of canonical secret names to RubyLLM config setter names

{
  'anthropic_api_key' => 'anthropic',
  'openai_api_key' => 'openai',
  'gemini_api_key' => 'gemini',
  'deepseek_api_key' => 'deepseek',
  'mistral_api_key' => 'mistral',
  'openrouter_api_key' => 'openrouter',
  'perplexity_api_key' => 'perplexity',
  'xai_api_key' => 'xai'
}.freeze

Class Method Summary collapse

Class Method Details

.chat(model: nil) ⇒ Object

Create a new chat instance with the configured model



44
45
46
# File 'lib/kodo/llm.rb', line 44

def chat(model: nil)
  RubyLLM.chat(model: model || Kodo.config.llm_model)
end

.configure!(config, broker: nil) ⇒ Object

Configure RubyLLM from Kodo's config, optionally resolving keys via broker



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kodo/llm.rb', line 21

def configure!(config, broker: nil)
  RubyLLM.configure do |c|
    if broker
      configure_via_broker(c, broker)
    else
      config.llm_api_keys.each do |provider, key|
        setter = "#{provider}_api_key="
        c.send(setter, key) if c.respond_to?(setter)
      end
    end

    if (ollama_url = config.ollama_api_base)
      c.ollama_api_base = ollama_url
    end
  end

  # Refresh model registry so newly released models are recognized
  RubyLLM.models.refresh!

  Kodo.logger.info("LLM configured: #{config.llm_model}")
end

.utility_chat(model: nil) ⇒ Object

Create a chat instance with the utility model (for lightweight tasks like redaction)



49
50
51
# File 'lib/kodo/llm.rb', line 49

def utility_chat(model: nil)
  RubyLLM.chat(model: model || Kodo.config.utility_model)
end