通义千问是阿里云推出的大语言模型服务,提供 Qwen 系列模型的 API 访问。兼容 OpenAI API 格式,支持文本生成、对话、函数调用、长文档理解、图像理解、语音合成等能力。以超高性价比和完善的中文能力著称。
通过阿里云 DashScope 平台管理 API Key:
Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxx
API Key 在 DashScope 控制台 创建。
OpenAI兼容格式:https://dashscope.aliyuncs.com/compatible-mode/v1
原生格式:https://dashscope.aliyuncs.com/api/v1
| 模型ID | 上下文 | 最大输出 | 说明 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| qwen-max | 128K | 8K | 旗舰模型,最强能力 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-plus | 128K | 8K | 能力与性价比均衡 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-turbo | 128K | 8K | 快速响应,低成本 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-long | 1M | 6K | 超长上下文专用 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-vl-max | 32K | 8K | 多模态旗舰 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-vl-plus | 32K | 8K | 多模态均衡 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen2.5-coder-32b | 128K | 8K | 代码专用 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-audio-turbo | 8K | 2K | 语音理解 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text-embedding-v3 | 8K | - | 文本嵌入 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxxxx",
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
response = client.chat.completions.create(
model="qwen-max",
messages=[
{"role": "system", "content": "你是通义千问助手。"},
{"role": "user", "content": "你好!"}
],
temperature=0.7,
top_p=0.8,
max_tokens=2048,
stream=False
)
print(response.choices[0].message.content)
curl https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
-H "Authorization: Bearer $DASHSCOPE_API_KEY"
-H "Content-Type: application/json"
-d '{
"model": "qwen-max",
"messages": [
{"role": "user", "content": "你好"}
]
}'
stream = client.chat.completions.create(
model="qwen-max",
messages=[{"role": "user", "content": "写一首诗"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名"}
},
"required": ["city"]
}
}
}]
response = client.chat.completions.create(
model="qwen-max",
messages=[{"role": "user", "content": "上海今天天气如何?"}],
tools=tools,
tool_choice="auto"
)
import base64
with open("image.jpg", "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()
response = client.chat.completions.create(
model="qwen-vl-max",
messages=[{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_b64}"}},
{"type": "text", "text": "描述这张图片"}
]
}]
)
response = client.chat.completions.create(
model="qwen-long",
messages=[
{"role": "system", "content": "你是一个文档分析助手。"},
{"role": "user", "content": very_long_text} # 支持百万级token
],
max_tokens=4096
)
| 模型 | 输入价格 | 输出价格 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| qwen-max | ¥20/1M tokens | ¥60/1M tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-plus | ¥4/1M tokens | ¥12/1M tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-turbo | ¥1/1M tokens | ¥3/1M tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qwen-long | ¥0.5/1M tokens | ¥2/1M tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
新用户赠送 100 万 tokens 免费额度。
| 层级 | TPM | RPM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 免费 | 60,000 | 60 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 付费 | 300,000 | 300 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 状态码 | 含义 | 处理 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 400 | 请求格式错误 | 检查参数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | API Key 无效 | 检查密钥 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 速率限制/额度不足 | 退避重试或充值 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 服务端错误 | 稍后重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
评论区