Module: OpenAI::Providers
- Defined in:
- lib/openai/providers/azure.rb,
lib/openai/providers/bedrock.rb,
sig/openai/providers.rbs
Defined Under Namespace
Class Method Summary collapse
-
.azure(endpoint: OpenAI::Internal::OMIT, api_key: OpenAI::Internal::OMIT, token_provider: nil) ⇒ OpenAI::Provider
Configure the standard OpenAI client for the Azure OpenAI v1 API.
-
.bedrock(region: nil, base_url: OpenAI::Internal::OMIT, api_key: OpenAI::Internal::OMIT, token_provider: nil, access_key_id: nil, secret_access_key: nil, session_token: nil, profile: nil, credentials_provider: nil) ⇒ OpenAI::Provider
Configure the standard OpenAI client for Amazon Bedrock Mantle.
Class Method Details
.azure(endpoint: OpenAI::Internal::OMIT, api_key: OpenAI::Internal::OMIT, token_provider: nil) ⇒ OpenAI::Provider
Configure the standard OpenAI client for the Azure OpenAI v1 API.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/openai/providers/azure.rb', line 158 def azure( endpoint: OpenAI::Internal::OMIT, api_key: OpenAI::Internal::OMIT, token_provider: nil ) configured_endpoint = if endpoint.equal?(OpenAI::Internal::OMIT) Azure.normalize_optional_string(ENV["AZURE_OPENAI_ENDPOINT"]) else normalized = Azure.normalize_optional_string(endpoint) if !endpoint.nil? && normalized.nil? raise ArgumentError, "The Azure OpenAI `endpoint` must not be empty." end normalized end raise ArgumentError, Azure::MISSING_ENDPOINT_MESSAGE if configured_endpoint.nil? base_url = Azure.normalize_endpoint(configured_endpoint) explicit_api_key = !api_key.equal?(OpenAI::Internal::OMIT) && !api_key.nil? normalized_api_key = Azure.normalize_optional_string(api_key) if explicit_api_key && normalized_api_key.nil? raise ArgumentError, "The Azure OpenAI API key must not be empty." end unless token_provider.nil? || token_provider.respond_to?(:call) raise ArgumentError, "The Azure OpenAI `token_provider` must respond to `call`." end if explicit_api_key && !token_provider.nil? raise ArgumentError, "The Azure OpenAI `api_key` and `token_provider` options are mutually exclusive." end environment_api_key = if api_key.equal?(OpenAI::Internal::OMIT) && token_provider.nil? Azure.normalize_optional_string(ENV["AZURE_OPENAI_API_KEY"]) end resolved_api_key = normalized_api_key || environment_api_key if resolved_api_key.nil? && token_provider.nil? raise ArgumentError, Azure::MISSING_CREDENTIALS_MESSAGE end if token_provider credential_provider = token_provider authentication = :bearer else resolved_api_key.freeze credential_provider = -> { resolved_api_key } authentication = :api_key end definition = Azure::Definition.new( base_url: base_url, credential_provider: credential_provider, authentication: authentication ) OpenAI::Internal::Provider.create(definition) end |
.bedrock(region: nil, base_url: OpenAI::Internal::OMIT, api_key: OpenAI::Internal::OMIT, token_provider: nil, access_key_id: nil, secret_access_key: nil, session_token: nil, profile: nil, credentials_provider: nil) ⇒ OpenAI::Provider
Configure the standard OpenAI client for Amazon Bedrock Mantle.
Authentication precedence is an explicit bearer or AWS mode, then AWS_BEARER_TOKEN_BEDROCK, then the standard AWS credential chain.
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/openai/providers/bedrock.rb', line 376 def bedrock( region: nil, base_url: OpenAI::Internal::OMIT, api_key: OpenAI::Internal::OMIT, token_provider: nil, access_key_id: nil, secret_access_key: nil, session_token: nil, profile: nil, credentials_provider: nil ) normalized_region = Bedrock.normalize_optional_string(region) if !region.nil? && normalized_region.nil? raise ArgumentError, "The Bedrock AWS `region` must not be empty." end normalized_region ||= Bedrock.normalize_optional_string(ENV["AWS_REGION"]) || Bedrock.normalize_optional_string(ENV["AWS_DEFAULT_REGION"]) normalized_profile = Bedrock.normalize_optional_string(profile) if !profile.nil? && normalized_profile.nil? raise ArgumentError, "The Bedrock AWS `profile` must not be empty." end configured_base_url = if base_url.equal?(OpenAI::Internal::OMIT) Bedrock.normalize_optional_string(ENV["AWS_BEDROCK_BASE_URL"]) elsif base_url.nil? nil else normalized = Bedrock.normalize_optional_string(base_url) raise(ArgumentError, "The Bedrock `base_url` must not be empty.") unless normalized normalized end configured_base_url = Bedrock.normalize_base_url(configured_base_url) if configured_base_url has_access_key = !access_key_id.nil? has_secret_key = !secret_access_key.nil? if has_access_key != has_secret_key || (!session_token.nil? && !has_access_key) raise ArgumentError, Bedrock::PARTIAL_STATIC_CREDENTIALS_MESSAGE end normalized_access_key_id = Bedrock.normalize_optional_string(access_key_id) normalized_secret_access_key = Bedrock.normalize_optional_string(secret_access_key) normalized_session_token = Bedrock.normalize_optional_string(session_token) if has_access_key && [normalized_access_key_id, normalized_secret_access_key].any?(&:nil?) raise ArgumentError, "Static AWS credentials require non-empty `access_key_id` and `secret_access_key` values." end if !session_token.nil? && normalized_session_token.nil? raise ArgumentError, "A static AWS `session_token` must not be empty when provided." end explicit_api_key = !api_key.equal?(OpenAI::Internal::OMIT) && !api_key.nil? normalized_api_key = Bedrock.normalize_optional_string(api_key) if explicit_api_key && normalized_api_key.nil? raise ArgumentError, "The Bedrock bearer credential must not be empty." end if explicit_api_key && !token_provider.nil? raise ArgumentError, "The `api_key` and `token_provider` options are mutually exclusive. Configure only one." end if !token_provider.nil? && !token_provider.respond_to?(:call) raise ArgumentError, "The Bedrock `token_provider` must respond to `call`." end if !credentials_provider.nil? && !credentials_provider.respond_to?(:call) && !credentials_provider.respond_to?(:credentials) raise ArgumentError, "The Bedrock `credentials_provider` must respond to `call` or `credentials`." end explicit_bearer = explicit_api_key || !token_provider.nil? aws_modes = [has_access_key, !normalized_profile.nil?, !credentials_provider.nil?].count(true) if aws_modes > 1 raise ArgumentError, Bedrock::AMBIGUOUS_AWS_AUTH_MESSAGE end if explicit_bearer && aws_modes.positive? raise ArgumentError, Bedrock::AMBIGUOUS_AUTH_MESSAGE end skip_environment_bearer = !api_key.equal?(OpenAI::Internal::OMIT) && api_key.nil? environment_bearer = !explicit_bearer && aws_modes.zero? && !skip_environment_bearer && !Bedrock.normalize_optional_string(ENV["AWS_BEARER_TOKEN_BEDROCK"]).nil? bearer_provider = if explicit_api_key normalized_api_key.freeze -> { normalized_api_key } elsif !token_provider.nil? token_provider elsif environment_bearer lambda do ENV["AWS_BEARER_TOKEN_BEDROCK"] || raise(OpenAI::Errors::Error, Bedrock::MISSING_CREDENTIALS_MESSAGE) end end if bearer_provider && configured_base_url.nil? && normalized_region.nil? raise ArgumentError, Bedrock::MISSING_REGION_MESSAGE end definition = Bedrock::Definition.new( region: normalized_region, base_url: configured_base_url, bearer_provider: bearer_provider, profile: normalized_profile, access_key_id: normalized_access_key_id, secret_access_key: normalized_secret_access_key, session_token: normalized_session_token, credentials_provider: credentials_provider, default_chain: aws_modes.zero? ) OpenAI::Internal::Provider.create(definition) end |