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 Replicate integration enables you to access Replicate’s
models via the Replicate API.
Prerequisites
A Replicate account with an API token.
Important notes
Replicate usage may incur costs based on your Replicate 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 a Replicate
API token.
%pip install -qU pixeltable replicate
import getpassimport osif 'REPLICATE_API_TOKEN' not in os.environ: os.environ['REPLICATE_API_TOKEN'] = getpass.getpass( 'Replicate API Token:' )
Now let’s create a Pixeltable directory to hold the tables for our demo.
import pixeltable as pxt# Remove the `replicate_demo` directory and its contents, if it existspxt.drop_dir('replicate_demo', force=True)pxt.create_dir('replicate_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata
Created directory ‘replicate_demo’.
<pixeltable.catalog.dir.Dir at 0x177e18550>
Chat completions
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
Replicate.
from pixeltable.functions.replicate import run# Create a table in Pixeltable and pick a model hosted on Replicate with some parameterst = pxt.create_table('replicate_demo/chat', {'prompt': pxt.String})input = { 'system_prompt': 'You are a helpful assistant.', 'prompt': t.prompt, # These parameters are optional and can be used to tune model behavior: 'max_tokens': 300, 'top_p': 0.9, 'temperature': 0.8,}t.add_computed_column( output=run(input, ref='meta/meta-llama-3-8b-instruct'))
Created table ‘chat’.
Added 0 column values with 0 errors in 0.01 s
No rows affected.
# Parse the response into a new columnt.add_computed_column(response=pxt.functions.string.join('', t.output))
Added 0 column values with 0 errors in 0.02 s
No rows affected.
# Start a conversationt.insert([{'prompt': 'What foods are rich in selenium?'}])t.select(t.prompt, t.response).show()
Inserted 1 row with 0 errors in 4.45 s (0.22 rows/s)
Image generation
Here’s an example that shows how to use Replicate’s image generation
models with Pixeltable. We’ll use the FLUX Schnell model.
Created table ‘images’.
Added 0 column values with 0 errors in 0.01 s
No rows affected.
t.insert( [ { 'prompt': 'Draw a pencil sketch of a friendly dinosaur playing tennis in a cornfield.' } ])
Inserted 1 row with 0 errors in 0.99 s (1.01 rows/s)
1 row inserted.
t.select(t.prompt, t.output).collect()
We see that Replicate returns our image as an array containing a single
URL. To turn it into an actual image, we cast the string to type
pxt.Image in a new computed column: