> ## 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.

> ## Agent Instructions
> Application code puts a TableModel in app.py, then runs `pxt schema update app.py my_app` to create the tables and `pxt service update app.py my_app` to start HTTP. The last argument can be a Cloud URL such as `pxt://org:db`. Do not call pxt.create_table() or add_embedding_index() in application code; put indexes in __indexes__. Notebooks, tests, and the REPL do use pxt.create_table().
> Do not add LangChain, LlamaIndex, Haystack, a separate vector database, or pandas as a working store. Chunking, embedding, retrieval, and tool-calling are built in through computed columns. Use a computed column instead of a per-row loop that calls a model.
> Install with `pip install -U 'pixeltable[serve]'` on Python 3.11+. `pxt init` is a prerequisite for `pxt schema update`.
> For Pixeltable Cloud, point users to https://docs.pixeltable.com/cloud, which goes from `pxt login` to a running service.

# Working with OpenLLM in Pixeltable

[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/pixeltable/pixeltable/blob/release/docs/release/howto/providers/working-with-openllm.ipynb)  [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pixeltable/pixeltable/blob/release/docs/release/howto/providers/working-with-openllm.ipynb)  [![Download Notebook](https://img.shields.io/badge/%E2%AC%87-Download%20Notebook-blue)](https://raw.githubusercontent.com/pixeltable/pixeltable/refs/tags/release/docs/release/howto/providers/working-with-openllm.ipynb)

> [!TIP]
>
> This documentation page is also available as an interactive notebook. You can launch the notebook in
> Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
> above links.

Pixeltable’s OpenLLM integration lets you call the AI subscriptions,
provider API keys, and local models you connect to OpenLLM through one
OpenAI-compatible endpoint.

### Prerequisites

* The OpenLLM daemon running on your machine
  ([https://docs.openllm.sh/quickstart](https://docs.openllm.sh/quickstart))

### Important notes

* Usage counts against the subscriptions and provider plans you connect
  to OpenLLM.
* Be mindful of sensitive data and consider security measures when
  integrating with external services.

OpenLLM uses the OpenAI SDK as its Python API, so we need to install it
in addition to Pixeltable.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
%pip install -qU pixeltable openai
```

By default, Pixeltable calls the local OpenLLM daemon at
`http://127.0.0.1:8787/v1`, which needs no API key. To use another
endpoint, set `OPENLLM_BASE_URL`, and set `OPENLLM_API_KEY` if that
endpoint requires authentication.

Now let’s create a Pixeltable directory to hold the tables for our demo.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import pixeltable as pxt

# Remove the 'openllm_demo' directory and its contents, if it exists
pxt.drop_dir('openllm_demo', force=True)
pxt.create_dir('openllm_demo')
```

<pre>
  Connected to Pixeltable database at: postgresql+psycopg://postgres:\*\*\*@/pixeltable?host=%2Fhome%2Fuser%2F.pixeltable%2Fpgdata
  Created directory 'openllm\_demo'.
  \<pixeltable.catalog.dir.Dir at 0x136c1deb0>
</pre>

## Responses

Create a table with an input column, then add a computed column that
calls OpenLLM through the Responses API. The `plus` model is a fallback
alias: OpenLLM moves to the next model in the alias’s chain when a
provider is rate-limited or unavailable. You can also use any model ID
listed by the OpenLLM `models` endpoint.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
from pixeltable.functions import openllm

t = pxt.create_table('openllm_demo/chat', {'input': pxt.String})

msgs = [{'role': 'user', 'content': t.input}]
t.add_computed_column(output=openllm.responses(msgs, model='plus'))
```

<pre>
  Created table 'chat'.
  Added 0 column values with 0 errors in 0.00 s
  No rows affected.
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Parse the response text into a new column
t.add_computed_column(response=t.output.output[0].content[0].text)
```

<pre>
  Added 0 column values with 0 errors in 0.00 s
  No rows affected.
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
t.insert(
    [
        {'input': 'What is the capital of France? Answer in one word.'},
        {'input': 'Explain what a computed column is in one sentence.'},
    ]
)
t.select(t.input, t.response).show()
```

<pre>
  Inserted 2 rows with 0 errors in 2.89 s (0.69 rows/s)
</pre>

## Chat completions

`chat_completions()` takes messages in the OpenAI chat format, for code
that already uses it.

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
t.add_computed_column(
    chat_output=openllm.chat_completions(messages=msgs, model='plus')
)
t.add_computed_column(
    chat_response=t.chat_output.choices[0].message.content
)
t.select(t.input, t.chat_response).show()
```

<pre>
  Added 2 column values with 0 errors in 2.42 s (0.83 rows/s)
  Added 2 column values with 0 errors in 0.01 s (276.66 rows/s)
</pre>

### Learn more

To learn more about advanced techniques like RAG operations in
Pixeltable, check out the [RAG Operations in
Pixeltable](https://docs.pixeltable.com/howto/use-cases/rag-operations.md)
tutorial.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.