Class: OpenRouter::Client
- Inherits:
-
Object
- Object
- OpenRouter::Client
- Includes:
- HTTP
- Defined in:
- lib/open_router/client.rb
Instance Method Summary collapse
-
#complete(messages, model: "openrouter/auto", providers: [], transforms: [], extras: {}, stream: nil) ⇒ Hash
Performs a chat completion request to the OpenRouter API.
-
#initialize(access_token: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) {|OpenRouter.configuration| ... } ⇒ Client
constructor
Initializes the client with optional configurations.
-
#models ⇒ Array<Hash>
Fetches the list of available models from the OpenRouter API.
-
#query_generation_stats(generation_id) ⇒ Hash
Queries the generation stats for a given id.
Methods included from HTTP
#delete, #get, #multipart_post, #post
Constructor Details
#initialize(access_token: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) {|OpenRouter.configuration| ... } ⇒ Client
Initializes the client with optional configurations.
15 16 17 18 19 20 21 |
# File 'lib/open_router/client.rb', line 15 def initialize(access_token: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) OpenRouter.configuration.access_token = access_token if access_token OpenRouter.configuration.request_timeout = request_timeout if request_timeout OpenRouter.configuration.uri_base = uri_base if uri_base OpenRouter.configuration.extra_headers = extra_headers if extra_headers.any? yield(OpenRouter.configuration) if block_given? end |
Instance Method Details
#complete(messages, model: "openrouter/auto", providers: [], transforms: [], extras: {}, stream: nil) ⇒ Hash
Performs a chat completion request to the OpenRouter API.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/open_router/client.rb', line 31 def complete(, model: "openrouter/auto", providers: [], transforms: [], extras: {}, stream: nil) parameters = { messages: } if model.is_a?(String) parameters[:model] = model elsif model.is_a?(Array) parameters[:models] = model parameters[:route] = "fallback" end parameters[:provider] = { provider: { order: providers } } if providers.any? parameters[:transforms] = transforms if transforms.any? parameters[:stream] = stream if stream parameters.merge!(extras) post(path: "/chat/completions", parameters:).tap do |response| raise ServerError, response.dig("error", "message") if response.presence&.dig("error", "message").present? raise ServerError, "Empty response from OpenRouter. Might be worth retrying once or twice." if stream.blank? && response.blank? return response.with_indifferent_access if response.is_a?(Hash) end end |
#models ⇒ Array<Hash>
Fetches the list of available models from the OpenRouter API.
54 55 56 |
# File 'lib/open_router/client.rb', line 54 def models get(path: "/models")["data"] end |
#query_generation_stats(generation_id) ⇒ Hash
Queries the generation stats for a given id.
61 62 63 64 |
# File 'lib/open_router/client.rb', line 61 def query_generation_stats(generation_id) response = get(path: "/generation?id=#{generation_id}") response["data"] end |