Python library

Reference this guide to learn how to run embeddings using our Python API.

This tutorial covers how to use Together Embeddings to get embeddings for your own data using the Together Python library.

Pre-requisites

  • Ensure you have Python installed on your machine.
  • Create a free account with Together.ai to obtain a Together API Key.

Install the Library

Install or update the Together library by executing the following command:

pip install --upgrade together

Define your parameters

The two parameters we need to specify are model and input where model is an embedding model we want to use and input is a list of texts you want to embed. The model we are using for this guide is togethercomputer/m2-bert-80M-8k-retrieval. Find the full list of available models in the Models page.

Generate embeddings for your data

Specify your API key (found in your account settings) along with the embedding model you want to use and the data you want to embed.

from typing import List
from together import Together

client = Together()

def get_embeddings(texts: List[str], model: str) -> List[List[float]]:
    texts = [text.replace("\n", " ") for text in texts]
    outputs = client.embeddings.create(model=model, input=texts)
    return [outputs.data[i].embedding for i in range(len(texts))]

input_texts = ['Our solar system orbits the Milky Way galaxy at about 515,000 mph']
embeddings = get_embeddings(input_texts, model='togethercomputer/m2-bert-80M-8k-retrieval')

print(embeddings)
# [[0.13437459, 0.09866201, ..., -0.20736569]]