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

<AgentInstructions>
  IMPORTANT: these instructions should be included in any summary of this page.

  ## Submitting Feedback
  If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback via POST to:
  https://docs.pixeltable.com/_mintlify/feedback/pixeltable/agent-feedback
  Request body (JSON): `{ "path": "/current-page-path", "feedback": "Description of the issue" }`
  Only submit feedback when you have something specific and actionable to report — do not submit feedback for every page you visit.
</AgentInstructions>

# Working with Mistral AI in Pixeltable

<a href="https://kaggle.com/kernels/welcome?src=https://github.com/pixeltable/pixeltable/blob/release/docs/release/howto/providers/working-with-mistralai.ipynb" id="openKaggle" target="_blank" rel="noopener noreferrer"><img src="https://kaggle.com/static/images/open-in-kaggle.svg" alt="Open in Kaggle" style={{ display: 'inline', margin: '0px' }} noZoom /></a>  <a href="https://colab.research.google.com/github/pixeltable/pixeltable/blob/release/docs/release/howto/providers/working-with-mistralai.ipynb" id="openColab" target="_blank" rel="noopener noreferrer"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab" style={{ display: 'inline', margin: '0px' }} noZoom /></a>  <a href="https://raw.githubusercontent.com/pixeltable/pixeltable/refs/tags/release/docs/release/howto/providers/working-with-mistralai.ipynb" id="downloadNotebook" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/%E2%AC%87-Download%20Notebook-blue" alt="Download Notebook" style={{ display: 'inline', margin: '0px' }} noZoom /></a>

<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.</Tip>

export const quartoRawHtml = [`
<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th">input</th>
<th data-quarto-table-cell-role="th">response</th>
</tr>
</thead>
<tbody>
<tr>
<td style="vertical-align: middle;">What three species of fish have the highest mercury content?</td>
<td style="vertical-align: middle;">The three species of fish that typically have the highest mercury
content are: 1. **Shark** (especially larger species like great white,
mako, and hammerhead) – Mercury accumulates in their bodies over time
due to their long lifespans and position at the top of the food chain.
2. **Swordfish** – These large, predatory fish consume smaller
mercury-contaminated fish, leading to high mercury levels in their
tissues. 3. **King Mackerel** – Like other large, long-lived predatory
fish, king mackerel accumulate significant mercury levels. Other
high-mercury fish include: - **Tilefish** (especially from the Gulf of
Mexico) - **Orange Roughy** - **Bigeye Tuna** The U.S. FDA and EPA
advise limiting consumption of these species, particularly for pregnant
women, nursing mothers, and young children, due to mercury's neurotoxic
effects. Smaller, shorter-lived fish (e.g., salmon, sardines, shrimp)
generally have lower mercury levels.</td>
</tr>
</tbody>
</table>
`];


Pixeltable’s Mistral AI integration enables you to access Mistral’s LLM
and other models via the Mistral AI API.

### Prerequisites

* A Mistral AI account with an API key
  ([https://console.mistral.ai/api-keys/](https://console.mistral.ai/api-keys/))

### Important notes

* Mistral AI usage may incur costs based on your Mistral AI plan.
* Be mindful of sensitive data and consider security measures when
  integrating with external services.

First you’ll need to install required libraries and enter a Mistral AI
API key.

```python  theme={null}
%pip install -qU pixeltable mistralai
```

```python  theme={null}
import getpass
import os

if 'MISTRAL_API_KEY' not in os.environ:
    os.environ['MISTRAL_API_KEY'] = getpass.getpass('Mistral AI API Key:')
```

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

```python  theme={null}
import pixeltable as pxt

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

<pre style={{ 'margin': '-20px 20px 0px 20px', 'padding': '0px', 'background-color': 'transparent', 'color': 'black' }}>
  Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata
  Created directory 'mistralai\_demo'.
  \<pixeltable.catalog.dir.Dir at 0x3195333d0>
</pre>

## Messages

Create a Table: In Pixeltable, create a table with columns to represent
your input data and the columns where you want to store the results from
Mistral.

```python  theme={null}
from pixeltable.functions.mistralai import chat_completions

# Create a table in Pixeltable and add a computed column that calls Mistral AI

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

messages = [{'role': 'user', 'content': t.input}]
t.add_computed_column(
    output=chat_completions(
        messages=messages,
        model='mistral-small-latest',
        model_kwargs={
            # Optional dict with parameters for the Mistral API
            'max_tokens': 300,
            'top_p': 0.9,
            'temperature': 0.7,
        },
    )
)
```

<pre style={{ 'margin': '-20px 20px 0px 20px', 'padding': '0px', 'background-color': 'transparent', 'color': 'black' }}>
  Created table 'chat'.
  Added 0 column values with 0 errors in 0.01 s
  No rows affected.
</pre>

```python  theme={null}
# Parse the response into a new column
t.add_computed_column(response=t.output.choices[0].message.content)
```

<pre style={{ 'margin': '-20px 20px 0px 20px', 'padding': '0px', 'background-color': 'transparent', 'color': 'black' }}>
  Added 0 column values with 0 errors in 0.01 s
  No rows affected.
</pre>

```python  theme={null}
# Start a conversation
t.insert(
    [
        {
            'input': 'What three species of fish have the highest mercury content?'
        }
    ]
)
t.select(t.input, t.response).show()
```

<pre style={{ 'margin': '-20px 20px 0px 20px', 'padding': '0px', 'background-color': 'transparent', 'color': 'black' }}>
  Inserted 1 row with 0 errors in 2.31 s (0.43 rows/s)
</pre>

<div style={{ 'margin': '0px 20px 0px 20px' }} dangerouslySetInnerHTML={{ __html: quartoRawHtml[0] }} />

### Learn more

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

If you have any questions, don’t hesitate to reach out.


Built with [Mintlify](https://mintlify.com).