Class: RubyLLM::Providers::OpenRouter

Inherits:
RubyLLM::Provider show all
Defined in:
lib/ruby_llm/providers/openrouter.rb,
lib/ruby_llm/providers/openrouter/chat.rb,
lib/ruby_llm/providers/openrouter/media.rb,
lib/ruby_llm/providers/openrouter/images.rb,
lib/ruby_llm/providers/openrouter/models.rb,
lib/ruby_llm/providers/openrouter/speech.rb,
lib/ruby_llm/providers/openrouter/videos.rb,
lib/ruby_llm/providers/openrouter/streaming.rb,
lib/ruby_llm/providers/openrouter/embeddings.rb

Overview

OpenRouter API integration.

Defined Under Namespace

Modules: Chat, Embeddings, Images, Media, Models, Speech, Streaming, Videos Classes: ChatCompletions

Constant Summary

Constants included from Support::Inspectable

Support::Inspectable::TRUNCATE_AT

Instance Attribute Summary

Attributes inherited from RubyLLM::Provider

#config, #connection

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RubyLLM::Provider

#animate_later, #assume_models_exist?, assume_models_exist?, #batch_cost, #batch_cost_amounts, #batch_cost_multiplier, #batch_protocol_name, #batch_results, #batch_status, #batches?, #cache_content, #cancel_batch, #capabilities, capabilities, #compact, #complete, #configuration_requirements, configured?, #configured?, configured_providers, configured_remote_providers, #count_tokens, #create_batch, #delete_cache, display_name, #download_file, #embed, #extend_cache, #files?, #find_batch, #find_cache, #find_file, #find_research_job, #initialize, #list_file_uris, #list_models, local?, #local?, local_providers, #long_context_pricing?, model_registry_files, model_required?, models_dev_alias, models_dev_model_id, #moderate, #name, #ocr, #paint, #preprocess_message, protocol, #protocol_for, protocols, #protocols, providers, register, remote?, remote_providers, #render, #render_embedding, #rerank, #research_later, resolve, resolve!, resolve_registry_id, #retry_delay, #slug, #speak, #tokenize, #tool_approval_response, #transcribe, #upload_file

Methods included from Support::Inspectable

#full_inspect, #inspect, #pretty_print

Constructor Details

This class inherits a constructor from RubyLLM::Provider

Class Method Details

.configuration_optionsObject



77
78
79
# File 'lib/ruby_llm/providers/openrouter.rb', line 77

def configuration_options
  %i[openrouter_api_key openrouter_api_base openrouter_app_url openrouter_app_name]
end

.configuration_requirementsObject



81
82
83
# File 'lib/ruby_llm/providers/openrouter.rb', line 81

def configuration_requirements
  %i[openrouter_api_key]
end

Instance Method Details

#api_baseObject



43
44
45
# File 'lib/ruby_llm/providers/openrouter.rb', line 43

def api_base
  @config.openrouter_api_base || 'https://openrouter.ai/api/v1'
end

#batch_api_baseObject



47
48
49
# File 'lib/ruby_llm/providers/openrouter.rb', line 47

def batch_api_base
  "#{api_base.sub(%r{/v1/?\z}, '')}/beta/batches"
end

#headersObject



51
52
53
54
55
56
57
# File 'lib/ruby_llm/providers/openrouter.rb', line 51

def headers
  {
    'Authorization' => "Bearer #{@config.openrouter_api_key}",
    'HTTP-Referer' => @config.openrouter_app_url || 'https://rubyllm.com',
    'X-OpenRouter-Title' => @config.openrouter_app_name || 'RubyLLM'
  }
end

#parse_error(response) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_llm/providers/openrouter.rb', line 59

def parse_error(response)
  body = parse_error_body(response)
  return unless body

  case body
  when Hash
    parse_error_part_message body
  when Array
    messages = body.filter_map do |part|
      parse_error_part_message part
    end.reject(&:empty?)
    messages.join('. ') unless messages.empty?
  else
    body
  end
end

#resolve_protocol(name, model, **request) ⇒ Object



37
38
39
40
41
# File 'lib/ruby_llm/providers/openrouter.rb', line 37

def resolve_protocol(name, model, **request)
  return fetch_protocol(:chat_completions) if !name && request[:operation]

  super
end