This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
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 import anthropic
# Create a table in Pixeltable and add a computed column that calls Anthropic
t = pxt.create_table('anthropic_demo.chat', {'input': pxt.String})
msgs = [{'role': 'user', 'content': t.input}]
t.add_computed_column(output=anthropic.messages(
messages=msgs,
model='claude-3-haiku-20240307',
max_tokens=300,
model_kwargs={
# Optional dict with parameters for the Anthropic API
'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.add_computed_column(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.
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.