How to use Qwen on Together AI
Quick answer
Use the
OpenAI Python SDK with base_url="https://api.together.xyz/v1" and your TOGETHER_API_KEY to call the qwen-v1 model on Together AI. Create a client with OpenAI(api_key=os.environ["TOGETHER_API_KEY"], base_url="https://api.together.xyz/v1") and invoke chat.completions.create with the model and messages.PREREQUISITES
Python 3.8+Together AI API key (set TOGETHER_API_KEY environment variable)pip install openai>=1.0
Setup
Install the openai Python package (version 1.0 or higher) and set your Together AI API key as an environment variable TOGETHER_API_KEY. Together AI uses an OpenAI-compatible API endpoint, so you specify base_url="https://api.together.xyz/v1" when creating the client.
pip install openai>=1.0 Step by step
This example shows how to create a client for Together AI, call the qwen-v1 model with a simple chat prompt, and print the response.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["TOGETHER_API_KEY"],
base_url="https://api.together.xyz/v1"
)
response = client.chat.completions.create(
model="qwen-v1",
messages=[{"role": "user", "content": "Hello, how do I use Qwen on Together AI?"}]
)
print(response.choices[0].message.content) output
Hello! To use Qwen on Together AI, create an OpenAI-compatible client with your API key and call the qwen-v1 model as shown.
Common variations
You can use other Qwen variants if available by changing the model parameter. For asynchronous calls, use async functions with await. Streaming responses are supported by passing stream=True and iterating over the response chunks.
import asyncio
from openai import OpenAI
async def async_chat():
client = OpenAI(
api_key=os.environ["TOGETHER_API_KEY"],
base_url="https://api.together.xyz/v1"
)
stream = await client.chat.completions.create(
model="qwen-v1",
messages=[{"role": "user", "content": "Stream a response from Qwen."}],
stream=True
)
async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
asyncio.run(async_chat()) output
Streaming response text from Qwen model printed token by token...
Troubleshooting
- If you get authentication errors, verify your
TOGETHER_API_KEYenvironment variable is set correctly. - If the model is not found, confirm
qwen-v1is the correct model name and Together AI supports it. - For network issues, check your internet connection and that
https://api.together.xyz/v1is reachable.
Key Takeaways
- Together AI uses an OpenAI-compatible API with a custom base_url.
- Use
OpenAISDK withbase_url="https://api.together.xyz/v1"and your API key. - Call
chat.completions.createwithmodel="qwen-v1"to use Qwen. - Support for async and streaming calls is available via the OpenAI SDK.
- Always verify environment variables and model names to avoid common errors.