{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "983yjns496tx" }, "source": [ "# Working with Bedrock in Pixeltable\n", "\n", "Pixeltable's Bedrock integration enables you to access AWS Bedrock foundation models directly from your tables.\n", "\n", "### Prerequisites\n", "\n", "- Activate Bedrock in your AWS account.\n", "- Request access to your desired models (e.g. Claude Sonnet 3.7, Amazon Nova Pro).\n", "- Obtain a **Bedrock API Key** from the AWS console (under Bedrock > API keys), or configure standard AWS IAM credentials.\n", "\n", "### Important notes\n", "\n", "- Bedrock usage may incur costs based on your Bedrock 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 configure your Bedrock credentials." ] }, { "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 boto3" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if 'BEDROCK_API_KEY' not in os.environ:\n", " os.environ['BEDROCK_API_KEY'] = getpass.getpass(\n", " 'Enter your Bedrock API Key: '\n", " )\n", "\n", "# Optional: set the region if your Bedrock endpoint is not in us-east-1\n", "# os.environ['BEDROCK_REGION_NAME'] = 'us-west-2'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's create a Pixeltable directory to hold the tables for our demo." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "brtjK-88tTSS", "outputId": "55d08c91-438a-4c3e-c217-3cea72faca11" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created directory 'bedrock_demo'.\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pixeltable as pxt\n", "\n", "# Remove the `bedrock_demo` directory and its contents, if it exists\n", "pxt.drop_dir('bedrock_demo', force=True)\n", "pxt.create_dir('bedrock_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 Bedrock." ] }, { "cell_type": "code", "execution_count": 15, "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": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pixeltable.functions import bedrock\n", "\n", "# Create a table in Pixeltable and add a computed column that calls Bedrock\n", "\n", "t = pxt.create_table('bedrock_demo/chat', {'input': pxt.String})\n", "\n", "t.add_computed_column(\n", " output=bedrock.converse(\n", " model_id='amazon.nova-pro-v1:0',\n", " messages=[{'role': 'user', 'content': [{'text': t.input}]}],\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 16, "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": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Parse the response into a new column\n", "t.add_computed_column(response=t.output.output.message.content[0].text)" ] }, { "cell_type": "code", "execution_count": 17, "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.75 s (0.36 rows/s)\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
inputresponse
What was the outcome of the 1904 US Presidential election?The 1904 United States Presidential election was a contest between the incumbent President Theodore Roosevelt, representing the Republican Party, and Alton B. Parker, the candidate of the Democratic Party. The election took place on November 8, 1904.\n", "\n", "### Outcome:\n", "- **Theodore Roosevelt** won the election in a landslide victory.\n", "- **Electoral Votes:**\n", "  - Theodore Roosevelt (Republican): 336 electoral votes\n", "  - Alton B. Parker (Democratic): 140 electoral votes\n", "- **Popular Vote:**\n", "  - Theodor ...... mpaign emphasized a return to limited government and states' rights, which did not resonate as strongly with the electorate.\n", "\n", "4. **Third Parties:** Minor parties, including the Socialist Party with Eugene V. Debs as its candidate, had little impact on the overall outcome but indicated growing interest in alternative political movements.\n", "\n", "In summary, the 1904 election solidified Theodore Roosevelt’s position as a transformative leader and marked a significant victory for the Republican Party.
" ], "text/plain": [ " input \\\n", "0 What was the outcome of the 1904 US Presidenti... \n", "\n", " response \n", "0 The 1904 United States Presidential election w... " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Start a conversation\n", "t.insert(\n", " [\n", " {\n", " 'input': 'What was the outcome of the 1904 US Presidential election?'\n", " }\n", " ]\n", ")\n", "t.select(t.input, t.response).show()" ] }, { "cell_type": "markdown", "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." ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "pixeltable", "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.11.11" } }, "nbformat": 4, "nbformat_minor": 4 }