Class: Ace::LLM::Organisms::OpenRouterClient

Inherits:
BaseClient
  • Object
show all
Includes:
Molecules::OpenAICompatibleParams
Defined in:
lib/ace/llm/organisms/openrouter_client.rb

Overview

OpenRouterClient handles interactions with OpenRouter’s API OpenRouter provides unified access to 400+ models through OpenAI-compatible API

Constant Summary collapse

API_BASE_URL =
"https://openrouter.ai/api/v1"
DEFAULT_MODEL =
"openai/gpt-oss-120b:nitro"
GENERATION_KEYS =

Generation parameters to include in API request

%i[temperature max_tokens top_p frequency_penalty presence_penalty].freeze

Constants inherited from BaseClient

BaseClient::DEFAULT_GENERATION_CONFIG, BaseClient::DEFAULT_SYSTEM_PROMPT_SEPARATOR

Instance Attribute Summary

Attributes inherited from BaseClient

#api_key, #base_url, #generation_config, #http_client, #model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Molecules::OpenAICompatibleParams

#extract_openai_compatible_options

Methods inherited from BaseClient

#needs_credentials?, #provider_name

Constructor Details

#initialize(api_key: nil, model: nil, referer: nil, title: nil, **options) ⇒ OpenRouterClient

Initialize the client

Parameters:

  • api_key (String, nil) (defaults to: nil)

    API key (uses env if nil)

  • model (String, nil) (defaults to: nil)

    Model name (uses default if nil)

  • referer (String, nil) (defaults to: nil)

    HTTP-Referer header for app attribution (should not contain sensitive information as it’s sent with each request)

  • title (String, nil) (defaults to: nil)

    X-Title header for app attribution (should not contain sensitive information as it’s sent with each request)

  • options (Hash)

    Additional options passed to base client



34
35
36
37
38
39
# File 'lib/ace/llm/organisms/openrouter_client.rb', line 34

def initialize(api_key: nil, model: nil, referer: nil, title: nil, **options)
  # Store attribution headers separately for explicit dependencies
  @referer = referer
  @title = title
  super(api_key: api_key, model: model, **options)
end

Class Method Details

.provider_nameString

Get the provider name

Returns:

  • (String)

    Provider name



22
23
24
# File 'lib/ace/llm/organisms/openrouter_client.rb', line 22

def self.provider_name
  "openrouter"
end

Instance Method Details

#generate(messages, **options) ⇒ Hash

Generate a response from OpenRouter

Parameters:

  • messages (Array<Hash>, String)

    Messages or prompt

  • options (Hash)

    Generation options

Returns:

  • (Hash)

    Response with text and metadata



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ace/llm/organisms/openrouter_client.rb', line 45

def generate(messages, **options)
  messages_array = build_messages(messages)
  generation_params = extract_generation_options(options)

  request_body = build_request_body(messages_array, generation_params)
  response = make_api_request(request_body)

  parse_response(response)
rescue => e
  # Intentionally catch StandardError to wrap all API/network errors
  # as ProviderError for consistent error handling upstream
  handle_api_error(e)
end