Kaggle Colab Download Notebook

Working with Anthropic in Pixeltable

Pixeltable's Anthropic integration enables you to access Anthropic's Claude LLM via the Anthropic API.

Prerequisites

Important Notes

  • Anthropic usage may incur costs based on your Anthropic 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 an Anthropic API key.

%pip install -qU pixeltable anthropic
import os
import getpass

if 'ANTHROPIC_API_KEY' not in os.environ:
    os.environ['ANTHROPIC_API_KEY'] = getpass.getpass('Anthropic API Key:')

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

import pixeltable as pxt

# Remove the `anthropic_demo` directory and its contents, if it exists
pxt.drop_dir('anthropic_demo', force=True)
pxt.create_dir('anthropic_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata
Created directory `anthropic_demo`.

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

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

from pixeltable.functions.anthropic import messages

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

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

msgs = [{'role': 'user', 'content': t.input}]
t['output'] = messages(
    messages=msgs,
    model='claude-3-haiku-20240307',
    # These parameters are optional and can be used to tune model behavior:
    max_tokens=300,
    system='Respond to the prompt with detailed historical information.',
    top_k=40,
    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.content[0].text
Added 0 column values with 0 errors.
# Start a conversation
t.insert(input="What was the outcome of the 1904 US Presidential election?")
t.select(t.input, t.response).show()
Computing cells: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 3/3 [00:01<00:00,  1.54 cells/s]
Inserting rows into `chat`: 1 rows [00:00, 149.28 rows/s]
Computing cells: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 3/3 [00:01<00:00,  1.53 cells/s]
Inserted 1 row with 0 errors.

input response
What was the outcome of the 1904 US Presidential election? The outcome of the 1904 US presidential election was:
  • The Republican candidate, Theodore Roosevelt, won the election. Roosevelt had become president in 1901 after the assassination of President William McKinley, and this was his first election as the Republican nominee.

  • Roosevelt defeated the Democratic candidate, Alton B. Parker, by a wide margin, winning 56.4% of the popular vote and 336 electoral votes compared to Parker's 37.6% of the popular vote and 140 electoral votes.

  • Roosev ...... cies and his aggressive foreign policy, known as "Big Stick" diplomacy.

  • The election was also notable for the fact that it was the first time a sitting president (Roosevelt) was elected in his own right after originally assuming the presidency due to the death of his predecessor.

So in summary, Theodore Roosevelt won a decisive victory over Alton B. Parker, cementing his status as a powerful and popular president during the Progressive Era of American politics in the early 20th century.

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.