Skip to content

Commit 4e0c426

Browse files
committed
Add the ability to set OpenRouter headers
1 parent cb28d86 commit 4e0c426

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

docs/configuration.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ RubyLLM.configure do |config|
5454
config.openrouter_api_key = ENV.fetch('OPENROUTER_API_KEY', nil)
5555
config.ollama_api_base = ENV.fetch('OLLAMA_API_BASE', nil)
5656

57+
# --- Open Router Configuration ---
58+
config.openrouter_referer = 'https://rubyllm.com'
59+
config.openrouter_title = 'RubyLLM'
60+
5761
# --- AWS Bedrock Credentials ---
5862
# Uses standard AWS credential chain (environment, shared config, IAM role)
5963
# if these specific keys aren't set. Region is required if using Bedrock.
@@ -224,4 +228,3 @@ default_response = default_chat.ask("Query using global production settings...")
224228
* **Thread Safety:** Each context is independent, making them safe for use across different threads.
225229

226230
Contexts provide a clean and safe mechanism for managing diverse configuration needs within a single application.
227-

lib/ruby_llm/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Configuration
2323
:bedrock_region,
2424
:bedrock_session_token,
2525
:openrouter_api_key,
26+
:openrouter_referer,
27+
:openrouter_title,
2628
:ollama_api_base,
2729
# Default models
2830
:default_model,

lib/ruby_llm/providers/openrouter.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ def api_base(_config)
1414
'https://openrouter.ai/api/v1'
1515
end
1616

17+
# @see https://openrouter.ai/docs/api-reference/overview#headers
1718
def headers(config)
1819
{
19-
'Authorization' => "Bearer #{config.openrouter_api_key}"
20-
}
20+
'Authorization' => "Bearer #{config.openrouter_api_key}",
21+
'HTTP-Referer' => config.openrouter_referer, # Optional: Site URL for rankings on openrouter.ai.
22+
'X-Title' => config.openrouter_title # Optional: Site title for rankings on openrouter.ai.
23+
}.compact
2124
end
2225

2326
def slug

0 commit comments

Comments
 (0)