Skip to main content

Prerequisites

Important Notes

  • Together.ai usage may incur costs based on your Together.ai 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 your Together API key.
%pip install -qU pixeltable together
import os
import getpass

if 'TOGETHER_API_KEY' not in os.environ:
    os.environ['TOGETHER_API_KEY'] = getpass.getpass('Together API Key: ')
Now let’s create a Pixeltable directory to hold the tables for our demo.
import pixeltable as pxt

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

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 OpenAI.
from pixeltable.functions import together

chat_t = pxt.create_table('together_demo.chat', {'input': pxt.String})

messages = [{'role': 'user', 'content': chat_t.input}]

chat_t.add_computed_column(output=together.chat_completions(
    messages=messages,
    model='meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
    model_kwargs={
        # Optional dict with parameters for the Together API
        'max_tokens': 300,
        'stop': ['\n'],
        'temperature': 0.7,
        'top_p': 0.9,
    }
))
chat_t.add_computed_column(response=chat_t.output.choices[0].message.content)
Created table `chat`.
Added 0 column values with 0 errors.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
# Start a conversation
chat_t.insert([
    {'input': 'How many species of felids have been classified?'},
    {'input': 'Can you make me a coffee?'}
])
chat_t.select(chat_t.input, chat_t.response).head()
Inserting rows into `chat`: 2 rows [00:00, 221.12 rows/s]
Inserted 2 rows with 0 errors.
input response
Can you make me a coffee? I'm not capable of physically making you a coffee, but I can guide you through the process or provide you with a virtual coffee recipe if you'd like.
How many species of felids have been classified? There are approximately 40 species of felids that have been classified, but this number may vary depending on the source and the taxonomy used. Some sources may group certain species together, while others may recognize them as separate species.

Embeddings

emb_t = pxt.create_table('together_demo.embeddings', {'input': pxt.String})
emb_t.add_computed_column(embedding=together.embeddings(
    input=emb_t.input,
    model='BAAI/bge-base-en-v1.5'
))
Created table `embeddings`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
emb_t.insert([{'input': 'Together AI provides a variety of embeddings models.'}])
Inserting rows into `embeddings`: 1 rows [00:00, 135.03 rows/s]
Inserted 1 row with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=2, num_excs=0, updated_cols=[], cols_with_excs=[])
emb_t.head()
input embedding
Together AI provides a variety of embeddings models. [ 0.016 -0.21 0.201 0.153 -0.339 0.164 ... -0.049 0.003 -0.157 0.178 -0.063 -0.168]

Image Generations

image_t = pxt.create_table('together_demo.images', {'input': pxt.String})
image_t.add_computed_column(img=together.image_generations(
    image_t.input,
    model='black-forest-labs/FLUX.1-schnell',
    model_kwargs={'steps': 5}
))
Created table `images`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
image_t.insert([
    {'input': 'A friendly dinosaur playing tennis in a cornfield'}
])
Inserting rows into `images`: 1 rows [00:00, 204.46 rows/s]
Inserted 1 row with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=2, num_excs=0, updated_cols=[], cols_with_excs=[])
image_t
Table 'together_demo.images'
Column Name Type Computed With
input String
img Image image_generations(input, model='black-forest-labs/FLUX.1-schnell', options={'steps': 5})
image_t.head()
input img
A friendly dinosaur playing tennis in a cornfield

Learn More

To learn more about advanced techniques like RAG operations in Pixeltable, check out the RAG Operations in Pixeltable tutorial. You can also look at how to work with images in Pixeltable: Working with Images. If you have any questions, don’t hesitate to reach out.