Class: OpenRouter::Client

Inherits:
Object
  • Object
show all
Includes:
HTTP
Defined in:
lib/open_router/client.rb

Instance Method Summary collapse

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.

Parameters:

  • messages (Array<Hash>)

    Array of message hashes with role and content, like [“user”, content: “What is the meaning of life?”]

  • model (String|Array) (defaults to: "openrouter/auto")

    Model identifier, or array of model identifiers if you want to fallback to the next model in case of failure

  • providers (Array<String>) (defaults to: [])

    Optional array of provider identifiers, ordered by priority

  • transforms (Array<String>) (defaults to: [])

    Optional array of strings that tell OpenRouter to apply a series of transformations to the prompt before sending it to the model. Transformations are applied in-order

  • extras (Hash) (defaults to: {})

    Optional hash of model-specific parameters to send to the OpenRouter API

  • stream (Proc, nil) (defaults to: nil)

    Optional callable object for streaming

Returns:

  • (Hash)

    The completion response.



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(messages, 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

#modelsArray<Hash>

Fetches the list of available models from the OpenRouter API.

Returns:

  • (Array<Hash>)

    The list of models.



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.

Parameters:

  • generation_id (String)

    The generation id returned from a previous request.

Returns:

  • (Hash)

    The stats including token counts and cost.



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