{ "cells": [ { "cell_type": "markdown", "id": "3e777354", "metadata": {}, "source": [ "# Working with Groq in Pixeltable\n", "\n", "Pixeltable's Groq integration enables you to access Groq models via the Groq API.\n", "\n", "### Prerequisites\n", "\n", "- A Groq account with an API key (https://console.groq.com/docs/quickstart)\n", "\n", "### Important notes\n", "\n", "- Groq usage may incur costs based on your Groq plan.\n", "- Be mindful of sensitive data and consider security measures when integrating with external services." ] }, { "cell_type": "markdown", "id": "8b2e6912-e936-4c3a-84a2-ba99950c9493", "metadata": {}, "source": [ "First you'll need to install required libraries and enter your OpenAI API key." ] }, { "cell_type": "code", "execution_count": null, "id": "d5288926-c278-4cbc-815c-cbc0433bbf49", "metadata": {}, "outputs": [], "source": [ "%pip install -qU pixeltable groq" ] }, { "cell_type": "code", "execution_count": null, "id": "385f6831-f029-42bb-99f1-652a809ffc6e", "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if 'GROQ_API_KEY' not in os.environ:\n", " os.environ['GROQ_API_KEY'] = getpass.getpass(\n", " 'Enter your Groq API key:'\n", " )" ] }, { "cell_type": "markdown", "id": "8d3dd131-22de-496c-9f02-ffd4515c20d3", "metadata": {}, "source": [ "Now let's create a Pixeltable directory to hold the tables for our demo." ] }, { "cell_type": "code", "execution_count": 1, "id": "9bdc613f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata\n", "Created directory 'groq_demo'.\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pixeltable as pxt\n", "\n", "# Remove the 'groq_demo' directory and its contents, if it exists\n", "pxt.drop_dir('groq_demo', force=True)\n", "pxt.create_dir('groq_demo')" ] }, { "cell_type": "markdown", "id": "02f8595f-fb03-419f-9440-ee2ae784fd20", "metadata": {}, "source": [ "## Chat Completions\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 Groq." ] }, { "cell_type": "code", "execution_count": 2, "id": "342407c1", "metadata": {}, "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 import groq\n", "\n", "# Create a table in Pixeltable and add a computed column that calls OpenAI\n", "\n", "t = pxt.create_table('groq_demo/chat', {'input': pxt.String})\n", "\n", "messages = [{'role': 'user', 'content': t.input}]\n", "t.add_computed_column(\n", " output=groq.chat_completions(\n", " messages=messages,\n", " model='llama-3.3-70b-versatile',\n", " model_kwargs={\n", " # Optional dict with parameters for the Groq API\n", " 'max_tokens': 300,\n", " 'top_p': 0.9,\n", " 'temperature': 0.7,\n", " },\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "id": "c5f0b862", "metadata": {}, "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, "id": "15c9bc76-1b28-4d17-9a2d-339968f90786", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inserted 1 row with 0 errors in 1.16 s (0.86 rows/s)\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
inputresponse
How many islands are in the Aleutian island chain?The Aleutian Islands are a chain of approximately 300 small volcanic islands, stretching about 1,200 miles (1,900 km) westward from the Alaska Peninsula. However, not all of these islands are named or inhabited.\n", "\n", "According to the U.S. Geological Survey, there are around 14 main islands in the Aleutian Islands chain, along with many smaller islets and rocks. Some of the main islands include:\n", "\n", "1. Unimak Island\n", "2. Unalaska Island\n", "3. Umnak Island\n", "4. Adak Island\n", "5. Atka Island\n", "6. Amlia Island\n", "7. Tanaga Island\n", "8. Kanaga Island\n", "9. Adugak Island (also known as Adugak Island or "Four Mountains Islands")\n", "10. Great Sitkin Island\n", "11. Semisopochnoi Island\n", "12. Kiska Island\n", "13. Little Sitkin Island\n", "14. Attu Island\n", "\n", "Keep in mind that some sources may group the islands differently, and there may be variations in the number of islands depending on the definition of a "main island" versus a smaller islet or rock.
" ], "text/plain": [ " input \\\n", "0 How many islands are in the Aleutian island ch... \n", "\n", " response \n", "0 The Aleutian Islands are a chain of approximat... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Start a conversation\n", "t.insert(\n", " [{'input': 'How many islands are in the Aleutian island chain?'}]\n", ")\n", "t.select(t.input, t.response).head()" ] }, { "cell_type": "markdown", "id": "622c2abd-8709-452a-b773-18fb28d180ce", "metadata": {}, "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": { "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": 5 }