> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pixeltable.com/llms.txt
> Use this file to discover all available pages before exploring further.

# openrouter

> <a href="https://github.com/pixeltable/pixeltable/blob/main/pixeltable/functions/openrouter.py#L0" id="viewSource" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/View%20Source%20on%20Github-blue?logo=github&labelColor=gray" alt="View Source on GitHub" style={{ display: 'inline', margin: '0px' }} noZoom /></a>

# <span style={{ 'color': 'gray' }}>module</span>  pixeltable.functions.openrouter

Pixeltable UDFs that wrap the OpenRouter API.

OpenRouter provides a unified interface to multiple LLM providers. In order to use it,
you must first sign up at [https://openrouter.ai](https://openrouter.ai), create an API key, and configure it
as described in the Working with OpenRouter tutorial.

## <span style={{ 'color': 'gray' }}>udf</span>  chat\_completions()

```python Signature theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
@pxt.udf
chat_completions(
    messages: pxt.Json[(Json, ...)],
    *,
    model: pxt.String,
    model_kwargs: pxt.Json | None = None,
    tools: pxt.Json[(Json, ...)] | None = None,
    tool_choice: pxt.Json | None = None,
    provider: pxt.Json | None = None,
    transforms: pxt.Json[(String, ...)] | None = None
) -> pxt.Json
```

Chat Completion API via OpenRouter.

OpenRouter provides access to multiple LLM providers through a unified API.
For additional details, see: [https://openrouter.ai/docs](https://openrouter.ai/docs)

Supported models can be found at: [https://openrouter.ai/models](https://openrouter.ai/models)

Request throttling:
Applies the rate limit set in the config (section `openrouter`, key `rate_limit`). If no rate
limit is configured, uses a default of 600 RPM.

**Requirements:**

* `pip install openai`

**Parameters:**

* **`messages`** (`pxt.Json[(Json`): A list of messages comprising the conversation so far.
* **`model`** (`Any`): ID of the model to use (e.g., `'anthropic/claude-3.5-sonnet'`, `'openai/gpt-4'`).
* **`model_kwargs`** (`Any`): Additional OpenAI-compatible parameters.
* **`tools`** (`Any`): List of tools available to the model.
* **`tool_choice`** (`Any`): Controls which (if any) tool is called by the model.
* **`provider`** (`Any`): OpenRouter-specific provider preferences (e.g., `{'order': ['Anthropic', 'OpenAI']}`).
* **`transforms`** (`Any`): List of message transforms to apply (e.g., `['middle-out']`).

**Returns:**

* `pxt.Json`: A dictionary containing the response in OpenAI format.

**Examples:**

Basic chat completion:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
messages = [{'role': 'user', 'content': tbl.prompt}]
tbl.add_computed_column(
    response=chat_completions(
        messages, model='anthropic/claude-3.5-sonnet'
    )
)
```

With provider routing:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
tbl.add_computed_column(
    response=chat_completions(
        messages,
        model='anthropic/claude-3.5-sonnet',
        provider={'require_parameters': True, 'order': ['Anthropic']},
    )
)
```

With transforms:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
tbl.add_computed_column(
    response=chat_completions(
        messages,
        model='openai/gpt-4',
        transforms=['middle-out'],  # Optimize for long contexts
    )
)
```
