Class: Llm::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/config.rb

Overview

Reads LLM configuration from env vars and config/config.yml Env vars take precedence over config file values.

Constant Summary collapse

PROVIDERS =
%w[openai anthropic ollama].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



13
14
15
16
17
18
# File 'lib/llm/config.rb', line 13

def initialize
  @provider_name = env('RUBY_RAIDER_LLM_PROVIDER') || config_value('llm_provider')
  @api_key = env('RUBY_RAIDER_LLM_API_KEY') || config_value('llm_api_key')
  @model = env('RUBY_RAIDER_LLM_MODEL') || config_value('llm_model')
  @url = env('RUBY_RAIDER_LLM_URL') || config_value('llm_url')
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/llm/config.rb', line 11

def api_key
  @api_key
end

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/llm/config.rb', line 11

def model
  @model
end

#provider_nameObject (readonly)

Returns the value of attribute provider_name.



11
12
13
# File 'lib/llm/config.rb', line 11

def provider_name
  @provider_name
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/llm/config.rb', line 11

def url
  @url
end

Instance Method Details

#build_providerObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/llm/config.rb', line 27

def build_provider
  return nil unless configured?

  case @provider_name
  when 'openai'
    require_relative 'providers/openai_provider'
    Providers::OpenaiProvider.new(api_key: @api_key, model: @model)
  when 'anthropic'
    require_relative 'providers/anthropic_provider'
    Providers::AnthropicProvider.new(api_key: @api_key, model: @model)
  when 'ollama'
    require_relative 'providers/ollama_provider'
    Providers::OllamaProvider.new(model: @model, url: @url)
  end
end

#configured?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/llm/config.rb', line 20

def configured?
  return false unless @provider_name && PROVIDERS.include?(@provider_name)
  return true if @provider_name == 'ollama'

  !@api_key.nil? && !@api_key.empty?
end