active_genie 0.0.12 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +65 -22
  3. data/VERSION +1 -1
  4. data/lib/active_genie/battle/README.md +7 -7
  5. data/lib/active_genie/battle/basic.rb +47 -31
  6. data/lib/active_genie/battle.rb +4 -0
  7. data/lib/active_genie/clients/anthropic_client.rb +110 -0
  8. data/lib/active_genie/clients/google_client.rb +158 -0
  9. data/lib/active_genie/clients/helpers/retry.rb +29 -0
  10. data/lib/active_genie/clients/openai_client.rb +61 -83
  11. data/lib/active_genie/clients/unified_client.rb +4 -4
  12. data/lib/active_genie/concerns/loggable.rb +44 -0
  13. data/lib/active_genie/configuration/log_config.rb +1 -1
  14. data/lib/active_genie/configuration/providers/anthropic_config.rb +54 -0
  15. data/lib/active_genie/configuration/providers/base_config.rb +85 -0
  16. data/lib/active_genie/configuration/providers/deepseek_config.rb +54 -0
  17. data/lib/active_genie/configuration/providers/google_config.rb +56 -0
  18. data/lib/active_genie/configuration/providers/openai_config.rb +54 -0
  19. data/lib/active_genie/configuration/providers_config.rb +7 -4
  20. data/lib/active_genie/configuration/runtime_config.rb +35 -0
  21. data/lib/active_genie/configuration.rb +18 -4
  22. data/lib/active_genie/data_extractor/basic.rb +15 -2
  23. data/lib/active_genie/data_extractor.rb +4 -0
  24. data/lib/active_genie/logger.rb +40 -21
  25. data/lib/active_genie/ranking/elo_round.rb +71 -50
  26. data/lib/active_genie/ranking/free_for_all.rb +31 -14
  27. data/lib/active_genie/ranking/player.rb +11 -16
  28. data/lib/active_genie/ranking/players_collection.rb +4 -4
  29. data/lib/active_genie/ranking/ranking.rb +74 -19
  30. data/lib/active_genie/ranking/ranking_scoring.rb +3 -3
  31. data/lib/active_genie/scoring/basic.rb +44 -25
  32. data/lib/active_genie/scoring.rb +3 -0
  33. data/lib/tasks/benchmark.rake +27 -0
  34. metadata +91 -70
  35. data/lib/active_genie/configuration/openai_config.rb +0 -56
@@ -0,0 +1,29 @@
1
+ MAX_RETRIES = 3
2
+ BASE_DELAY = 0.5
3
+
4
+ def retry_with_backoff(config: {})
5
+ retries = config[:runtime][:max_retries] || MAX_RETRIES
6
+
7
+ begin
8
+ yield
9
+ rescue => e
10
+ if retries > 0
11
+ ActiveGenie::Logger.warn({ code: :retry_with_backoff, message: "Retrying request after error: #{e.message}. Attempts remaining: #{retries}" })
12
+
13
+ retries -= 1
14
+ backoff_time = calculate_backoff(MAX_RETRIES - retries)
15
+ sleep(backoff_time)
16
+ retry
17
+ else
18
+ raise
19
+ end
20
+ end
21
+ end
22
+
23
+ def calculate_backoff(retry_count)
24
+ # Exponential backoff with jitter: 2^retry_count + random jitter
25
+ # Base delay is 0.5 seconds, doubles each retry, plus up to 0.5 seconds of random jitter
26
+ # Simplified example: 0.5, 1, 2, 4, 8, 12, 16, 20, 24, 28, 30 seconds
27
+ jitter = rand * BASE_DELAY
28
+ [BASE_DELAY * (2 ** retry_count) + jitter, 30].min # Cap at 30 seconds
29
+ end
@@ -1,119 +1,97 @@
1
1
  require 'json'
2
2
  require 'net/http'
3
3
 
4
+ require_relative './helpers/retry'
5
+
4
6
  module ActiveGenie::Clients
5
- class OpenaiClient
6
- MAX_RETRIES = 3
7
-
7
+ class OpenaiClient
8
8
  class OpenaiError < StandardError; end
