OpenRouter completion client for making AI API calls.
This module handles the actual HTTP requests to OpenRouter's chat completions
and other endpoints. It's used internally by PhoenixKit.Modules.AI public functions.
Supported Endpoints
/chat/completions- Text and vision completions/embeddings- Text embeddings/images/generations- Image generation (planned)
Summary
Functions
Makes a chat completion request to OpenRouter.
Makes an embeddings request to OpenRouter.
Extracts the text content from a chat completion response.
Extracts usage information from a response.
Functions
Makes a chat completion request to OpenRouter.
Parameters
endpoint- The AI endpoint struct with API key and modelmessages- List of message maps with:roleand:contentopts- Additional options (temperature, max_tokens, etc.)
Options
:temperature- Sampling temperature (0-2):max_tokens- Maximum tokens in response:top_p- Nucleus sampling parameter:top_k- Top-k sampling parameter:frequency_penalty- Frequency penalty (-2 to 2):presence_penalty- Presence penalty (-2 to 2):repetition_penalty- Repetition penalty (0 to 2):stop- Stop sequences (list of strings):seed- Random seed for reproducibility:stream- Enable streaming (default: false)
Returns
{:ok, response}- Successful response with completion{:error, reason}- Error with reason string
Response Structure
%{
"id" => "gen-...",
"model" => "anthropic/claude-3-haiku",
"choices" => [
%{
"message" => %{
"role" => "assistant",
"content" => "Hello! How can I help you today?"
},
"finish_reason" => "stop"
}
],
"usage" => %{
"prompt_tokens" => 10,
"completion_tokens" => 15,
"total_tokens" => 25
}
}
Makes an embeddings request to OpenRouter.
Parameters
endpoint- The AI endpoint struct with API key and modelinput- Text or list of texts to embedopts- Additional options
Options
:dimensions- Output dimensions (model-specific)
Returns
{:ok, response}- Response with embeddings{:error, reason}- Error with reason
Extracts the text content from a chat completion response.
Extracts usage information from a response.
Returns a map with token counts and cost (if available from OpenRouter). Cost is stored in nanodollars (1/1,000,000 of a dollar) to preserve precision for cheap API calls. Stored in the cost_cents field for backward compatibility.