Class: LumenLLM::Providers::OpenRouter::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lumen_llm/providers/open_router/client.rb

Constant Summary collapse

ENDPOINT =
"https://openrouter.ai/api/v1/chat/completions"

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, referer: nil, title: nil, transport: nil, open_timeout: nil, read_timeout: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
# File 'lib/lumen_llm/providers/open_router/client.rb', line 11

def initialize(api_key: nil, referer: nil, title: nil, transport: nil, open_timeout: nil, read_timeout: nil)
  config = LumenLLM.configuration
  @api_key = api_key || config.openrouter_api_key || ENV["OPENROUTER_API_KEY"]
  @referer = referer || config.openrouter_referer
  @title = title || config.openrouter_title
  @transport = transport || NetHTTPTransport.new
  @open_timeout = open_timeout || config.http_open_timeout
  @read_timeout = read_timeout || config.http_read_timeout
end

Instance Method Details

#chat(messages, model:) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lumen_llm/providers/open_router/client.rb', line 21

def chat(messages, model:)
  raise ConfigurationError, "OpenRouter API key is not configured" if blank?(@api_key)

  body = {
    :model => model,
    :messages => messages
  }

  response = @transport.request(
    URI.parse(ENDPOINT),
    headers,
    body.to_json,
    @open_timeout,
    @read_timeout
  )

  unless success?(response)
    raise ProviderError, "OpenRouter API Error: #{response.code} - #{response.body}"
  end

  JSON.parse(response.body)
end