9
9
  class RateLimitError < OpenaiError; end
10
+ class InvalidResponseError < StandardError; end
10
11
 
11
12
  def initialize(config)
12
13
  @app_config = config
13
14
  end
14
15
 
16
+ # Requests structured JSON output from the Gemini model based on a schema.
17
+ #
18
+ # @param messages [Array<Hash>] A list of messages representing the conversation history.
19
+ # Each hash should have :role ('user' or 'model') and :content (String).
20
+ # Gemini uses 'user' and 'model' roles.
21
+ # @param function [Hash] A JSON schema definition describing the desired output format.
22
+ # @param model_tier [Symbol, nil] A symbolic representation of the model quality/size tier.
23
+ # @param config [Hash] Optional configuration overrides:
24
+ # - :api_key [String] Override the default API key.
25
+ # - :model [String] Override the model name directly.
26
+ # - :max_retries [Integer] Max retries for the request.
27
+ # - :retry_delay [Integer] Initial delay for retries.
28
+ # @return [Hash, nil] The parsed JSON object matching the schema, or nil if parsing fails or content is empty.
15
29
  def function_calling(messages, function, model_tier: nil, config: {})
16
- model = config[:model] || @app_config.tier_to_model(model_tier)
30
+ model = config[:runtime][:model] || @app_config.tier_to_model(model_tier)
17
31
 
18
32
  payload = {
19
33
  messages:,
20
- response_format: {
21
- type: 'json_schema',
22
- json_schema: function
23
- },
34
+ tools: [{ type: 'function', function: }],
35
+ tool_choice: { type: 'function', function: { name: function[:name] } },
36
+ stream: false,
24
37
  model:,
25
38
  }
26
39
 
27
- api_key = config[:api_key] || @app_config.api_key
40
+ api_key = config[:runtime][:api_key] || @app_config.api_key
28
41
  headers = DEFAULT_HEADERS.merge(
29
42
  'Authorization': "Bearer #{api_key}"
30
43
  ).compact
31
44
 
32
- response = request(payload, headers, config:)
45
+ retry_with_backoff(config:) do
46
+ response = request(payload, headers, config:)
33
47
 
34
- parsed_response = JSON.parse(response.dig('choices', 0, 'message', 'content'))
35
- parsed_response.dig('properties') || parsed_response
36
- rescue JSON::ParserError
37
- nil
38
- end
48
+ parsed_response = JSON.parse(response.dig('choices', 0, 'message', 'tool_calls', 0, 'function', 'arguments'))
49
+ parsed_response = parsed_response.dig('message') || parsed_response
39
50
 
40
- private
51
+ raise InvalidResponseError, "Invalid response: #{parsed_response}" if parsed_response.nil? || parsed_response.keys.size.zero?
41
52
 
42
- def request(payload, headers, config:)
43
- retries = config[:max_retries] || MAX_RETRIES
44
- start_time = Time.now
45
-
46
- begin
47
- response = Net::HTTP.post(
48
- URI("#{@app_config.api_url}/chat/completions"),
49
- payload.to_json,
50
- headers
51
- )
52
-
53
- if response.is_a?(Net::HTTPTooManyRequests)
54
- raise RateLimitError, "OpenAI API rate limit exceeded: #{response.body}"
55
- end
56
-
57
- raise OpenaiError, response.body unless response.is_a?(Net::HTTPSuccess)
58
-
59
- return nil if response.body.empty?
60
-
61
- parsed_body = JSON.parse(response.body)
62
- # log_response(start_time, parsed_body, config:)
63
-
64
- parsed_body
65
- rescue OpenaiError, Net::HTTPError, JSON::ParserError, Errno::ECONNRESET, Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout => e
66
- if retries > 0
67
- retries -= 1
68
- backoff_time = calculate_backoff(MAX_RETRIES - retries)
69
- ActiveGenie::Logger.trace(
70
- {
71
- category: :llm,
72
- trace: "#{config.dig(:log, :trace)}/#{self.class.name}",
73
- message: "Retrying request after error: #{e.message}. Attempts remaining: #{retries}",
74
- backoff_time: backoff_time
75
- }
76
- )
77
- sleep(backoff_time)
78
- retry
79
- else
80
- ActiveGenie::Logger.trace(
81
- {
82
- category: :llm,
83
- trace: "#{config.dig(:log, :trace)}/#{self.class.name}",
84
- message: "Max retries reached. Failing with error: #{e.message}"
85
- }
86
- )
87
- raise
88
- end
53
+ ActiveGenie::Logger.trace({code: :function_calling, payload:, parsed_response: })
54
+
55
+ parsed_response
89
56
  end
