{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "983yjns496tx" }, "source": [ "# Working with Mistral AI in Pixeltable\n", "\n", "Pixeltable's Mistral AI integration enables you to access Mistral's LLM and other models via the Mistral AI API.\n", "\n", "### Prerequisites\n", "\n", "- A Mistral AI account with an API key (https://console.mistral.ai/api-keys/)\n", "\n", "### Important notes\n", "\n", "- Mistral AI usage may incur costs based on your Mistral AI plan.\n", "- Be mindful of sensitive data and consider security measures when integrating with external services.\n", "\n", "First you'll need to install required libraries and enter a Mistral AI API key." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9pckrD01ik-e", "outputId": "060b8b32-48a6-48a0-e720-4eacf94d83ef" }, "outputs": [], "source": [ "%pip install -qU pixeltable mistralai" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AQ6_Py7_7d0r", "outputId": "f82cfe36-be9e-4d43-f13e-9f6f5b680e8e" }, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if 'MISTRAL_API_KEY' not in os.environ:\n", " os.environ['MISTRAL_API_KEY'] = getpass.getpass('Mistral AI API Key:')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's create a Pixeltable directory to hold the tables for our demo." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "brtjK-88tTSS", "outputId": "55d08c91-438a-4c3e-c217-3cea72faca11" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata\n", "Created directory 'mistralai_demo'.\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pixeltable as pxt\n", "\n", "# Remove the 'mistralai_demo' directory and its contents, if it exists\n", "pxt.drop_dir('mistralai_demo', force=True)\n", "pxt.create_dir('mistralai_demo')" ] }, { "cell_type": "markdown", "metadata": { "id": "0kmjJoDq9Oqe" }, "source": [ "## Messages\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5ti10tXu5m3X", "outputId": "30848066-1e9b-4efd-aad7-b2271a031ec3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created table 'chat'.\n", "Added 0 column values with 0 errors in 0.01 s\n" ] }, { "data": { "text/plain": [ "No rows affected." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pixeltable.functions.mistralai import chat_completions\n", "\n", "# Create a table in Pixeltable and add a computed column that calls Mistral AI\n", "\n", "t = pxt.create_table('mistralai_demo/chat', {'input': pxt.String})\n", "\n", "messages = [{'role': 'user', 'content': t.input}]\n", "t.add_computed_column(\n", " output=chat_completions(\n", " messages=messages,\n", " model='mistral-small-latest',\n", " model_kwargs={\n", " # Optional dict with parameters for the Mistral API\n", " 'max_tokens': 300,\n", " 'top_p': 0.9,\n", " 'temperature': 0.7,\n", " },\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mePjoku95iUn", "outputId": "07c3f8f0-5301-44ba-ba33-25d10c3c26fa" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 0 column values with 0 errors in 0.01 s\n" ] }, { "data": { "text/plain": [ "No rows affected." ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Parse the response into a new column\n", "t.add_computed_column(response=t.output.choices[0].message.content)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 599 }, "id": "IkMM7OYb5rQ_", "outputId": "8e94af3e-485c-49f2-d7ba-b5490ec83af9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inserted 1 row with 0 errors in 2.31 s (0.43 rows/s)\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
inputresponse
What three species of fish have the highest mercury content?The three species of fish that typically have the highest mercury content are:\n", "\n", "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.\n", "\n", "2. **Swordfish** – These large, predatory fish consume smaller mercury-contaminated fish, leading to high mercury levels in their tissues.\n", "\n", "3. **King Mackerel** – Like other large, long-lived predatory fish, king mackerel accumulate significant mercury levels.\n", "\n", "Other high-mercury fish include:\n", "- **Tilefish** (especially from the Gulf of Mexico)\n", "- **Orange Roughy**\n", "- **Bigeye Tuna**\n", "\n", "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.
" ], "text/plain": [ " input \\\n", "0 What three species of fish have the highest me... \n", "\n", " response \n", "0 The three species of fish that typically have ... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Start a conversation\n", "t.insert(\n", " [\n", " {\n", " 'input': 'What three species of fish have the highest mercury content?'\n", " }\n", " ]\n", ")\n", "t.select(t.input, t.response).show()" ] }, { "cell_type": "markdown", "metadata": { "id": "lTtQcjKQAlis" }, "source": [ "### Learn more\n", "\n", "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) tutorial.\n", "\n", "If you have any questions, don't hesitate to reach out." ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "pxt", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.19" } }, "nbformat": 4, "nbformat_minor": 4 }