|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../common/options" |
| 4 | + |
| 5 | +module ActiveAgent |
| 6 | + module GenerationProvider |
| 7 | + module OpenAI |
| 8 | + class Options < Common::Options |
| 9 | + # Client Options |
| 10 | + attribute :access_token, :string |
| 11 | + attribute :uri_base, :string |
| 12 | + attribute :request_timeout, :integer |
| 13 | + attribute :organization_id, :string |
| 14 | + attribute :admin_token, :string |
| 15 | + |
| 16 | + # Prompting Options |
| 17 | + attribute :model, :string, default: "gpt-4o-mini" |
| 18 | + attribute :temperature, :float, default: 0.7 |
| 19 | + attribute :stream, :boolean, default: false |
| 20 | + |
| 21 | + validates :model, presence: true |
| 22 | + validates :temperature, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 2 }, allow_nil: true |
| 23 | + validates :access_token, presence: true, if: :require_access_token? |
| 24 | + |
| 25 | + # Backwards Compatibility |
| 26 | + alias_attribute :host, :uri_base |
| 27 | + alias_attribute :api_key, :access_token |
| 28 | + alias_attribute :model_name, :model |
| 29 | + |
| 30 | + # Initialize from a hash (settings) with fallback to environment variables and OpenAI gem configuration |
| 31 | + def initialize(**settings) |
| 32 | + settings = settings.deep_symbolize_keys if settings.respond_to?(:deep_symbolize_keys) |
| 33 | + |
| 34 | + super(**deep_compact(settings.except(:default_url_options).merge( |
| 35 | + access_token: settings[:access_token] || resolve_access_token(settings), |
| 36 | + organization_id: settings[:organization_id] || resolve_organization_id(settings), |
| 37 | + admin_token: settings[:admin_token] || resolve_admin_token(settings), |
| 38 | + ))) |
| 39 | + end |
| 40 | + |
| 41 | + # Returns a hash suitable for OpenAI::Client initialization |
| 42 | + def client_options |
| 43 | + deep_compact( |
| 44 | + access_token:, |
| 45 | + uri_base:, |
| 46 | + organization_id:, |
| 47 | + extra_headers: client_options_extra_headers, |
| 48 | + log_errors: true |
| 49 | + ) |
| 50 | + end |
| 51 | + |
| 52 | + # Returns parameters for chat completion requests |
| 53 | + def chat_parameters |
| 54 | + deep_compact( |
| 55 | + model:, |
| 56 | + temperature: |
| 57 | + ) |
| 58 | + end |
| 59 | + |
| 60 | + # Convert to hash for compatibility with existing code |
| 61 | + def to_h |
| 62 | + deep_compact( |
| 63 | + "host" => host, |
| 64 | + "api_key" => api_key, |
| 65 | + "access_token" => access_token, |
| 66 | + "organization_id" => organization_id, |
| 67 | + "admin_token" => admin_token, |
| 68 | + "model" => model, |
| 69 | + "temperature" => temperature, |
| 70 | + "stream" => stream |
| 71 | + ) |
| 72 | + end |
| 73 | + |
| 74 | + alias_method :to_hash, :to_h |
| 75 | + |
| 76 | + protected |
| 77 | + |
| 78 | + def client_options_extra_headers = nil |
| 79 | + |
| 80 | + private |
| 81 | + |
| 82 | + def resolve_access_token(settings) |
| 83 | + settings[:api_key] || |
| 84 | + openai_settings_access_token || |
| 85 | + ENV["OPENAI_ACCESS_TOKEN"] |
| 86 | + end |
| 87 | + |
| 88 | + def resolve_organization_id(settings) |
| 89 | + openai_settings_organization_id || |
| 90 | + ENV["OPENAI_ORGANIZATION_ID"] |
| 91 | + end |
| 92 | + |
| 93 | + def resolve_admin_token(settings) |
| 94 | + openai_settings_admin_token || |
| 95 | + ENV["OPENAI_ADMIN_TOKEN"] |
| 96 | + end |
| 97 | + |
| 98 | + def openai_settings_access_token |
| 99 | + return nil unless defined?(::OpenAI) |
| 100 | + ::OpenAI.configuration.access_token |
| 101 | + rescue |
| 102 | + nil |
| 103 | + end |
| 104 | + |
| 105 | + def openai_settings_organization_id |
| 106 | + return nil unless defined?(::OpenAI) |
| 107 | + ::OpenAI.configuration.organization_id |
| 108 | + rescue |
| 109 | + nil |
| 110 | + end |
| 111 | + |
| 112 | + def openai_settings_admin_token |
| 113 | + return nil unless defined?(::OpenAI) |
| 114 | + ::OpenAI.configuration.admin_token |
| 115 | + rescue |
| 116 | + nil |
| 117 | + end |
| 118 | + |
| 119 | + # Only require access token if no other authentication method is available |
| 120 | + def require_access_token? |
| 121 | + resolved_access_token.blank? |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments