Class: LastLLM::Configuration
- Inherits:
-
Object
- Object
- LastLLM::Configuration
- 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
- VALID_LOG_LEVELS =
[:debug, :info, :warn, :error, :fatal].freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#default_model ⇒ Object
Default model to use.
-
#default_provider ⇒ Object
Default provider to use.
-
#globals ⇒ Object
readonly
Global settings.
-
#logger ⇒ Object
Logger instance.
-
#providers ⇒ Object
readonly
Provider-specific configurations.
Instance Method Summary collapse
-
#configure_provider(provider, config = {}) ⇒ Hash
Configure a provider with specific settings.
-
#get_global(key) ⇒ Object
Get a global configuration value.
-
#get_provider_config(provider, key = nil) ⇒ Object
Get provider configuration value.
-
#initialize(options = {}) ⇒ Configuration
constructor
Initialize a new configuration.
- #log_level ⇒ Object
- #log_level=(level) ⇒ Object
-
#provider_config(provider) ⇒ Hash
Get provider configuration.
-
#set_global(key, value) ⇒ Object
Set a global configuration value.
-
#set_provider_config(provider, key, value) ⇒ Object
Set configuration value for a specific provider.
-
#test_mode? ⇒ Boolean
Check if running in test mode.
-
#validate_provider_config!(provider) ⇒ Object
Validate provider configuration based on requirements.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Initialize a new configuration
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/last_llm/configuration.rb', line 48 def initialize( = {}) @default_provider = [:default_provider] || Providers::Constants::OPENAI @default_model = [:default_model] || 'gpt-3.5-turbo' @test_mode = [:test_mode] || false @providers = {} @globals = { timeout: 60, max_retries: 3, retry_delay: 1 } @provider_configs = {} @logger = nil @log_level = :info end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
35 36 37 |
# File 'lib/last_llm/configuration.rb', line 35 def base_url @base_url end |
#default_model ⇒ Object
Default model to use
30 31 32 |
# File 'lib/last_llm/configuration.rb', line 30 def default_model @default_model end |
#default_provider ⇒ Object
Default provider to use
27 28 29 |
# File 'lib/last_llm/configuration.rb', line 27 def default_provider @default_provider end |
#globals ⇒ Object (readonly)
Global settings
38 39 40 |
# File 'lib/last_llm/configuration.rb', line 38 def globals @globals end |
#logger ⇒ Object
Logger instance
41 42 43 |
# File 'lib/last_llm/configuration.rb', line 41 def logger @logger end |
#providers ⇒ Object (readonly)
Provider-specific configurations
33 34 35 |
# File 'lib/last_llm/configuration.rb', line 33 def providers @providers end |
Instance Method Details
#configure_provider(provider, config = {}) ⇒ Hash
Configure a provider with specific settings
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/last_llm/configuration.rb', line 79 def configure_provider(provider, config = {}) provider_sym = provider.to_sym @providers[provider_sym] ||= {} @providers[provider_sym].merge!(config) # Also update @provider_configs to maintain consistency @provider_configs ||= {} @provider_configs[provider_sym] ||= {} @provider_configs[provider_sym].merge!(config) end |
#get_global(key) ⇒ Object
Get a global configuration value
157 158 159 |
# File 'lib/last_llm/configuration.rb', line 157 def get_global(key) @globals[key] end |
#get_provider_config(provider, key = nil) ⇒ Object
Get provider configuration value
111 112 113 114 115 116 117 |
# File 'lib/last_llm/configuration.rb', line 111 def get_provider_config(provider, key = nil) @provider_configs ||= {} provider_config = @provider_configs[provider.to_sym] || {} return provider_config if key.nil? provider_config[key.to_sym] end |
#log_level ⇒ Object
71 72 73 |
# File 'lib/last_llm/configuration.rb', line 71 def log_level @log_level end |
#log_level=(level) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/last_llm/configuration.rb', line 63 def log_level=(level) level_sym = level.to_sym unless VALID_LOG_LEVELS.include?(level_sym) raise ConfigurationError, "Invalid log level: #{level}. Valid levels are: #{VALID_LOG_LEVELS.join(', ')}" end @log_level = level_sym end |
#provider_config(provider) ⇒ Hash
Get provider configuration
93 94 95 96 97 98 99 100 |
# File 'lib/last_llm/configuration.rb', line 93 def provider_config(provider) provider_sym = provider.to_sym config = @provider_configs&.dig(provider_sym) || @providers[provider_sym] || {} # 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
150 151 152 |
# File 'lib/last_llm/configuration.rb', line 150 def set_global(key, value) @globals[key] = value end |
#set_provider_config(provider, key, value) ⇒ Object
Set configuration value for a specific provider
103 104 105 106 107 108 |
# File 'lib/last_llm/configuration.rb', line 103 def set_provider_config(provider, key, value) @provider_configs ||= {} provider_sym = provider.to_sym @provider_configs[provider_sym] ||= {} @provider_configs[provider_sym][key.to_sym] = value end |
#test_mode? ⇒ Boolean
Check if running in test mode
120 121 122 |
# File 'lib/last_llm/configuration.rb', line 120 def test_mode? @test_mode end |
#validate_provider_config!(provider) ⇒ Object
Validate provider configuration based on requirements
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/last_llm/configuration.rb', line 127 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| unless config[key.to_sym] raise ConfigurationError, "#{key.to_s.gsub('_', ' ')} is required for #{provider} provider" end end return unless validation[:custom] validation[:custom].call(config) end |