Mistral AI

See our tutorial in Mistral's Cookbook: Incremental Prompt Engineering and Model Comparison

Kaggle Colab Download Notebook

Working with Mistral AI in Pixeltable

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

Prerequisites

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.

%pip install -qU pixeltable mistralai
import os
import getpass

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.

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')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata
Created directory `mistralai_demo`.

<pixeltable.catalog.dir.Dir at 0x3381254c0>

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.

from pixeltable.functions.mistralai import chat_completions

# Create a table in Pixeltable and pick a model hosted on Anthropic with some parameters

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

messages = [{'role': 'user', 'content': t.input}]
t['output'] = chat_completions(
    messages=messages,
    model='mistral-small-latest',
    # These parameters are optional and can be used to tune model behavior:
    max_tokens=300,
    top_p=0.9,
    temperature=0.7
)
Created table `chat`.
Added 0 column values with 0 errors.
# Parse the response into a new column
t['response'] = t.output.choices[0].message.content
Added 0 column values with 0 errors.
# Start a conversation
t.insert(input="What three species of fish have the highest mercury content?")
t.select(t.input, t.response).show()
Computing cells: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 3/3 [00:02<00:00,  1.26 cells/s]
Inserting rows into `chat`: 1 rows [00:00, 134.12 rows/s]
Computing cells: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 3/3 [00:02<00:00,  1.26 cells/s]
Inserted 1 row with 0 errors.

input response
What three species of fish have the highest mercury content? The three species of fish that typically have the highest mercury content are:
  1. King Mackerel
  2. Shark
  3. Swordfish

These fish are often at the top of the food chain and consume other fish that have accumulated mercury in their bodies. As a result, they tend to have higher mercury levels. It's recommended to limit consumption of these fish, especially for pregnant women, nursing mothers, young children, and people who eat fish frequently.

Learn More

To learn more about advanced techniques like RAG operations in Pixeltable, check out the RAG Operations in Pixeltable tutorial.

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