Class: LastLLM::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/last_llm/configuration.rb

Overview

Configuration class for LastLLM Handles global and provider-specific settings

Constant Summary collapse

PROVIDER_VALIDATIONS =

Provider validation configuration

{
  Providers::Constants::OPENAI => { required: [:api_key] },
  Providers::Constants::ANTHROPIC => { required: [:api_key] },
  Providers::Constants::GOOGLE_GEMINI => { required: [:api_key] },
  Providers::Constants::DEEPSEEK => { required: [:api_key] },
  Providers::Constants::OLLAMA => {
    required: [],
    custom: lambda { |config|
      return if config[:api_key]
      raise ConfigurationError, 'Ollama host is required when no API key is provided' unless config[:host]
    }
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Initialize a new configuration

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :default_provider (Symbol) — default: :openai

    The default provider to use

  • :default_model (String) — default: 'gpt-3.5-turbo'

    The default model to use

  • :test_mode (Boolean) — default: false

    Whether to run in test mode



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/last_llm/configuration.rb', line 42

def initialize(options = {})
  @default_provider = options[:default_provider] || Providers::Constants::OPENAI
  @default_model = options[:default_model] || 'gpt-3.5-turbo'
  @test_mode = options[:test_mode] || false
  @providers = {}
  @globals = {
    timeout: 60,
    max_retries: 3,
    retry_delay: 1
  }
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



32
33
34
# File 'lib/last_llm/configuration.rb', line 32

def base_url
  @base_url
end

#default_modelObject

Default model to use



27
28
29
# File 'lib/last_llm/configuration.rb', line 27

def default_model
  @default_model
end

#default_providerObject

Default provider to use



24
25
26
# File 'lib/last_llm/configuration.rb', line 24

def default_provider
  @default_provider
end

#globalsObject (readonly)

Global settings



35
36
37
# File 'lib/last_llm/configuration.rb', line 35

def globals
  @globals
end

#providersObject (readonly)

Provider-specific configurations



30
31
32
# File 'lib/last_llm/configuration.rb', line 30

def providers
  @providers
end

Instance Method Details

#configure_provider(provider, config = {}) ⇒ Hash

Configure a provider with specific settings

Parameters:

  • provider (Symbol)

    The provider name

  • config (Hash) (defaults to: {})

    Provider-specific configuration

Returns:

  • (Hash)

    The updated provider configuration



58
59
60
61
# File 'lib/last_llm/configuration.rb', line 58

def configure_provider(provider, config = {})
  @providers[provider] ||= {}
  @providers[provider].merge!(config)
end

#get_global(key) ⇒ Object

Get a global configuration value

Parameters:

  • key (Symbol)

    The configuration key

Returns:

  • The configuration value



111
112
113
# File 'lib/last_llm/configuration.rb', line 111

def get_global(key)
  @globals[key]
end

#provider_config(provider) ⇒ Hash

Get provider configuration

Parameters:

  • provider (Symbol)

    The provider name

Returns:

  • (Hash)

    The provider configuration



66
67
68
69
70
71
72
# File 'lib/last_llm/configuration.rb', line 66

def provider_config(provider)
  config = @providers[provider] || {}
  # Ensure skip_validation is set when in test mode
  config = config.dup
  config[:skip_validation] = true if @test_mode
  config
end

#set_global(key, value) ⇒ Object

Set a global configuration value

Parameters:

  • key (Symbol)

    The configuration key

  • value

    The configuration value

Returns:

  • The set value



104
105
106
# File 'lib/last_llm/configuration.rb', line 104

def set_global(key, value)
  @globals[key] = value
end

#test_mode?Boolean

Check if running in test mode

Returns:

  • (Boolean)

    Whether in test mode



76
77
78
# File 'lib/last_llm/configuration.rb', line 76

def test_mode?
  @test_mode
end

#validate_provider_config!(provider) ⇒ Object

Validate provider configuration based on requirements

Parameters:

  • provider (Symbol)

    The provider to validate

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/last_llm/configuration.rb', line 83

def validate_provider_config!(provider)
  return if @test_mode

  validation = PROVIDER_VALIDATIONS[provider]
  raise ConfigurationError, "Unknown provider: #{provider}" unless validation

  config = provider_config(provider)

  validation[:required]&.each do |key|
    raise ConfigurationError, "#{key.to_s.gsub('_', ' ')} is required for #{provider} provider" unless config[key]
  end

  return unless validation[:custom]

  validation[:custom].call(config)
end