Class: OpenAI::Client
- Inherits:
-
Internal::Transport::BaseClient
- Object
- Internal::Transport::BaseClient
- OpenAI::Client
- Includes:
- Helpers::Realtime::ClientExtension
- Defined in:
- lib/openai/client.rb,
sig/openai/client.rbs,
sig/openai/helpers/realtime/extensions.rbs
Constant Summary collapse
- DEFAULT_MAX_RETRIES =
Default max number of retries to attempt after a failed retryable request.
2- DEFAULT_TIMEOUT_IN_SECONDS =
Default per-request timeout.
600.0- DEFAULT_INITIAL_RETRY_DELAY =
Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.
0.5- DEFAULT_MAX_RETRY_DELAY =
Default max retry delay in seconds.
8.0- WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER =
"workload-identity-auth"
Constants inherited from Internal::Transport::BaseClient
Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS
Instance Attribute Summary collapse
- #admin ⇒ OpenAI::Resources::Admin readonly
- #admin_api_key ⇒ String? readonly
- #api_key ⇒ String? readonly
- #audio ⇒ OpenAI::Resources::Audio readonly
-
#batches ⇒ OpenAI::Resources::Batches
readonly
Create large batches of API requests to run asynchronously.
- #beta ⇒ OpenAI::Resources::Beta readonly
- #chat ⇒ OpenAI::Resources::Chat readonly
-
#completions ⇒ OpenAI::Resources::Completions
readonly
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
- #containers ⇒ OpenAI::Resources::Containers readonly
- #content_provenance_checks ⇒ OpenAI::Resources::ContentProvenanceChecks readonly
-
#conversations ⇒ OpenAI::Resources::Conversations
readonly
Manage conversations and conversation items.
-
#embeddings ⇒ OpenAI::Resources::Embeddings
readonly
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
-
#evals ⇒ OpenAI::Resources::Evals
readonly
Manage and run evals in the OpenAI platform.
-
#files ⇒ OpenAI::Resources::Files
readonly
Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
- #fine_tuning ⇒ OpenAI::Resources::FineTuning readonly
- #graders ⇒ OpenAI::Resources::Graders readonly
-
#images ⇒ OpenAI::Resources::Images
readonly
Given a prompt and/or an input image, the model will generate a new image.
-
#models ⇒ OpenAI::Resources::Models
readonly
List and describe the various models available in the API.
-
#moderations ⇒ OpenAI::Resources::Moderations
readonly
Given text and/or image inputs, classifies if those inputs are potentially harmful.
- #organization ⇒ String? readonly
- #project ⇒ String? readonly
- #realtime ⇒ OpenAI::Resources::Realtime readonly
- #responses ⇒ OpenAI::Resources::Responses readonly
- #skills ⇒ OpenAI::Resources::Skills readonly
-
#uploads ⇒ OpenAI::Resources::Uploads
readonly
Use Uploads to upload large files in multiple parts.
- #vector_stores ⇒ OpenAI::Resources::VectorStores readonly
- #videos ⇒ OpenAI::Resources::Videos readonly
- #webhook_secret ⇒ String? readonly
- #webhooks ⇒ OpenAI::Resources::Webhooks readonly
- #workload_identity_auth ⇒ OpenAI::Auth::WorkloadIdentityAuth? readonly private
Attributes inherited from Internal::Transport::BaseClient
#base_url, #headers, #idempotency_header, #initial_retry_delay, #log_level, #logger, #max_retries, #max_retry_delay, #on_retry, #requester, #timeout
Instance Method Summary collapse
- #adopt_workload_identity_auth!(authenticator) ⇒ Object private
-
#initialize(api_key: OpenAI::Internal::OMIT, admin_api_key: OpenAI::Internal::OMIT, workload_identity: nil, organization: OpenAI::Internal::OMIT, project: OpenAI::Internal::OMIT, webhook_secret: OpenAI::Internal::OMIT, provider: nil, base_url: OpenAI::Internal::OMIT, data_residency: nil, default_headers: nil, max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, http_client: nil, logger: nil, log_level: nil, on_retry: nil) ⇒ Client
constructor
Creates and returns a new client for interacting with the API.
- #realtime_connection_request ⇒ Object private
-
#with_options(**overrides) ⇒ self
Returns a new client of the same class with the supplied constructor options overridden.
- #with_realtime_connection_request ⇒ Object private
Methods inherited from Internal::Transport::BaseClient
follow_redirect, #inspect, reap_connection!, #request, request_body_replayable?, should_retry?, validate!
Methods included from Internal::Util::SorbetRuntimeSupport
#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type
Constructor Details
#initialize(api_key: OpenAI::Internal::OMIT, admin_api_key: OpenAI::Internal::OMIT, workload_identity: nil, organization: OpenAI::Internal::OMIT, project: OpenAI::Internal::OMIT, webhook_secret: OpenAI::Internal::OMIT, provider: nil, base_url: OpenAI::Internal::OMIT, data_residency: nil, default_headers: nil, max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, http_client: nil, logger: nil, log_level: nil, on_retry: nil) ⇒ Client
Creates and returns a new client for interacting with the API.
"https://api.example.com/v2/". Defaults to ENV["OPENAI_BASE_URL"]
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 |
# File 'lib/openai/client.rb', line 582 def initialize( api_key: OpenAI::Internal::OMIT, admin_api_key: OpenAI::Internal::OMIT, workload_identity: nil, organization: OpenAI::Internal::OMIT, project: OpenAI::Internal::OMIT, webhook_secret: OpenAI::Internal::OMIT, provider: nil, base_url: OpenAI::Internal::OMIT, data_residency: nil, default_headers: nil, max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, http_client: nil, logger: nil, log_level: nil, on_retry: nil ) if Object.instance_method(:is_a?).bind_call(workload_identity, OpenAI::Auth::X509WorkloadIdentity) && !x509_identity?(workload_identity) raise ArgumentError, "X509WorkloadIdentity subclasses are not supported" end x509_identity = x509_identity?(workload_identity) if x509_identity && (configured_transport = workload_identity.transport) if !http_client.nil? && !http_client.equal?(configured_transport) raise ArgumentError, "X.509 workload identity must use its configured X.509 transport" end http_client = configured_transport end if x509_identity && !x509_transport?(http_client) raise ArgumentError, "X.509 workload identity requires an attested X509Transport" end base_url = OpenAI::Internal::ClientOptions.resolve_data_residency( data_residency, base_url: base_url, provider: provider ) if x509_transport?(http_client) && !data_residency.nil? unless http_client.supports_data_residency?(data_residency) raise ArgumentError, "X.509 data residency must match its attested OpenAI mTLS API origin" end base_url = "#{http_client.api_origin}/v1" end provider_runtime = nil unless provider.nil? provider_name = OpenAI::Internal::Provider.name(provider) conflicts = { api_key: api_key, admin_api_key: admin_api_key, workload_identity: workload_identity, base_url: base_url }.filter_map do |name, value| name unless value.equal?(OpenAI::Internal::OMIT) || value.nil? end unless conflicts.empty? formatted = conflicts.map { "`#{_1}`" }.join(", ") = "`provider` cannot be combined with top-level #{formatted}. Move provider " \ "authentication and routing options into `#{provider_name}(...)`." raise ArgumentError, end provider_runtime = OpenAI::Internal::Provider.configure(provider) end api_key = ENV["OPENAI_API_KEY"] if api_key.equal?(OpenAI::Internal::OMIT) && provider_runtime.nil? if admin_api_key.equal?(OpenAI::Internal::OMIT) && provider_runtime.nil? admin_api_key = ENV["OPENAI_ADMIN_KEY"] end if organization.equal?(OpenAI::Internal::OMIT) && provider_runtime.nil? organization = ENV["OPENAI_ORG_ID"] end project = ENV["OPENAI_PROJECT_ID"] if project.equal?(OpenAI::Internal::OMIT) && provider_runtime.nil? webhook_secret = ENV["OPENAI_WEBHOOK_SECRET"] if webhook_secret.equal?(OpenAI::Internal::OMIT) base_url = ENV["OPENAI_BASE_URL"] if base_url.equal?(OpenAI::Internal::OMIT) && provider_runtime.nil? api_key = nil if api_key.equal?(OpenAI::Internal::OMIT) admin_api_key = nil if admin_api_key.equal?(OpenAI::Internal::OMIT) organization = nil if organization.equal?(OpenAI::Internal::OMIT) project = nil if project.equal?(OpenAI::Internal::OMIT) webhook_secret = nil if webhook_secret.equal?(OpenAI::Internal::OMIT) base_url = provider_runtime.base_url if provider_runtime base_url = nil if base_url.equal?(OpenAI::Internal::OMIT) base_url ||= if x509_transport?(http_client) "#{http_client.api_origin}/v1" else "https://api.openai.com/v1" end if x509_identity configured_uri = URI(base_url.to_s) unless configured_uri.is_a?(URI::HTTPS) && configured_uri.userinfo.nil? && configured_uri.port == URI::HTTPS::DEFAULT_PORT && OpenAI::Internal::Util.uri_origin(configured_uri).casecmp?(http_client.api_origin) raise ArgumentError, "X.509 workload identity requires its attested OpenAI mTLS API origin" end end if !api_key.nil? && !workload_identity.nil? raise ArgumentError, "`api_key` and `workload_identity` are mutually exclusive" end if provider_runtime.nil? && api_key.nil? && admin_api_key.nil? && workload_identity.nil? raise( ArgumentError, "Missing credentials. Please pass an `api_key`, `workload_identity`, `admin_api_key`, or set the `OPENAI_API_KEY` or `OPENAI_ADMIN_KEY` environment variable." ) end headers = { "openai-organization" => (@organization = organization&.to_s), "openai-project" => (@project = project&.to_s) } parsed = {} resolved_headers = default_headers.is_a?(OpenAI::Internal::ClientOptions::ResolvedHeaders) custom_headers_env = ENV["OPENAI_CUSTOM_HEADERS"] unless provider_runtime || resolved_headers unless custom_headers_env.nil? custom_headers_env.split("\n").each do |line| colon = line.index(":") unless colon.nil? parsed[line[0...colon].strip] = line[(colon + 1)..].strip end end end client_headers = OpenAI::Internal::Util.normalized_headers(default_headers.to_h) unless provider_runtime.nil? provider_runtime.authentication_headers.each { client_headers.delete(_1) } end headers = OpenAI::Internal::Util.normalized_headers(parsed, headers, client_headers) if workload_identity.nil? @api_key = api_key&.to_s @workload_identity_auth = nil else @api_key = WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER token_exchange = if x509_identity OpenAI::Auth::X509TokenExchange.new(workload_identity, transport: http_client) end @workload_identity_auth = OpenAI::Auth::WorkloadIdentityAuth.new( workload_identity, organization&.to_s, token_exchange_url: if x509_identity "#{OpenAI::Auth::X509Transport::ISSUER_ORIGIN}/oauth/token" else OpenAI::Auth::WorkloadIdentityAuth::DEFAULT_TOKEN_EXCHANGE_URL end, token_exchange: token_exchange ) end @admin_api_key = admin_api_key&.to_s @webhook_secret = webhook_secret&.to_s @provider_runtime = provider_runtime super( base_url: base_url, timeout: timeout, max_retries: max_retries, initial_retry_delay: initial_retry_delay, max_retry_delay: max_retry_delay, headers: headers, http_client: http_client, logger: logger, log_level: log_level, on_retry: on_retry ) @copy_options = OpenAI::Internal::ClientOptions.capture( api_key: api_key, admin_api_key: @admin_api_key, workload_identity: workload_identity, organization: @organization, project: @project, webhook_secret: @webhook_secret, provider: provider, base_url: provider.nil? ? self.base_url.to_s : nil, default_headers: OpenAI::Internal::Util.normalized_headers(parsed, client_headers), max_retries: self.max_retries(), timeout: self.timeout(), initial_retry_delay: self.initial_retry_delay(), max_retry_delay: self.max_retry_delay(), http_client: requester, logger: self.logger(), log_level: self.log_level(), on_retry: self.on_retry() ) @completions = OpenAI::Resources::Completions.new(client: self) @chat = OpenAI::Resources::Chat.new(client: self) @embeddings = OpenAI::Resources::Embeddings.new(client: self) @files = OpenAI::Resources::Files.new(client: self) @images = OpenAI::Resources::Images.new(client: self) @content_provenance_checks = OpenAI::Resources::ContentProvenanceChecks.new(client: self) @audio = OpenAI::Resources::Audio.new(client: self) @moderations = OpenAI::Resources::Moderations.new(client: self) @models = OpenAI::Resources::Models.new(client: self) @fine_tuning = OpenAI::Resources::FineTuning.new(client: self) @graders = OpenAI::Resources::Graders.new(client: self) @vector_stores = OpenAI::Resources::VectorStores.new(client: self) @webhooks = OpenAI::Resources::Webhooks.new(client: self) @beta = OpenAI::Resources::Beta.new(client: self) @batches = OpenAI::Resources::Batches.new(client: self) @uploads = OpenAI::Resources::Uploads.new(client: self) @admin = OpenAI::Resources::Admin.new(client: self) @responses = OpenAI::Resources::Responses.new(client: self) @realtime = OpenAI::Resources::Realtime.new(client: self) @conversations = OpenAI::Resources::Conversations.new(client: self) @evals = OpenAI::Resources::Evals.new(client: self) @containers = OpenAI::Resources::Containers.new(client: self) @skills = OpenAI::Resources::Skills.new(client: self) @videos = OpenAI::Resources::Videos.new(client: self) end |
Instance Attribute Details
#admin ⇒ OpenAI::Resources::Admin (readonly)
102 103 104 |
# File 'lib/openai/client.rb', line 102 def admin @admin end |
#admin_api_key ⇒ String? (readonly)
26 27 28 |
# File 'lib/openai/client.rb', line 26 def admin_api_key @admin_api_key end |
#api_key ⇒ String? (readonly)
23 24 25 |
# File 'lib/openai/client.rb', line 23 def api_key @api_key end |
#audio ⇒ OpenAI::Resources::Audio (readonly)
67 68 69 |
# File 'lib/openai/client.rb', line 67 def audio @audio end |
#batches ⇒ OpenAI::Resources::Batches (readonly)
Create large batches of API requests to run asynchronously.
95 96 97 |
# File 'lib/openai/client.rb', line 95 def batches @batches end |
#beta ⇒ OpenAI::Resources::Beta (readonly)
91 92 93 |
# File 'lib/openai/client.rb', line 91 def beta @beta end |
#chat ⇒ OpenAI::Resources::Chat (readonly)
47 48 49 |
# File 'lib/openai/client.rb', line 47 def chat @chat end |
#completions ⇒ OpenAI::Resources::Completions (readonly)
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
44 45 46 |
# File 'lib/openai/client.rb', line 44 def completions @completions end |
#containers ⇒ OpenAI::Resources::Containers (readonly)
119 120 121 |
# File 'lib/openai/client.rb', line 119 def containers @containers end |
#content_provenance_checks ⇒ OpenAI::Resources::ContentProvenanceChecks (readonly)
64 65 66 |
# File 'lib/openai/client.rb', line 64 def content_provenance_checks @content_provenance_checks end |
#conversations ⇒ OpenAI::Resources::Conversations (readonly)
Manage conversations and conversation items.
112 113 114 |
# File 'lib/openai/client.rb', line 112 def conversations @conversations end |
#embeddings ⇒ OpenAI::Resources::Embeddings (readonly)
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
52 53 54 |
# File 'lib/openai/client.rb', line 52 def @embeddings end |
#evals ⇒ OpenAI::Resources::Evals (readonly)
Manage and run evals in the OpenAI platform.
116 117 118 |
# File 'lib/openai/client.rb', line 116 def evals @evals end |
#files ⇒ OpenAI::Resources::Files (readonly)
Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
57 58 59 |
# File 'lib/openai/client.rb', line 57 def files @files end |
#fine_tuning ⇒ OpenAI::Resources::FineTuning (readonly)
79 80 81 |
# File 'lib/openai/client.rb', line 79 def fine_tuning @fine_tuning end |
#graders ⇒ OpenAI::Resources::Graders (readonly)
82 83 84 |
# File 'lib/openai/client.rb', line 82 def graders @graders end |
#images ⇒ OpenAI::Resources::Images (readonly)
Given a prompt and/or an input image, the model will generate a new image.
61 62 63 |
# File 'lib/openai/client.rb', line 61 def images @images end |
#models ⇒ OpenAI::Resources::Models (readonly)
List and describe the various models available in the API.
76 77 78 |
# File 'lib/openai/client.rb', line 76 def models @models end |
#moderations ⇒ OpenAI::Resources::Moderations (readonly)
Given text and/or image inputs, classifies if those inputs are potentially harmful.
72 73 74 |
# File 'lib/openai/client.rb', line 72 def moderations @moderations end |
#organization ⇒ String? (readonly)
29 30 31 |
# File 'lib/openai/client.rb', line 29 def organization @organization end |
#project ⇒ String? (readonly)
32 33 34 |
# File 'lib/openai/client.rb', line 32 def project @project end |
#realtime ⇒ OpenAI::Resources::Realtime (readonly)
108 109 110 |
# File 'lib/openai/client.rb', line 108 def realtime @realtime end |
#responses ⇒ OpenAI::Resources::Responses (readonly)
105 106 107 |
# File 'lib/openai/client.rb', line 105 def responses @responses end |
#skills ⇒ OpenAI::Resources::Skills (readonly)
122 123 124 |
# File 'lib/openai/client.rb', line 122 def skills @skills end |
#uploads ⇒ OpenAI::Resources::Uploads (readonly)
Use Uploads to upload large files in multiple parts.
99 100 101 |
# File 'lib/openai/client.rb', line 99 def uploads @uploads end |
#vector_stores ⇒ OpenAI::Resources::VectorStores (readonly)
85 86 87 |
# File 'lib/openai/client.rb', line 85 def vector_stores @vector_stores end |
#videos ⇒ OpenAI::Resources::Videos (readonly)
125 126 127 |
# File 'lib/openai/client.rb', line 125 def videos @videos end |
#webhook_secret ⇒ String? (readonly)
35 36 37 |
# File 'lib/openai/client.rb', line 35 def webhook_secret @webhook_secret end |
#webhooks ⇒ OpenAI::Resources::Webhooks (readonly)
88 89 90 |
# File 'lib/openai/client.rb', line 88 def webhooks @webhooks end |
#workload_identity_auth ⇒ OpenAI::Auth::WorkloadIdentityAuth? (readonly)
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.
39 40 41 |
# File 'lib/openai/client.rb', line 39 def workload_identity_auth @workload_identity_auth end |
Instance Method Details
#adopt_workload_identity_auth!(authenticator) ⇒ 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.
522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/openai/client.rb', line 522 def adopt_workload_identity_auth!(authenticator) identity = @copy_options.fetch(:workload_identity) unless x509_identity?(identity) && x509_transport?(@requester) && OpenAI::Auth::X509Transport.exact_instance?(authenticator, OpenAI::Auth::WorkloadIdentityAuth) && authenticator.bound_to?(identity, transport: @requester) raise ArgumentError, "X.509 authenticator must match its workload identity and attested transport" end @workload_identity_auth = authenticator end |
#realtime_connection_request ⇒ 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.
4 |
# File 'sig/openai/helpers/realtime/extensions.rbs', line 4
def realtime_connection_request: (
|
#with_options(**overrides) ⇒ self
Returns a new client of the same class with the supplied constructor options overridden. Other settings and the HTTP transport are inherited. The original client is unchanged. Header overrides are merged; nil removes an individual header, and default_headers: nil clears all custom default headers. Subclasses with incompatible constructors or a private .new must override this method if they need to support copying.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/openai/client.rb', line 472 def (**overrides) if overrides.key?(:workload_identity) && !overrides.key?(:http_client) selected_identity = overrides.fetch(:workload_identity) if x509_identity?(selected_identity) && (configured_transport = selected_identity.transport) overrides = overrides.merge(http_client: configured_transport) end end previous_transport = @copy_options.fetch(:http_client) transport = overrides.fetch(:http_client, previous_transport) if overrides[:data_residency] && x509_transport?(transport) residency = overrides.fetch(:data_residency) = OpenAI::Internal::ClientOptions.copy(@copy_options, overrides.except(:data_residency)) .delete(:base_url) unless overrides.key?(:base_url) return copy_with_workload_identity_auth(.merge(data_residency: residency)) end = OpenAI::Internal::ClientOptions.copy(@copy_options, overrides) adopted_identity = x509_identity?(.fetch(:workload_identity)) && !x509_identity?(@copy_options.fetch(:workload_identity)) previous_origin = previous_transport.api_origin if x509_transport?(previous_transport) selected_origin = transport.api_origin if x509_transport?(transport) if adopted_identity && selected_origin inherited_origin = OpenAI::Internal::Util.uri_origin(URI(.fetch(:base_url).to_s)) adopted_identity = !inherited_origin.casecmp?(selected_origin) end if (adopted_identity || previous_origin != selected_origin) && !overrides.key?(:base_url) && !overrides[:data_residency] .delete(:base_url) end copy_with_workload_identity_auth() end |
#with_realtime_connection_request ⇒ 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.
12 |
# File 'sig/openai/helpers/realtime/extensions.rbs', line 12
def with_realtime_connection_request: (
|