class OpenAI < Interface
- Inherits from
Interface
Forwards OpenAI-compatible HTTP requests to the OpenAI API.
Definitions
def initialize(api_key: ENV["OPENAI_API_KEY"], endpoint: DEFAULT_ENDPOINT, client: nil, organization: nil, project: nil, **client_options)
Initialize a provider for OpenAI or another compatible endpoint.
Signature
-
option
:api_keyString | Nil The bearer token for the upstream service.
-
option
:endpointString | Async::HTTP::Endpoint The upstream HTTP endpoint.
-
option
:clientInterface(:call) | Nil An optional externally managed HTTP client.
-
option
:organizationString | Nil An optional OpenAI organization ID.
-
option
:projectString | Nil An optional OpenAI project ID.
Implementation
def initialize(api_key: ENV["OPENAI_API_KEY"], endpoint: DEFAULT_ENDPOINT, client: nil, organization: nil, project: nil, **client_options)
@endpoint = Async::HTTP::Endpoint[endpoint]
@api_key = api_key
@organization = organization
@project = project
@client = client || Async::HTTP::Client.new(@endpoint, **client_options)
@owns_client = client.nil?
end
def models
Fetch the upstream model-list response. Fetch the upstream OpenAI-compatible model list as a raw HTTP response.
Signature
-
returns
Protocol::HTTP::Response The raw model-list response.
Implementation
def models
self.call(Protocol::HTTP::Request["GET", "/v1/models"])
end
def call(request)
Forward a request without transforming its body.
Signature
-
parameter
requestProtocol::HTTP::Request The request to send upstream.
-
returns
Protocol::HTTP::Response The unmodified upstream response.
Implementation
def call(request)
headers = forwarded_headers(request.headers)
headers["authorization"] = "Bearer #{@api_key}" if @api_key
headers["openai-organization"] = @organization if @organization
headers["openai-project"] = @project if @project
upstream_request = Protocol::HTTP::Request.new(
@endpoint.scheme,
@endpoint.authority,
request.method,
request.path,
request.version,
headers,
request.body,
request.protocol,
request.interim_response,
)
return @client.call(upstream_request)
end
def close
Close the HTTP client when this provider created it.
Implementation
def close
@client.close if @owns_client
end