Python client for Together AI's cloud platform - chat completions, image generation, embeddings, reranking, fine-tuning, and more
A comprehensive Python client for Together AI's cloud platform, providing access to chat completions, image generation, embeddings, reranking, and fine-tuning capabilities.
This skill helps you integrate Together AI's services into Python applications. Together AI provides access to state-of-the-art AI models including Llama, Qwen, CodeLlama, Stable Diffusion, and more through a unified API.
Install the Together AI Python SDK:
```bash
pip install --upgrade together
```
Or using uv (recommended):
```bash
uv add together
```
1. Create an account at [Together.ai](https://api.together.xyz/)
2. Obtain your API key from [API Keys settings](https://api.together.xyz/settings/api-keys)
3. Set your API key as an environment variable:
```bash
export TOGETHER_API_KEY=your_api_key_here
```
Or pass it directly to the client:
```python
from together import Together
client = Together(api_key="your_api_key_here")
```
**Basic text chat:**
```python
from together import Together
client = Together()
response = client.chat.completions.create(
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
messages=[{"role": "user", "content": "tell me about new york"}],
)
print(response.choices[0].message.content)
```
**Multi-modal with images:**
```python
response = client.chat.completions.create(
model="meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.png"}}
]
}]
)
```
**Streaming responses:**
```python
stream = client.chat.completions.create(
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
messages=[{"role": "user", "content": "tell me about new york"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
```
**Async usage:**
```python
import asyncio
from together import AsyncTogether
async def async_chat():
async_client = AsyncTogether()
response = await async_client.chat.completions.create(
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
asyncio.run(async_chat())
```
```python
response = client.completions.create(
model="codellama/CodeLlama-34b-Python-hf",
prompt="Write a Next.js component with TailwindCSS for a header component.",
max_tokens=200,
)
print(response.choices[0].text)
```
```python
response = client.images.generate(
prompt="space robots",
model="stabilityai/stable-diffusion-xl-base-1.0",
steps=10,
n=4,
)
print(response.data[0].b64_json)
```
```python
def get_embeddings(texts, model):
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))]
embeddings = get_embeddings(
['Our solar system orbits the Milky Way galaxy at about 515,000 mph'],
model='togethercomputer/m2-bert-80M-8k-retrieval'
)
```
```python
def get_reranked_documents(query, documents, model, top_n=3):
outputs = client.rerank.create(
model=model,
query=query,
documents=documents,
top_n=top_n
)
return [documents[x.index] for x in sorted(
outputs.results,
key=lambda x: x.relevance_score,
reverse=True
)]
query = "What is the capital of the United States?"
documents = ["New York", "Washington, D.C.", "Los Angeles"]
reranked = get_reranked_documents(query, documents, model='Salesforce/Llama-Rank-V1')
```
**Upload training file:**
```python
client.files.upload(file="training_data.jsonl")
```
**Create fine-tuning job:**
```python
client.fine_tuning.create(
training_file='file-d0d318cb-b7d9-493a-bd70-1cfe089d3815',
model='meta-llama/Llama-3.2-3B-Instruct',
n_epochs=3,
n_checkpoints=1,
batch_size="max",
learning_rate=1e-5,
suffix='my-demo-finetune',
)
```
**List and retrieve jobs:**
```python
client.fine_tuning.list()
client.fine_tuning.retrieve(id="ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b")
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/together-ai-python-sdk/raw