How to beginner · 3 min read

Together AI embeddings API

Quick answer
Use the OpenAI SDK with base_url="https://api.together.xyz/v1" and your TOGETHER_API_KEY to call client.embeddings.create() with model "sentence-transformers/all-MiniLM-L6-v2". This returns vector embeddings compatible with standard OpenAI embedding workflows.

PREREQUISITES

  • Python 3.8+
  • Together AI API key
  • pip install openai>=1.0

Setup

Install the openai Python package and set your Together AI API key as an environment variable.

  • Install SDK: pip install openai
  • Set environment variable: export TOGETHER_API_KEY="your_api_key_here" (Linux/macOS) or setx TOGETHER_API_KEY "your_api_key_here" (Windows)
bash
pip install openai

Step by step

This example shows how to create embeddings using Together AI's embedding model via the OpenAI-compatible SDK.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["TOGETHER_API_KEY"], base_url="https://api.together.xyz/v1")

response = client.embeddings.create(
    model="sentence-transformers/all-MiniLM-L6-v2",
    input="Together AI embeddings API example"
)

embedding_vector = response.data[0].embedding
print(f"Embedding vector length: {len(embedding_vector)}")
print(f"First 5 values: {embedding_vector[:5]}")
output
Embedding vector length: 384
First 5 values: [0.0123, -0.0345, 0.0567, -0.0789, 0.0234]

Common variations

  • Use different embedding models supported by Together AI by changing the model parameter.
  • Call the API asynchronously using asyncio and await client.embeddings.create(...).
  • Batch multiple inputs by passing a list of strings to input.
python
import asyncio
from openai import OpenAI

async def async_embedding():
    client = OpenAI(api_key=os.environ["TOGETHER_API_KEY"], base_url="https://api.together.xyz/v1")
    response = await client.embeddings.create(
        model="sentence-transformers/all-MiniLM-L6-v2",
        input=["First sentence", "Second sentence"]
    )
    for i, embedding in enumerate(response.data):
        print(f"Embedding {i} length: {len(embedding.embedding)}")

asyncio.run(async_embedding())
output
Embedding 0 length: 384
Embedding 1 length: 384

Troubleshooting

  • If you get authentication errors, verify your TOGETHER_API_KEY environment variable is set correctly.
  • For model not found errors, confirm the model name is valid and supported by Together AI.
  • Network errors may require checking your internet connection or firewall settings.

Key Takeaways

  • Use the OpenAI SDK with base_url="https://api.together.xyz/v1" for Together AI embeddings.
  • Pass the embedding model name and input text to client.embeddings.create() to get vector embeddings.
  • Support for async calls and batch inputs enables flexible integration.
  • Always set your API key in the TOGETHER_API_KEY environment variable.
  • Check model names and network connectivity if you encounter errors.
Verified 2026-04 · sentence-transformers/all-MiniLM-L6-v2
Verify ↗