The documentation at https://github.com/openai/openai-ruby is rather poor - it doesn't mention base_url
or choices
at all! Further, LLMs trained so far are really bad at writing code to use it. So this gist is an attempt at correcting the record for this specific use case.
Last active
June 6, 2025 20:03
-
-
Save peterc/dc2a39ae4330d0352fda8b23dbd683a6 to your computer and use it in GitHub Desktop.
How to call an OpenRouter LLM with the official Ruby OpenAI gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "openai" | |
client = OpenAI::Client.new( | |
api_key: ENV.fetch("OPENROUTER_API_KEY"), | |
base_url: "https://openrouter.ai/api/v1" | |
) | |
models = %w{ | |
google/gemma-2b-it | |
anthropic/claude-sonnet-4 | |
openai/o4-mini | |
openai/gpt-4.1-mini | |
google/gemini-2.0-flash-001 | |
} | |
models.each do |model| | |
resp = client.chat.completions.create( | |
model: model, | |
messages: [{ role: "user", content: "Tell me a joke" }] | |
) | |
puts "=== #{model} ===\n\n#{resp.choices[0].message.content}\n\n" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "openai" | |
client = OpenAI::Client.new( | |
api_key: ENV.fetch("OPENROUTER_API_KEY"), | |
base_url: "https://openrouter.ai/api/v1" | |
) | |
resp = client.chat.completions.create( | |
model: :"openai/gpt-4o", | |
messages: [{ role: "user", content: "Tell me a joke" }] | |
) | |
puts resp.choices[0].message.content |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LLM | |
def initialize | |
@client = OpenAI::Client.new( | |
api_key: ENV.fetch("OPENROUTER_API_KEY"), | |
base_url: "https://openrouter.ai/api/v1" | |
) | |
end | |
def chat(prompt, history: []) | |
messages = history.map { |h| { role: h[:role], content: h[:content] } } | |
messages << { role: "user", content: prompt } | |
resp = @client.chat.completions.create( | |
model: "amazon/nova-micro-v1", | |
messages: messages | |
) | |
resp.choices[0].message.content | |
end | |
end | |
llm = LLM.new | |
puts llm.chat("What is the capital of France?") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment