ExLLM.Providers.OpenRouter (ex_llm v0.8.1)
View SourceOpenRouter API adapter for ExLLM.
OpenRouter provides access to 300+ AI models through a unified API that's compatible with the OpenAI format. It offers intelligent routing, automatic fallbacks, and normalized responses across different AI providers.
Configuration
This adapter requires an OpenRouter API key and optionally app identification headers.
Using Environment Variables
# Set environment variables
export OPENROUTER_API_KEY="your-api-key"
export OPENROUTER_MODEL="openai/gpt-4o" # optional
export OPENROUTER_APP_NAME="MyApp" # optional
export OPENROUTER_APP_URL="https://myapp.com" # optional
# Use with default environment provider
ExLLM.Providers.OpenRouter.chat(messages, config_provider: ExLLM.Infrastructure.ConfigProvider.Env)
Using Static Configuration
config = %{
openrouter: %{
api_key: "your-api-key",
model: "openai/gpt-4o",
app_name: "MyApp",
app_url: "https://myapp.com",
base_url: "https://openrouter.ai/api/v1" # optional
}
}
{:ok, provider} = ExLLM.Infrastructure.ConfigProvider.Static.start_link(config)
ExLLM.Providers.OpenRouter.chat(messages, config_provider: provider)
Example Usage
messages = [
%{role: "user", content: "Hello, how are you?"}
]
# Simple chat
{:ok, response} = ExLLM.Providers.OpenRouter.chat(messages)
IO.puts(response.content)
# With specific model
{:ok, response} = ExLLM.Providers.OpenRouter.chat(messages, model: "anthropic/claude-3-5-sonnet")
# Streaming chat
{:ok, stream} = ExLLM.Providers.OpenRouter.stream_chat(messages)
for chunk <- stream do
if chunk.content, do: IO.write(chunk.content)
end
OpenRouter Features
- Model Routing: Access 300+ models from providers like OpenAI, Anthropic, Google, Meta, etc.
- Automatic Fallbacks: Intelligent routing if primary model is unavailable
- Unified API: OpenAI-compatible format for all models
- Streaming: Real-time responses for all supported models
- Function Calling: Tool/function calling support where available
- Multimodal: Image and PDF input support for compatible models
Summary
Functions
Generate embeddings for text through OpenRouter.