File size: 10,524 Bytes
0908455 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | # Using Stack 2.9 with Together AI
This guide explains how to use Stack 2.9 with Together AI as the model provider.
## Overview
Together AI provides powerful cloud-hosted models with high performance and competitive pricing. Stack 2.9 supports Together AI through its OpenAI-compatible API, allowing you to use models like:
- `togethercomputer/meta-llama-3-70b-instruct`
- `togethercomputer/CodeLlama-34b-instruct`
- `togethercomputer/Qwen2.5-Coder-32B-Instruct` (recommended for Stack 2.9)
- And many others from Together's model library
## Prerequisites
1. **Together AI Account**: Sign up at [together.ai](https://together.ai)
2. **API Key**: Obtain your API key from the Together dashboard
3. **OpenAI Python Package**: Install `openai>=1.0.0` (required for Together client)
```bash
pip install openai
```
## Environment Variables
Configure your environment with the following variables:
```bash
# Required: Together AI API key
export TOGETHER_API_KEY="your-together-api-key-here"
# Optional: Model selection (default: togethercomputer/Qwen2.5-Coder-32B-Instruct)
export TOGETHER_MODEL="togethercomputer/Qwen2.5-Coder-32B-Instruct"
# Optional: Provider configuration (for auto-detection)
export MODEL_PROVIDER="together"
```
### Setting up in Shell
Add these lines to your `~/.zshrc`, `~/.bashrc`, or shell profile:
```bash
# Together AI configuration
export TOGETHER_API_KEY="tog-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
export TOGETHER_MODEL="togethercomputer/Qwen2.5-Coder-32B-Instruct"
```
Then reload your shell:
```bash
source ~/.zshrc # or ~/.bashrc
```
### Using .env file (recommended for development)
Create a `.env` file in your project root:
```env
TOGETHER_API_KEY=tog-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TOGETHER_MODEL=togethercomputer/Qwen2.5-Coder-32B-Instruct
MODEL_PROVIDER=together
```
Then load it with `python-dotenv`:
```bash
pip install python-dotenv
```
And in your Python script:
```python
from dotenv import load_dotenv
load_dotenv() # loads .env file
```
## Usage Examples
### Command Line
Use the built-in CLI with Together provider:
```bash
# Using default model (Meta-Llama-3-70B)
python stack.py --provider together "Write a Python function to reverse a string"
# Using a specific model (override env var)
TOGETHER_MODEL=togethercomputer/Qwen2.5-Coder-32B-Instruct python stack.py --provider together "def factorial(n):"
```
### Python API
```python
from stack_2_9_eval.model_client import create_model_client
# Create Together client (reads TOGETHER_API_KEY from env)
client = create_model_client(provider="together")
# Or specify explicitly
client = create_model_client(
provider="together",
model="togethercomputer/Qwen2.5-Coder-32B-Instruct",
api_key="your-api-key"
)
# Generate code
result = client.generate(
prompt="Write a Python function to sort a list using quicksort",
temperature=0.2,
max_tokens=1024
)
print(result.text)
```
### Chat Mode
```python
from stack_2_9_eval.model_client import create_model_client, ChatMessage
client = create_model_client(provider="together")
messages = [
ChatMessage(role="system", content="You are an expert Python programmer."),
ChatMessage(role="user", content="How do I read a JSON file in Python?"),
]
result = client.chat(messages, temperature=0.2, max_tokens=512)
print(result.text)
```
### Using with Tool Calls
```python
tools = [
{
"type": "function",
"function": {
"name": "FileReadTool",
"description": "Read file contents",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "File path"}
},
"required": ["path"]
}
}
}
]
messages = [
ChatMessage(role="user", content="Read the file 'config.yaml' and tell me what's in it")
]
result = client.chat(messages, temperature=0.2, tools=tools)
print(result.text)
# Check result.raw_response for tool_calls if model requested a tool
```
## Recommended Models
For Stack 2.9 use cases (coding + tool use), these Together models are recommended:
### Primary Recommendation
**`togethercomputer/qwen2.5-coder-32b-instruct`**
- Matches Stack 2.9's base model (Qwen2.5-Coder-32B)
- Excellent code generation
- Strong tool-calling capabilities
- Cost-effective: ~$0.22 / 1M tokens (input)
- Use this for production Stack 2.9 deployments
### Alternatives
**`togethercomputer/meta-llama-3-70b-instruct`**
- Larger model (70B) with strong reasoning
- Slightly higher cost but excellent quality
- Good for complex problem-solving
**`togethercomputer/codellama-34b-instruct`**
- Code-specialized Llama 34B
- Good performance, lower cost than 70B models
**`togethercomputer/qwen2.5-72b-instruct`**
- 72B variant of Qwen2.5 (if you need maximum quality)
- Higher cost and latency
### Model Selection Tips
- **Match training distribution**: Use Qwen models for Stack 2.9 pattern compatibility
- **Budget**: 34B models offer best price/performance for coding tasks
- **Latency**: Smaller models (7B-13B) are faster but less capable
- **Throughput**: Consider batching for large-scale usage
## Cost Estimation
Together AI pricing (as of 2025, check their site for current rates):
| Model | Input ($/1M tokens) | Output ($/1M tokens) |
|-------|---------------------|----------------------|
| Qwen2.5-Coder-32B | ~0.22 | ~0.22 |
| Meta-Llama-3-70B | ~0.70 | ~0.70 |
| CodeLlama-34B | ~0.22 | ~0.22 |
| Qwen2.5-72B | ~0.70 | ~0.70 |
### Example Cost Calculation
If your typical usage:
- 100 queries/day
- Average 2,000 tokens per query (input + output)
- Using Qwen2.5-Coder-32B
Daily cost: `(100 * 2000 / 1,000,000) * $0.22 β $0.044`
Monthly cost: ~$1.32
**Very affordable for development and light production use.**
## Performance Considerations
- **Latency**: Expect 100-500ms per request depending on model size and complexity
- **Rate Limits**: Together provides generous rate limits (check your plan)
- **Throughput**: Use concurrent requests for batch processing (respect rate limits)
- **Streaming**: Together supports streaming; use `stream=True` in client for long generations
## Error Handling
Implement robust error handling for production:
```python
from stack_2_9_eval.model_client import create_model_client
import time
def generate_with_retry(client, prompt, max_retries=3):
for attempt in range(max_retries):
try:
result = client.generate(prompt, temperature=0.2, max_tokens=1024)
return result
except Exception as e:
if attempt == max_retries - 1:
raise
wait = 2 ** attempt # exponential backoff
print(f"Error: {e}. Retrying in {wait}s...")
time.sleep(wait)
client = create_model_client(provider="together", api_key=os.getenv("TOGETHER_API_KEY"))
result = generate_with_retry(client, "Write a function to calculate prime numbers")
```
## Comparison with Other Providers
| Feature | Together AI | Ollama (local) | OpenAI | Anthropic |
|---------|-------------|----------------|--------|-----------|
| Cost (32B class) | Low (~$0.22/M) | Free (your hardware) | High (~$3/M) | High (~$3/M) |
| Qwen2.5-Coder | β
Supported | β
Via pull | β No | β No |
| Privacy | Cloud (check TOS) | Full local | Cloud | Cloud |
| Latency | Medium | Fast (local) | Medium | Medium |
| Setup Complexity | Low (API key) | Medium (install) | Low | Low |
| Rate Limits | Generous | Unlimited | Pay-as-you-go | Pay-as-you-go |
| Tool Calling | β
Yes | β
Yes | β
Yes | β
Yes |
**Best for Stack 2.9**: Together AI when you need cloud access and Qwen models without running locally.
## Troubleshooting
### API Key Errors
```
ValueError: Together AI API key required.
```
**Solution**: Set `TOGETHER_API_KEY` environment variable or pass `api_key` param.
### Model Not Found
```
openai.BadRequestError: The model '...' does not exist
```
**Solution**: Check model name spelling. Browse available models at [Together Models](https://together.ai/models). Use full model ID like `togethercomputer/qwen2.5-coder-32b-instruct`.
### Rate Limit Exceeded
**Solution**: Add retry logic with exponential backoff. Consider upgrading your Together plan.
### Import Errors
```
ImportError: openai package required
```
**Solution**: `pip install openai` (version 1.0+)
## Advanced Configuration
### Custom Base URL
If you need to use a custom endpoint (e.g., for regional deployments):
```python
client = create_model_client(
provider="together",
model="togethercomputer/qwen2.5-coder-32b-instruct",
base_url="https://your-custom-endpoint.together.ai/v1"
)
```
### Timeouts and Retries
```python
client = TogetherClient(
model="togethercomputer/qwen2.5-coder-32b-instruct",
api_key=os.getenv("TOGETHER_API_KEY"),
timeout=300 # 5 minute timeout
)
```
### Streaming Responses
For long generations, use streaming (requires modifying client or using OpenAI library directly):
```python
from openai import OpenAI
client = OpenAI(api_key=os.getenv("TOGETHER_API_KEY"), base_url="https://api.together.xyz/v1")
stream = client.chat.completions.create(
model="togethercomputer/qwen2.5-coder-32b-instruct",
messages=[{"role": "user", "content": "Write a detailed explanation of binary search"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
```
## Integration with Stack 2.9 CLI
To make Together AI the default provider:
```bash
# Set environment variables permanently
echo 'export MODEL_PROVIDER="together"' >> ~/.zshrc
echo 'export TOGETHER_MODEL="togethercomputer/qwen2.5-coder-32b-instruct"' >> ~/.zshrc
source ~/.zshrc
```
Now `stack.py` will automatically use Together AI without `--provider` flag.
## Security Best Practices
1. **Never commit API keys** to version control. Use `.env` files or environment variables.
2. **Rotate keys** periodically from Together dashboard.
3. **Use minimal permissions** - Together API keys have full access; protect them.
4. **Enable billing alerts** to avoid unexpected charges.
5. **Review Together's TOS** for data usage and privacy policies.
## Support
- **Together Documentation**: https://docs.together.io/
- **Stack 2.9 Issues**: https://github.com/my-ai-stack/stack-2.9/issues
- **Model Cards**: See `MODEL_CARD.md` for Stack 2.9 details
---
**Last Updated**: 2025-04-02
**Compatible Stack 2.9 Version**: 2.9.0+
|