Together AI
Working with Together AI Pixeltable¶
Prerequisites¶
- A Together AI account with an API key (https://api.together.ai/settings/api-keys)
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.
In [1]:
# Required libraries
%pip install -q pixeltable together
In [1]:
import pixeltable as pxt
pxt.create_dir('together_demo', ignore_errors=True)
Connected to Pixeltable database at: postgresql://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata Created directory `together_demo`.
Securely store your Together.ai API key by not hardcoding it into the notebook.
In [2]:
import os
import getpass
if 'TOGETHER_API_KEY' not in os.environ:
os.environ['TOGETHER_API_KEY'] = getpass.getpass('Together API Key: ')
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 Together.ai.
In [3]:
from pixeltable.functions.together import completions
# Create a table in Pixeltable and pick a model hosted on Together with some parameters
t = pxt.create_table('together_demo.completions', {'input': pxt.StringType()})
t['output'] = completions(
prompt=t.input,
model='mistralai/Mixtral-8x7B-v0.1',
max_tokens=300,
stop=['\n'],
temperature=0.7,
top_p=0.9,
top_k=40,
repetition_penalty=1.1,
logprobs=1,
echo=True,
n=3,
)
Created table `completions`. Added 0 column values with 0 errors.
In [4]:
t.describe()
Column Name | Type | Computed With |
---|---|---|
input | string | |
output | json | completions(input, logprobs=1, temperature=0.7, stop=[ ], model='mistralai/Mixtral-8x7B-v0.1', top_p=0.9, top_k=40, echo=True, max_tokens=300, repetition_penalty=1.1, n=3) |
In [5]:
# Parse the response (output) into a new column
t['response'] = t.output.choices[0].text
Added 0 column values with 0 errors.
In [6]:
# Start a conversation
t.insert(input='I want to rule')
t.select(t.input, t.response).show()
Computing cells: 100%|████████████████████████████████████████████| 2/2 [00:00<00:00, 4.47 cells/s] Inserting rows into `completions`: 1 rows [00:00, 245.09 rows/s] Computing cells: 100%|████████████████████████████████████████████| 2/2 [00:00<00:00, 4.37 cells/s] Inserted 1 row with 0 errors.
Out[6]:
input | response |
---|---|
I want to rule | the world. |
Chat Completions¶
In [7]:
from pixeltable.functions.together import chat_completions
# Create a table in Pixeltable and pick a model hosted on Together with some parameters
chat_t = pxt.create_table('together_demo.chat', {'input': pxt.StringType()})
messages = [{'role': 'user', 'content': chat_t.input}]
chat_t['output'] = chat_completions(
messages=messages,
model='mistralai/Mixtral-8x7B-Instruct-v0.1',
max_tokens=300,
stop=['\n'],
temperature=0.7,
top_p=0.9,
top_k=40,
repetition_penalty=1.1,
logprobs=1,
echo=True,
n=3,
)
Created table `chat`. Added 0 column values with 0 errors.
In [8]:
chat_t.describe()
Column Name | Type | Computed With |
---|---|---|
input | string | |
output | json | chat_completions([{'role': 'user', 'content': input}], logprobs=1, temperature=0.7, stop=[ ], model='mistralai/Mixtral-8x7B-Instruct-v0.1', top_p=0.9, top_k=40, echo=True, max_tokens=300, repetition_penalty=1.1, n=3) |
In [9]:
# Parse the bot response (output) into a new column
chat_t['response'] = chat_t.output.choices[0].message.content
Added 0 column values with 0 errors.
In [10]:
# Start a conversation
chat_t.insert(input='Can you make me a coffee?')
chat_t.select(chat_t.input, chat_t.response).show()
Computing cells: 100%|████████████████████████████████████████████| 2/2 [00:01<00:00, 1.47 cells/s] Inserting rows into `chat`: 1 rows [00:00, 203.04 rows/s] Computing cells: 100%|████████████████████████████████████████████| 2/2 [00:01<00:00, 1.45 cells/s] Inserted 1 row with 0 errors.
Out[10]:
input | response |
---|---|
Can you make me a coffee? | I'm sorry for any confusion, but I'm an artificial intelligence and do not have the ability to physically make coffee. However, I can provide instructions on how to make a cup of coffee if you'd like! Just let me know what type of coffee you prefer. |
Embeddings¶
In [11]:
from pixeltable.functions.together import embeddings
emb_t = pxt.create_table('together_demo.embeddings', {'input': pxt.StringType()})
emb_t['embed'] = embeddings(
input=emb_t.input,
model='togethercomputer/m2-bert-80M-8k-retrieval'
)
Created table `embeddings`. Added 0 column values with 0 errors.
In [12]:
emb_t.insert(input='Together AI provides a variety of embeddings models.')
Computing cells: 100%|████████████████████████████████████████████| 1/1 [00:00<00:00, 3.11 cells/s] Inserting rows into `embeddings`: 1 rows [00:00, 263.23 rows/s] Computing cells: 100%|████████████████████████████████████████████| 1/1 [00:00<00:00, 3.01 cells/s] Inserted 1 row with 0 errors.
Out[12]:
UpdateStatus(num_rows=1, num_computed_values=1, num_excs=0, updated_cols=[], cols_with_excs=[])
In [13]:
emb_t.head()
Out[13]:
input | embed |
---|---|
Together AI provides a variety of embeddings models. | [0.016232457, -0.20974171, 0.20096539, 0.15308, -0.33934394, 0.1641776, 0.28299868, 0.15103284, 0.13662423, 0.0044361604, 0.052466832, -0.13507378, -0.12935488, 0.124423794, 0.023793366, -0.20435934, 0.22676797, 0.1209625, -0.034715235, 0.16465378, -0.16237547, -0.16297175, 0.24655177, 0.314733, 0.061162468, -0.27838704, 0.07632538, 0.03980723, 0.03652375, -0.17735523, -0.042062216, -0.14621857, -0.10076542, 0.056929983, 0.011296147, 0.11708491, 0.06328845, 0.16986755, 0.14596735, 0.13139556, -0.13966292, -0.018491898, 0.049842115, -0.12972802, -0.061177313, 0.18479164, -0.25599495, 0.12789905, 0.06758038, -0.17133054, -0.2111011, -0.16792905, -0.21141277, -0.09853814, 0.003973395, -0.21781975, -0.05421263, 0.17835066, 0.073698185, -0.022551578, -0.15593226, 0.08124307, 0.1281974, 0.0080542425, -0.03267917, -0.12479671, -0.010064738, -0.0146909, 0.0007352329, 0.29774678, -0.044923063, -0.19443528, 0.069626346, 0.005396751, -0.08850598, 0.25516367, 0.100371145, -0.04594106, -0.056460228, 0.0725022, -0.005882636, -0.13697596, 0.1823212, 0.15798046, 0.1808659, -0.030637182, 0.15887189, -0.0067441277, 0.22216141, -0.24038352, -0.040920116, 0.045062836, -0.08090567, 0.20003206, -0.15341952, 0.0032852183, 0.11725804, 0.10148666, 0.037926383, 0.13792464, ...] |
Image Generations¶
In [14]:
from pixeltable.functions.together import image_generations
image_t = pxt.create_table('together_demo.images', {'input': pxt.StringType(), 'negative_prompt': pxt.StringType(nullable=True)})
image_t['img'] = image_generations(image_t.input, model='runwayml/stable-diffusion-v1-5')
image_t['img_2'] = image_generations(
image_t.input,
model='stabilityai/stable-diffusion-2-1',
steps=30,
seed=4171780,
height=768,
width=512,
negative_prompt=image_t.negative_prompt
)
Created table `images`. Added 0 column values with 0 errors. Added 0 column values with 0 errors.
In [15]:
# Start generating Images
image_t.insert([
{'input': 'A friendly dinosaur playing tennis in a cornfield'},
{'input': 'A friendly dinosaur playing tennis in a cornfield',
'negative_prompt': 'tennis court'}
])
Inserting rows into `images`: 2 rows [00:00, 886.18 rows/s] Inserted 2 rows with 0 errors.
Out[15]:
UpdateStatus(num_rows=2, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
In [16]:
image_t.describe()
image_t.select(image_t.input, image_t.negative_prompt).show()
Column Name | Type | Computed With |
---|---|---|
input | string | |
negative_prompt | string | |
img | image | image_generations(input, model='runwayml/stable-diffusion-v1-5') |
img_2 | image | image_generations(input, seed=4171780, model='stabilityai/stable-diffusion-2-1', height=768, steps=30, negative_prompt=negative_prompt, width=512) |
Out[16]:
input | negative_prompt |
---|---|
A friendly dinosaur playing tennis in a cornfield | None |
A friendly dinosaur playing tennis in a cornfield | tennis court |
In [17]:
image_t.show()
Out[17]:
input | negative_prompt | img | img_2 |
---|---|---|---|
A friendly dinosaur playing tennis in a cornfield | None | ||
A friendly dinosaur playing tennis in a cornfield | tennis court |
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.
Updated about 1 month ago