Class: Durable::Llm::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



9
10
11
12
13
# File 'lib/durable/llm/configuration.rb', line 9

def initialize
  @providers = {}
  @default_provider = 'openai'
  load_from_env
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/durable/llm/configuration.rb', line 48

def method_missing(method_name, *args)
  if method_name.to_s.end_with?('=')
    provider = method_name.to_s.chomp('=').to_sym
    @providers[provider] = args.first
  else
    @providers[method_name]
  end
end

Instance Attribute Details

#default_providerObject

Returns the value of attribute default_provider.



6
7
8
# File 'lib/durable/llm/configuration.rb', line 6

def default_provider
  @default_provider
end

#providersObject (readonly)

Returns the value of attribute providers.



7
8
9
# File 'lib/durable/llm/configuration.rb', line 7

def providers
  @providers
end

Instance Method Details

#clearObject



15
16
17
18
# File 'lib/durable/llm/configuration.rb', line 15

def clear
  @providers.clear
  @default_provider = 'openai'
end

#load_from_datasetteObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/durable/llm/configuration.rb', line 20

def load_from_datasette
  config_file = File.expand_path('~/.config/io.datasette.llm/keys.json')

  if File.exist?(config_file)
    config_data = JSON.parse(File.read(config_file))

    Durable::Llm::Providers.providers.each do |provider|
      @providers[provider.to_sym] ||= OpenStruct.new

      @providers[provider.to_sym][:api_key] = config_data[provider.to_s] if config_data[provider.to_s]
    end
  end
rescue JSON::ParserError => e
  puts "Error parsing JSON file: #{e.message}"
end

#load_from_envObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/durable/llm/configuration.rb', line 36

def load_from_env
  ENV.each do |key, value|
    next unless key.start_with?('DLLM__')

    parts = key.split('__')
    provider = parts[1].downcase.to_sym
    setting = parts[2].downcase.to_sym
    @providers[provider] ||= OpenStruct.new
    @providers[provider][setting] = value
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/durable/llm/configuration.rb', line 57

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('=') || @providers.key?(method_name) || super
end