PhoenixKit.Modules.AI.Completion (phoenix_kit v1.7.71)

Copy Markdown View Source

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

chat_completion(endpoint, messages, opts \\ [])

Makes a chat completion request to OpenRouter.

Parameters

  • endpoint - The AI endpoint struct with API key and model
  • messages - List of message maps with :role and :content
  • opts - 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
  }
}

embeddings(endpoint, input, opts \\ [])

Makes an embeddings request to OpenRouter.

Parameters

  • endpoint - The AI endpoint struct with API key and model
  • input - Text or list of texts to embed
  • opts - Additional options

Options

  • :dimensions - Output dimensions (model-specific)

Returns

  • {:ok, response} - Response with embeddings
  • {:error, reason} - Error with reason

extract_content(response)

Extracts the text content from a chat completion response.

extract_usage(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.