PhoenixKitAI.OpenRouterClient (PhoenixKitAI v0.2.1)

Copy Markdown View Source

OpenRouter API client for PhoenixKit AI system.

Provides functions for interacting with the OpenRouter API, including:

  • API key validation
  • Model discovery (fetching available models)
  • Building request headers

OpenRouter API Reference

  • Base URL: https://openrouter.ai/api/v1
  • Authentication: Bearer token in Authorization header
  • Optional headers: HTTP-Referer, X-Title (for rankings)

Usage Examples

# Validate an API key
{:ok, %{credits: _}} = PhoenixKitAI.OpenRouterClient.validate_api_key("sk-or-v1-...")

# Fetch available models
{:ok, models} = PhoenixKitAI.OpenRouterClient.fetch_models("sk-or-v1-...")

Summary

Functions

Returns the base URL for OpenRouter API.

Builds HTTP headers for OpenRouter API requests.

Builds headers from an Account struct's settings.

Builds headers from an Endpoint struct's provider_settings.

Returns the date the built-in embedding model list was last refreshed.

Extracts the model name without provider prefix.

Extracts the provider name from a model ID.

Fetches embedding models from OpenRouter.

Fetches embedding models grouped by provider.

Fetches details for a specific model by ID.

Fetches available models from OpenRouter.

Fetches models by type and groups them by provider.

Fetches models and groups them by provider.

Gets the effective max tokens for a model.

Converts a provider slug to a human-friendly name.

Formats a model for display in a select dropdown.

Checks if a model supports a specific parameter.

Validates an OpenRouter API key by making a request to the /models endpoint.

Functions

base_url()

Returns the base URL for OpenRouter API.

build_headers(api_key, opts \\ [])

Builds HTTP headers for OpenRouter API requests.

Options

  • :http_referer - Site URL for rankings
  • :x_title - Site title for rankings
  • :include_usage - Include detailed usage/cost info (default: true for completions)

build_headers_from_account(map)

Builds headers from an Account struct's settings.

build_headers_from_endpoint(endpoint)

Builds headers from an Endpoint struct's provider_settings.

Resolves the API key by uuid lookup against PhoenixKit.Integrations, falling back to the legacy endpoint.api_key column when the integration row is missing or has no key.

embedding_models_last_updated()

Returns the date the built-in embedding model list was last refreshed.

extract_model_name(model_id)

Extracts the model name without provider prefix.

extract_provider(model_id)

Extracts the provider name from a model ID.

Examples

iex> extract_provider("anthropic/claude-3-opus")
"Anthropic"

iex> extract_provider("openai/gpt-4")
"OpenAI"

fetch_embedding_models(api_key, opts \\ [])

Fetches embedding models from OpenRouter.

Note: Embedding models are not returned by /models, so this function returns a curated list maintained in source (last refreshed 2026-03-24) merged with any user-contributed entries from config :phoenix_kit_ai, :embedding_models.

Returns {:ok, models} with a list of known embedding models.

fetch_embedding_models_grouped(api_key, opts \\ [])

Fetches embedding models grouped by provider.

fetch_model(api_key, model_id, opts \\ [])

Fetches details for a specific model by ID.

Returns {:ok, model} or {:error, reason}.

fetch_models(api_key, opts \\ [])

Fetches available models from OpenRouter.

Returns {:ok, models} where models is a list of model objects, or {:error, reason} on failure.

Options

  • :model_type - Filter by model type: :text, :vision, :image_gen, :all (default: :all)
  • :http_referer - Site URL for rankings
  • :x_title - Site title for rankings

Model Object Structure

Each model has:

  • id - Model identifier (e.g., "anthropic/claude-3-opus")
  • name - Display name
  • description - Model description
  • pricing - Pricing information (prompt/completion costs)
  • context_length - Maximum context window
  • architecture - Model architecture details

fetch_models_by_type(api_key, model_type, opts \\ [])

Fetches models by type and groups them by provider.

Model Types

  • :text - Text/chat completion models (text->text)
  • :vision - Vision/multimodal models (text+image->text)
  • :image_gen - Image generation models (text+image->text+image)
  • :all - All models without filtering

Examples

{:ok, grouped} = fetch_models_by_type(api_key, :vision)

fetch_models_grouped(api_key, opts \\ [])

Fetches models and groups them by provider.

Options

  • :model_type - Filter by model type: :text, :vision, :image_gen, :all (default: :text)

Returns a map where keys are provider names and values are lists of models.

get_model_max_tokens(model)

Gets the effective max tokens for a model.

Returns the model's max_completion_tokens if available, otherwise falls back to a percentage of context_length.

humanize_provider(provider)

Converts a provider slug to a human-friendly name.

OpenRouter model IDs use slugs like "arcee-ai/model-name". This function converts the slug portion to a human-readable provider name.

Examples

iex> humanize_provider("openai")
"OpenAI"

iex> humanize_provider("meta-llama")
"Meta Llama"

iex> humanize_provider("arcee-ai")
"Arcee AI"

model_option(model)

Formats a model for display in a select dropdown.

Returns {label, value} tuple.

model_supports_parameter?(model, param)

Checks if a model supports a specific parameter.

Examples

iex> model_supports_parameter?(model, "temperature")
true

iex> model_supports_parameter?(model, "tools")
false

validate_api_key(api_key)

Validates an OpenRouter API key by making a request to the /models endpoint.

Returns {:ok, %{valid: true}} on success, {:error, reason} on failure.