Module: OpenAI::Internal::ClientOptions Private

Defined in:
lib/openai/helpers/client_options.rb,
lib/openai/helpers/data_residency.rb,
sig/openai/helpers/client_options.rbs,
sig/openai/helpers/data_residency.rbs

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: ResolvedHeaders

Constant Summary collapse

DATA_RESIDENCY_URLS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Hash[String, String])
{
  "global" => "https://api.openai.com/v1",
  "us" => "https://us.api.openai.com/v1",
  "eu" => "https://eu.api.openai.com/v1",
  "ae" => "https://ae.api.openai.com/v1",
  "au" => "https://au.api.openai.com/v1",
  "in" => "https://in.api.openai.com/v1",
  "jp" => "https://jp.api.openai.com/v1"
}.freeze

Class Method Summary collapse

Class Method Details

.capture(**options) ⇒ ::Hash[Symbol, untyped]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • options (Object)

Returns:

  • (::Hash[Symbol, untyped])


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openai/helpers/client_options.rb', line 17

def capture(**options)
  options
    .transform_values do |value|
      case value
      when String
        value.dup.freeze
      when Hash
        value.transform_values { _1.is_a?(String) ? _1.dup.freeze : _1 }.freeze
      else
        value
      end
    end
    .freeze
end

.copy(defaults, overrides) ⇒ ::Hash[Symbol, untyped]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • defaults (::Hash[Symbol, untyped])
  • overrides (::Hash[Symbol, untyped])

Returns:

  • (::Hash[Symbol, untyped])


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/openai/helpers/client_options.rb', line 33

def copy(defaults, overrides)
  overrides = overrides.dup
  data_residency = overrides.delete(:data_residency)
  unless data_residency.nil?
    overrides[:base_url] = resolve_data_residency(
      data_residency,
      base_url: overrides.fetch(:base_url, OpenAI::Internal::OMIT),
      provider: overrides.fetch(:provider, defaults[:provider])
    )
  end

  unknown = overrides.keys - defaults.keys
  unless unknown.empty?
    raise ArgumentError, "Unknown client option#{unknown.one? ? "" : "s"}: #{unknown.join(", ")}"
  end

  options = defaults.dup
  provider_changed = overrides.key?(:provider) && !overrides[:provider].equal?(defaults[:provider])
  credentials_changed = [:api_key, :admin_api_key, :workload_identity].any? { overrides.key?(_1) }

  if provider_changed
    [:api_key, :admin_api_key, :workload_identity, :base_url, :organization, :project].each do |name|
      options[name] = nil
    end

    options[:default_headers] = {}
  elsif credentials_changed
    options[:default_headers] = options.fetch(:default_headers).reject do |name, _value|
      OpenAI::Internal::Logging.credential_header?(name)
    end
  end

  # An explicitly selected authentication method replaces the inherited
  # mutually exclusive method. Explicitly conflicting values still fail
  # the ordinary constructor validation.
  if overrides[:api_key] && !overrides.key?(:workload_identity)
    options[:workload_identity] = nil
  elsif overrides[:workload_identity] && !overrides.key?(:api_key)
    options[:api_key] = nil
  end

  headers = options.fetch(:default_headers)
  if overrides.key?(:default_headers)
    supplied = overrides[:default_headers]
    headers = supplied.nil? ? {} : OpenAI::Internal::Util.normalized_headers(headers, supplied)
  end

  options.merge!(overrides)
  options[:default_headers] = ResolvedHeaders.new.replace(headers)
  options
end

.resolve_data_residency(data_residency, base_url:, provider:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Object)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/openai/helpers/data_residency.rb', line 22

def resolve_data_residency(data_residency, base_url:, provider:)
  return base_url if data_residency.nil?

  unless base_url.equal?(OpenAI::Internal::OMIT)
    raise ArgumentError, "`data_residency` and `base_url` are mutually exclusive"
  end

  unless provider.nil?
    raise ArgumentError, "`data_residency` cannot be combined with `provider`"
  end

  url = DATA_RESIDENCY_URLS[data_residency.to_s] if data_residency.is_a?(String) || data_residency.is_a?(Symbol)
  return url unless url.nil?

  raise ArgumentError, "`data_residency` must be one of: #{DATA_RESIDENCY_URLS.keys.join(", ")}"
end