90
57
  end
91
58
 
92
- BASE_DELAY = 0.5
93
- def calculate_backoff(retry_count)
94
- # Exponential backoff with jitter: 2^retry_count + random jitter
95
- # Base delay is 0.5 seconds, doubles each retry, plus up to 0.5 seconds of random jitter
96
- # Simplified example: 0.5, 1, 2, 4, 8, 12, 16, 20, 24, 28, 30 seconds
97
- jitter = rand * BASE_DELAY
98
- [BASE_DELAY * (2 ** retry_count) + jitter, 30].min # Cap at 30 seconds
99
- end
59
+ private
100
60
 
101
61
  DEFAULT_HEADERS = {
102
62
  'Content-Type': 'application/json',
103
63
  }
104
64
 
105
- def log_response(start_time, response, config: {})
106
- ActiveGenie::Logger.trace(
107
- {
108
- **config.dig(:log),
109
- category: :llm,
110
- trace: "#{config.dig(:log, :trace)}/#{self.class.name}",
111
- total_tokens: response.dig('usage', 'total_tokens'),
112
- model: response.dig('model'),
113
- request_duration: Time.now - start_time,
114
- openai: response
115
- }
65
+ def request(payload, headers, config:)
66
+ start_time = Time.now
67
+
68
+ response = Net::HTTP.post(
69
+ URI("#{@app_config.api_url}/chat/completions"),
70
+ payload.to_json,
71
+ headers
116
72
  )
73
+
74
+ if response.is_a?(Net::HTTPTooManyRequests)
75
+ raise RateLimitError, "OpenAI API rate limit exceeded: #{response.body}"
76
+ end
77
+
78
+ raise OpenaiError, response.body unless response.is_a?(Net::HTTPSuccess)
79
+
80
+ return nil if response.body.empty?
81
+
82
+ parsed_body = JSON.parse(response.body)
83
+
84
+ ActiveGenie::Logger.trace({
85
+ code: :llm_usage,
86
+ input_tokens: parsed_body.dig('usage', 'prompt_tokens'),
87
+ output_tokens: parsed_body.dig('usage', 'completion_tokens'),
88
+ total_tokens: parsed_body.dig('usage', 'prompt_tokens') + parsed_body.dig('usage', 'completion_tokens'),
89
+ model: payload[:model],
90
+ duration: Time.now - start_time,
91
+ usage: parsed_body.dig('usage')
92
+ })
93
+
94
+ parsed_body
117
95
  end
118
96
  end
119
- end
97
+ end
@@ -2,12 +2,12 @@ module ActiveGenie::Clients
2
2
  class UnifiedClient
3
3
  class << self
4
4
  def function_calling(messages, function, model_tier: nil, config: {})
5
- provider_name = config[:provider]&.downcase&.strip&.to_sym
6
- provider = ActiveGenie.configuration.providers.all[provider_name] || ActiveGenie.configuration.providers.default
5
+ provider_name = config[:runtime][:provider]&.to_s&.downcase&.strip&.to_sym || ActiveGenie.configuration.providers.default
6
+ provider_instance = ActiveGenie.configuration.providers.valid[provider_name]
7
7
 
8
- raise InvalidProviderError if provider.nil? || provider.client.nil?
8
+ raise InvalidProviderError if provider_instance.nil? || provider_instance.client.nil?
9
9
 
10
- provider.client.function_calling(messages, function, model_tier:, config:)
10
+ provider_instance.client.function_calling(messages, function, model_tier:, config:)
11
11
  end
12
12
 
13
13
  private
@@ -0,0 +1,44 @@
1
+ module ActiveGenie
2
+ module Concerns
3
+ module Loggable
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def with_logging_context(context_method, observer_proc = nil)
10
+ original_method = instance_method(:call)
11
+
12
+ define_method(:call) do |*args, **kwargs, &block|
13
+ context = send(context_method, *args, **kwargs)
14
+ bound_observer = observer_proc ? ->(log) { instance_exec(log, &observer_proc) } : nil
15
+
16
+ ActiveGenie::Logger.with_context(context, observer: bound_observer) do
17
+ original_method.bind(self).call(*args, **kwargs, &block)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ def info(log)
24
+ ::ActiveGenie::Logger.info(log)
25
+ end
26
+
27
+ def error(log)
28
+ ::ActiveGenie::Logger.error(log)
29
+ end
30
+
31
+ def warn(log)
32
+ ::ActiveGenie::Logger.warn(log)
33
+ end
34
+
35
+ def debug(log)
36
+ ::ActiveGenie::Logger.debug(log)
37
+ end
38
+
39
+ def trace(log)
40
+ ::ActiveGenie::Logger.trace(log)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -8,7 +8,7 @@ module ActiveGenie::Configuration
8
8
  end
9
9
 
10
10
  def to_h(config = {})
11
- { log_level:, **config }
11
+ { log_level: }.merge(config)
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,54 @@
1
+ require_relative '../../clients/anthropic_client'
2
+ require_relative './base_config'
3
+
4
+ module ActiveGenie
5
+ module Configuration::Providers
6
+ # Configuration class for the Anthropic API client.
7
+ # Manages API keys, URLs, model selections, and client instantiation.
8
+ class AnthropicConfig < BaseConfig
9
+ NAME = :anthropic
10
+
11
+ # Retrieves the API key.
12
+ # Falls back to the ANTHROPIC_API_KEY environment variable if not set.
13
+ # @return [String, nil] The API key.
14
+ def api_key
15
+ @api_key || ENV['ANTHROPIC_API_KEY']
16
+ end
17
+
18
+ # Retrieves the base API URL for Anthropic API.
19
+ # Defaults to 'https://api.anthropic.com'.
20
+ # @return [String] The API base URL.
21
+ def api_url
22
+ @api_url || 'https://api.anthropic.com'
23
+ end
24
+
25
+ # Lazily initializes and returns an instance of the AnthropicClient.
26
+ # Passes itself (the config object) to the client's constructor.
27
+ # @return [ActiveGenie::Clients::AnthropicClient] The client instance.
28
+ def client
29
+ @client ||= ::ActiveGenie::Clients::AnthropicClient.new(self)
30
+ end
31
+
32
+ # Retrieves the model name designated for the lower tier (e.g., cost-effective, faster).
33
+ # Defaults to 'claude-3-haiku'.
34
+ # @return [String] The lower tier model name.
35
+ def lower_tier_model
36
+ @lower_tier_model || 'claude-3-5-haiku-20241022'
37
+ end
38
+
39
+ # Retrieves the model name designated for the middle tier (e.g., balanced performance).
40
+ # Defaults to 'claude-3-sonnet'.
41
+ # @return [String] The middle tier model name.
42
+ def middle_tier_model
43
+ @middle_tier_model || 'claude-3-7-sonnet-20250219'
44
+ end
45
+
46
+ # Retrieves the model name designated for the upper tier (e.g., most capable).
47
+ # Defaults to 'claude-3-opus'.
48
+ # @return [String] The upper tier model name.
49
+ def upper_tier_model
50
+ @upper_tier_model || 'claude-3-opus-20240229'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,85 @@
1
+ module ActiveGenie
2
+ module Configuration::Providers
3
+ class BaseConfig
4
+ NAME = :unknown
5
+
6
+ attr_writer :api_key, :organization, :api_url, :client,
7
+ :lower_tier_model, :middle_tier_model, :upper_tier_model
8
+
9
+ # Maps a symbolic tier (:lower_tier, :middle_tier, :upper_tier) to a specific model name.
10
+ # Falls back to the lower_tier_model if the tier is nil or unrecognized.
11
+ # @param tier [Symbol, String, nil] The symbolic tier name.
12
+ # @return [String] The corresponding model name.
13
+ def tier_to_model(tier)
14
+ {
15
+ lower_tier: lower_tier_model,
16
+ middle_tier: middle_tier_model,
17
+ upper_tier: upper_tier_model
18
+ }[tier&.to_sym] || lower_tier_model
19
+ end
20
+
21
+ # Returns a hash representation of the configuration.
22
+ # @param config [Hash] Additional key-value pairs to merge into the hash.
23
+ # @return [Hash] The configuration settings as a hash.
24
+ def to_h(config = {})
25
+ {
26
+ name: NAME,
27
+ api_key:,
28
+ api_url:,
29
+ lower_tier_model:,
30
+ middle_tier_model:,
31
+ upper_tier_model:,
32
+ **config
33
+ }
34
+ end
35
+
36
+ # Validates the configuration.
37
+ # @return [Boolean] True if the configuration is valid, false otherwise.
38
+ def valid?
39
+ api_key && api_url
40
+ end
41
+
42
+ # Retrieves the API key.
43
+ # Falls back to the OPENAI_API_KEY environment variable if not set.
44
+ # @return [String, nil] The API key.
45
+ def api_key
46
+ raise NotImplementedError, "Subclasses must implement this method"
47
+ end
48
+
49
+ # Retrieves the base API URL for OpenAI API.
50
+ # Defaults to 'https://api.openai.com/v1'.
51
+ # @return [String] The API base URL.
52
+ def api_url
53
+ raise NotImplementedError, "Subclasses must implement this method"
54
+ end
55
+
56
+ # Lazily initializes and returns an instance of the OpenaiClient.
57
+ # Passes itself (the config object) to the client's constructor.
58
+ # @return [ActiveGenie::Clients::OpenaiClient] The client instance.
59
+ def client
60
+ raise NotImplementedError, "Subclasses must implement this method"
61
+ end
62
+
63
+ # Retrieves the model name designated for the lower tier (e.g., cost-effective, faster).
64
+ # Defaults to 'gpt-4o-mini'.
65
+ # @return [String] The lower tier model name.
66
+ def lower_tier_model
67
+ raise NotImplementedError, "Subclasses must implement this method"
68
+ end
69
+
70
+ # Retrieves the model name designated for the middle tier (e.g., balanced performance).
71
+ # Defaults to 'gpt-4o'.
72
+ # @return [String] The middle tier model name.
73
+ def middle_tier_model
74
+ raise NotImplementedError, "Subclasses must implement this method"
75
+ end
76
+
77
+ # Retrieves the model name designated for the upper tier (e.g., most capable).
78
+ # Defaults to 'o1-preview'.
79
+ # @return [String] The upper tier model name.
80
+ def upper_tier_model
81
+ raise NotImplementedError, "Subclasses must implement this method"
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../../clients/openai_client'
2
+ require_relative './base_config'
3
+
4
+ module ActiveGenie
5
+ module Configuration::Providers
6
+ # Configuration class for the DeepSeek API client.
7
+ # Manages API keys, organization IDs, URLs, model selections, and client instantiation.
8
+ class DeepseekConfig < BaseConfig
9
+ NAME = :deepseek
10
+
11
+ # Retrieves the API key.
12
+ # Falls back to the DEEPSEEK_API_KEY environment variable if not set.
13
+ # @return [String, nil] The API key.
14
+ def api_key
15
+ @api_key || ENV['DEEPSEEK_API_KEY']
16
+ end
17
+
18
+ # Retrieves the base API URL for DeepSeek API.
19
+ # Defaults to 'https://api.deepseek.com/v1'.
20
+ # @return [String] The API base URL.
21
+ def api_url
22
+ @api_url || 'https://api.deepseek.com/v1'
23
+ end
24
+
25
+ # Lazily initializes and returns an instance of the OpenaiClient.
26
+ # Passes itself (the config object) to the client's constructor.
27
+ # @return [ActiveGenie::Clients::OpenaiClient] The client instance.
28
+ def client
29
+ @client ||= ::ActiveGenie::Clients::OpenaiClient.new(self)
30
+ end
31
+
32
+ # Retrieves the model name designated for the lower tier (e.g., cost-effective, faster).
33
+ # Defaults to 'deepseek-chat'.
34
+ # @return [String] The lower tier model name.
35
+ def lower_tier_model
36
+ @lower_tier_model || 'deepseek-chat'
37
+ end
38
+
39
+ # Retrieves the model name designated for the middle tier (e.g., balanced performance).
40
+ # Defaults to 'deepseek-chat'.
41
+ # @return [String] The middle tier model name.
42
+ def middle_tier_model
43
+ @middle_tier_model || 'deepseek-chat'
44
+ end
45
+
46
+ # Retrieves the model name designated for the upper tier (e.g., most capable).
47
+ # Defaults to 'deepseek-reasoner'.
48
+ # @return [String] The upper tier model name.
49
+ def upper_tier_model
50
+ @upper_tier_model || 'deepseek-reasoner'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,56 @@
1
+ require_relative '../../clients/google_client'
2
+ require_relative './base_config'
3
+
4
+ module ActiveGenie
5
+ module Configuration::Providers
6
+ # Configuration class for the Google Generative Language API client.
7
+ # Manages API keys, URLs, model selections, and client instantiation.
8
+ class GoogleConfig < BaseConfig
9
+ NAME = :google
10
+
11
+ # Retrieves the API key.
12
+ # Falls back to the GENERATIVE_LANGUAGE_GOOGLE_API_KEY environment variable if not set.
13
+ # @return [String, nil] The API key.
14
+ def api_key
15
+ @api_key || ENV['GENERATIVE_LANGUAGE_GOOGLE_API_KEY'] || ENV['GEMINI_API_KEY']
16
+ end
17
+
18
+ # Retrieves the base API URL for Google Generative Language API.
19
+ # Defaults to 'https://generativelanguage.googleapis.com'.
20
+ # @return [String] The API base URL.
21
+ def api_url
22
+ # Note: Google Generative Language API uses a specific path structure like /v1beta/models/{model}:generateContent
23
+ # The base URL here should be just the domain part.
24
+ @api_url || 'https://generativelanguage.googleapis.com'
25
+ end
26
+
27
+ # Lazily initializes and returns an instance of the GoogleClient.
28
+ # Passes itself (the config object) to the client's constructor.
29
+ # @return [ActiveGenie::Clients::GoogleClient] The client instance.
30
+ def client
31
+ @client ||= ::ActiveGenie::Clients::GoogleClient.new(self)
32
+ end
33
+
34
+ # Retrieves the model name designated for the lower tier (e.g., cost-effective, faster).
35
+ # Defaults to 'gemini-2.0-flash-lite'.
36
+ # @return [String] The lower tier model name.
37
+ def lower_tier_model
38
+ @lower_tier_model || 'gemini-2.0-flash-lite'
39
+ end
40
+
41
+ # Retrieves the model name designated for the middle tier (e.g., balanced performance).
42
+ # Defaults to 'gemini-2.0-flash'.
43
+ # @return [String] The middle tier model name.
44
+ def middle_tier_model
45
+ @middle_tier_model || 'gemini-2.0-flash'
46
+ end
47
+
48
+ # Retrieves the model name designated for the upper tier (e.g., most capable).
49
+ # Defaults to 'gemini-2.5-pro-experimental'.
50
+ # @return [String] The upper tier model name.
51
+ def upper_tier_model
52
+ @upper_tier_model || 'gemini-2.5-pro-experimental'
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../../clients/openai_client'
2
+ require_relative './base_config'
3
+
4
+ module ActiveGenie
5
+ module Configuration::Providers
6
+ # Configuration class for the OpenAI API client.
7
+ # Manages API keys, organization IDs, URLs, model selections, and client instantiation.
8
+ class OpenaiConfig < BaseConfig
9
+ NAME = :openai
10
+
11
+ # Retrieves the API key.
12
+ # Falls back to the OPENAI_API_KEY environment variable if not set.
13
+ # @return [String, nil] The API key.
14
+ def api_key
15
+ @api_key || ENV['OPENAI_API_KEY']
16
+ end
17
+
18
+ # Retrieves the base API URL for OpenAI API.
19
+ # Defaults to 'https://api.openai.com/v1'.
20
+ # @return [String] The API base URL.
21
+ def api_url
22
+ @api_url || 'https://api.openai.com/v1'
23
+ end
24
+
25
+ # Lazily initializes and returns an instance of the OpenaiClient.
26
+ # Passes itself (the config object) to the client's constructor.
27
+ # @return [ActiveGenie::Clients::OpenaiClient] The client instance.
28
+ def client
29
+ @client ||= ::ActiveGenie::Clients::OpenaiClient.new(self)
30
+ end
31
+
32
+ # Retrieves the model name designated for the lower tier (e.g., cost-effective, faster).
33
+ # Defaults to 'gpt-4o-mini'.
34
+ # @return [String] The lower tier model name.
35
+ def lower_tier_model
36
+ @lower_tier_model || 'gpt-4o-mini'
37
+ end
38
+
39
+ # Retrieves the model name designated for the middle tier (e.g., balanced performance).
40
+ # Defaults to 'gpt-4o'.
41
+ # @return [String] The middle tier model name.
42
+ def middle_tier_model
43
+ @middle_tier_model || 'gpt-4o'
44
+ end
45
+
46
+ # Retrieves the model name designated for the upper tier (e.g., most capable).
47
+ # Defaults to 'o1-preview'.
48
+ # @return [String] The upper tier model name.
49
+ def upper_tier_model
50
+ @upper_tier_model || 'o1-preview'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -5,8 +5,9 @@ module ActiveGenie::Configuration
5
5
  @default = nil
6
6
  end
7
7
 
8
- def register(name, provider_class)
8
+ def register(provider_class)
9
9
  @all ||= {}
10
+ name = provider_class::NAME
10
11
  @all[name] = provider_class.new
11
12
  define_singleton_method(name) do
12
13
  instance_variable_get("@#{name}") || instance_variable_set("@#{name}", @all[name])
@@ -16,11 +17,12 @@ module ActiveGenie::Configuration
16
17
  end
17
18
 
18
19
  def default
19
- @default || @all.values.first
20
+ @default || @all.values.find { |p| p.api_key }.class::NAME
20
21
  end
21
22
 
22
- def all
23
- @all
23
+ def valid
24
+ valid_provider_keys = @all.keys.select { |k| @all[k].valid? }
25
+ @all.slice(*valid_provider_keys)
24
26
  end
25
27
 
26
28
  def to_h(config = {})
@@ -32,6 +34,7 @@ module ActiveGenie::Configuration
32
34
  end
33
35
 
34
36
  private
37
+
35
38
  attr_writer :default
36
39
  end
37
40
  end
@@ -0,0 +1,35 @@
1
+ module ActiveGenie::Configuration
2
+ class RuntimeConfig
3
+ attr_writer :max_tokens, :temperature, :model, :provider, :api_key, :max_retries
4
+
5
+ def max_tokens
6
+ @max_tokens ||= 4096
7
+ end
8
+
9
+ def temperature
10
+ @temperature ||= 0.1
11
+ end
12
+
13
+ def model
14
+ @model
15
+ end
16
+
17
+ def provider
18
+ @provider ||= ActiveGenie.configuration.providers.default
19
+ end
20
+
21
+ def api_key
22
+ @api_key
23
+ end
24
+
25
+ def max_retries
26
+ @max_retries ||= 3
27
+ end
28
+
29
+ def to_h(config = {})
30
+ {
31
+ max_tokens:, temperature:, model:, provider:, api_key:, max_retries:,
32
+ }.merge(config)
33
+ end
34
+ end
35
+ end