Qwen 模型(OpenAI 兼容)
通过兼容 OpenAI 格式的 Chat API 调用通义千问系列模型,支持文本对话、多模态输入、流式输出、工具调用、联网搜索等能力。
接口地址
http
POST /v1/chat/completions认证
| 方式 | 说明 |
|---|---|
| Bearer Token | 在请求头中携带 Authorization: Bearer <API Key> 用于验证账户信息。 |
请求头
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
Content-Type | string | 是 | 请求体的媒介类型,请设置为 application/json |
Authorization | string | 是 | Bearer API Key,用于验证账户信息 |
请求体
| 参数名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
model | string | 是 | - | 模型名称,支持的模型:Qwen 大语言模型等 |
messages | array | 是 | - | 传递给大模型的上下文,按对话顺序排列 |
stream | boolean | 否 | false | 是否以流式输出方式回复 |
stream_options | object | 否 | - | 流式输出配置,仅在 stream 为 true 时生效 |
temperature | float | 否 | 0.7 | 采样温度,控制生成多样性,取值范围 [0, 2) |
top_p | float | 否 | 0.8 | 核采样概率阈值,取值范围 (0, 1.0] |
top_k | integer | 否 | 20 | 采样候选 Token 数量,非 OpenAI 标准参数,SDK 调用时放入 extra_body |
max_tokens | integer | 否 | - | 模型回答最大长度(不包含思维链),即将废弃,请用 max_completion_tokens |
max_completion_tokens | integer | 否 | - | 模型输出最大长度(包含思维链和回答) |
response_format | object | 否 | {"type":"text"} | 返回内容格式,可选 text 或 json_object |
tools | array | 否 | - | 工具对象数组,供模型在 Function Calling 中调用 |
tool_choice | string/object | 否 | auto | 工具选择策略,可选 auto、none 或指定工具 |
parallel_tool_calls | boolean | 否 | false | 是否开启并行工具调用 |
enable_search | boolean | 否 | false | 是否开启联网搜索,非 OpenAI 标准参数,SDK 调用时放入 extra_body |
search_options | object | 否 | - | 联网搜索策略 |
enable_thinking | boolean | 否 | - | 是否开启思考模式,适用于混合思考模型,非 OpenAI 标准参数 |
thinking_budget | integer | 否 | - | 思考过程最大 Token 数,非 OpenAI 标准参数 |
reasoning_effort | string | 否 | - | 推理力度,可选 low/medium/high/max/xhigh,非 OpenAI 标准参数 |
seed | integer | 否 | 1234 | 随机数种子,用于结果可复现,取值范围 [0, 2^31-1] |
stop | string/array | 否 | - | 停止词,生成文本中出现指定字符串时终止 |
n | integer | 否 | 1 | 生成响应数量,取值范围 1-4 |
logprobs | boolean | 否 | false | 是否返回输出 Token 的对数概率 |
top_logprobs | integer | 否 | 0 | 返回候选 Token 个数,取值范围 [0, 5] |
repetition_penalty | float | 否 | 1.05 | 重复度惩罚,大于 0 即可,非 OpenAI 标准参数 |
presence_penalty | float | 否 | 0.0 | 内容重复度控制,取值范围 [-2.0, 2.0] |
messages 消息类型
| 消息类型 | role | 说明 |
|---|---|---|
| System Message | system | 系统消息,设定模型角色、语气、任务约束,一般放第一位 |
| User Message | user | 用户消息,传递问题或上下文 |
| Assistant Message | assistant | 模型回复,用于多轮对话上下文回传 |
| Tool Message | tool | 工具输出信息 |
QwQ 模型不建议设置 System Message,QVQ 模型设置 System Message 不会生效。
stream_options 参数
| 参数名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
include_usage | boolean | 否 | false | 是否在最后一个数据块包含 Token 消耗信息 |
search_options 参数
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
forced_search | boolean | false | 是否强制开启联网搜索 |
search_strategy | string | turbo | 搜索策略,可选 turbo/max/agent/agent_max |
enable_search_extension | boolean | false | 是否开启垂域搜索 |
模型说明
支持的模型
qwen-plus、qwen-max、qwen-turbo、qwen-flashqwen3.5、qwen3.6、qwen3.7系列
具体模型名称和计费请参见平台控制台模型市场。
请求示例
文本输入
bash
curl -X POST 'https://neolink.com/api/v1/chat/completions' \
-H 'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-plus",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你是谁?"}
]
}'python
from openai import OpenAI
client = OpenAI(
api_key="<API Key>",
base_url="https://neolink.com/api/v1",
)
completion = client.chat.completions.create(
model="qwen-plus",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你是谁?"},
],
)
print(completion.model_dump_json())javascript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "<API Key>",
baseURL: "https://neolink.com/api/v1",
});
async function main() {
const completion = await openai.chat.completions.create({
model: "qwen-plus",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "你是谁?" },
],
});
console.log(JSON.stringify(completion));
}
main();流式输出
bash
curl --location 'https://neolink.com/api/v1/chat/completions' \
--header 'Authorization: Bearer <API Key>' \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-plus",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你是谁?"}
],
"stream": true,
"stream_options": {"include_usage": true}
}'python
from openai import OpenAI
client = OpenAI(
api_key="<API Key>",
base_url="https://neolink.com/api/v1",
)
completion = client.chat.completions.create(
model="qwen-plus",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你是谁?"},
],
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
print(chunk.model_dump_json())javascript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "<API Key>",
baseURL: "https://neolink.com/api/v1",
});
async function main() {
const completion = await openai.chat.completions.create({
model: "qwen-plus",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "你是谁?" },
],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of completion) {
console.log(JSON.stringify(chunk));
}
}
main();图像输入
bash
curl -X POST 'https://neolink.com/api/v1/chat/completions' \
-H 'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-vl-plus",
"messages": [
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"}},
{"type": "text", "text": "这是什么"}
]
}
]
}'python
from openai import OpenAI
client = OpenAI(
api_key="<API Key>",
base_url="https://neolink.com/api/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"}},
{"type": "text", "text": "这是什么"},
],
}],
)
print(completion.model_dump_json())javascript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "<API Key>",
baseURL: "https://neolink.com/api/v1",
});
async function main() {
const response = await openai.chat.completions.create({
model: "qwen-vl-max",
messages: [{
role: "user",
content: [
{ type: "image_url", image_url: { url: "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg" } },
{ type: "text", text: "这是什么?" },
],
}],
});
console.log(JSON.stringify(response));
}
main();视频输入(图片列表)
bash
curl -X POST 'https://neolink.com/api/v1/chat/completions' \
-H 'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-vl-max",
"messages": [
{
"role": "user",
"content": [
{
"type": "video",
"video": [
"https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg"
]
},
{"type": "text", "text": "描述这个视频的具体过程"}
]
}
]
}'python
from openai import OpenAI
client = OpenAI(
api_key="<API Key>",
base_url="https://neolink.com/api/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-max",
messages=[{
"role": "user",
"content": [
{
"type": "video",
"video": [
"https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
],
},
{"type": "text", "text": "描述这个视频的具体过程"},
],
}],
)
print(completion.model_dump_json())javascript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "<API Key>",
baseURL: "https://neolink.com/api/v1",
});
async function main() {
const response = await openai.chat.completions.create({
model: "qwen-vl-max",
messages: [{
role: "user",
content: [
{
type: "video",
video: [
"https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
],
},
{ type: "text", text: "描述这个视频的具体过程" },
],
}],
});
console.log(JSON.stringify(response));
}
main();工具调用(Function Calling)
bash
curl -X POST 'https://neolink.com/api/v1/chat/completions' \
-H 'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-plus",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "杭州天气怎么样"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "当你想知道现在的时间时非常有用。",
"parameters": {}
}
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当你想查询指定城市的天气时非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或县区,比如北京市、杭州市、余杭区等。"
}
},
"required": ["location"]
}
}
}
]
}'python
from openai import OpenAI
client = OpenAI(
api_key="<API Key>",
base_url="https://neolink.com/api/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "当你想知道现在的时间时非常有用。",
"parameters": {},
}
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "当你想查询指定城市的天气时非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或县区,比如北京市、杭州市、余杭区等。",
}
},
"required": ["location"],
},
}
},
]
messages = [{"role": "user", "content": "杭州天气怎么样"}]
completion = client.chat.completions.create(
model="qwen-plus",
messages=messages,
tools=tools,
)
print(completion.model_dump_json())javascript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "<API Key>",
baseURL: "https://neolink.com/api/v1",
});
const tools = [
{
type: "function",
function: {
name: "get_current_time",
description: "当你想知道现在的时间时非常有用。",
parameters: {},
},
},
{
type: "function",
function: {
name: "get_current_weather",
description: "当你想查询指定城市的天气时非常有用。",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "城市或县区,比如北京市、杭州市、余杭区等。",
},
},
required: ["location"],
},
},
},
];
async function main() {
const response = await openai.chat.completions.create({
model: "qwen-plus",
messages: [{ role: "user", content: "杭州天气怎么样" }],
tools: tools,
});
console.log(JSON.stringify(response));
}
main();联网搜索
bash
curl -X POST 'https://neolink.com/api/v1/chat/completions' \
-H 'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-plus",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "中国队在巴黎奥运会获得了多少枚金牌"}
],
"enable_search": true
}'python
from openai import OpenAI
client = OpenAI(
api_key="<API Key>",
base_url="https://neolink.com/api/v1",
)
completion = client.chat.completions.create(
model="qwen-plus",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "中国队在巴黎奥运会获得了多少枚金牌"},
],
extra_body={"enable_search": True},
)
print(completion.model_dump_json())javascript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "<API Key>",
baseURL: "https://neolink.com/api/v1",
});
async function main() {
const completion = await openai.chat.completions.create({
model: "qwen-plus",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "中国队在巴黎奥运会获得了多少枚金牌" },
],
enable_search: true,
});
console.log(JSON.stringify(completion));
}
main();异步调用
python
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI(
api_key="<API Key>",
base_url="https://neolink.com/api/v1",
)
async def main():
response = await client.chat.completions.create(
messages=[{"role": "user", "content": "你是谁"}],
model="qwen-plus",
)
print(response.model_dump_json())
asyncio.run(main())javascript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "<API Key>",
baseURL: "https://neolink.com/api/v1",
});
async function main() {
const completion = await openai.chat.completions.create({
model: "qwen-plus",
messages: [{ role: "user", content: "你是谁" }],
});
console.log(JSON.stringify(completion));
}
main();响应体
非流式响应
| 参数名 | 类型 | 说明 |
|---|---|---|
id | string | 本次调用的唯一标识符 |
choices | array | 模型生成内容的数组 |
choices[].message.content | string | 模型的回复内容 |
choices[].message.reasoning_content | string | 模型的思维链内容(思考模式) |
choices[].message.role | string | 消息角色,固定为 assistant |
choices[].message.tool_calls | array | 工具调用信息(Function Calling 时返回) |
choices[].finish_reason | string | 停止原因:stop/length/tool_calls |
choices[].index | integer | 在 choices 数组中的索引 |
choices[].logprobs | object | Token 概率信息 |
created | integer | 请求创建时的 Unix 时间戳(秒) |
model | string | 本次请求使用的模型 |
object | string | 固定为 chat.completion |
usage | object | Token 消耗信息 |
usage.prompt_tokens | integer | 输入 Token 数 |
usage.completion_tokens | integer | 输出 Token 数 |
usage.total_tokens | integer | 总 Token 数 |
usage.prompt_tokens_details.cached_tokens | integer | 命中缓存的 Token 数 |
usage.completion_tokens_details.text_tokens | integer | 输出文本 Token 数 |
非流式响应示例
json
{
"choices": [
{
"message": {
"role": "assistant",
"content": "我是阿里云开发的一款超大规模语言模型,我叫千问。"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 3019,
"completion_tokens": 104,
"total_tokens": 3123,
"prompt_tokens_details": {
"cached_tokens": 2048
}
},
"created": 1735120033,
"model": "qwen-plus",
"id": "chatcmpl-6ada9ed2-7f33-9de2-8bb0-78bd4035025a"
}流式响应
流式输出时,每个 chunk 的 object 为 chat.completion.chunk,choices 中通过 delta 字段返回增量内容:
| 参数名 | 类型 | 说明 |
|---|---|---|
id | string | 本次调用的唯一标识符,每个 chunk 相同 |
choices[].delta.content | string | 增量消息内容 |
choices[].delta.reasoning_content | string | 增量思维链内容 |
choices[].delta.role | string | 角色,只在第一个 chunk 中有值 |
choices[].delta.tool_calls | array | 增量工具调用信息 |
choices[].finish_reason | string | 停止原因,未结束时为 null |
created | integer | 请求创建时间戳 |
model | string | 本次请求使用的模型 |
object | string | 固定为 chat.completion.chunk |
usage | object | Token 消耗信息,仅在 include_usage 为 true 时的最后一个 chunk 返回 |
流式响应示例
json
{"id":"chatcmpl-e30f5ae7","choices":[{"delta":{"content":"","role":"assistant"},"finish_reason":null,"index":0}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","usage":null}
{"id":"chatcmpl-e30f5ae7","choices":[{"delta":{"content":"我是"}}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","usage":null}
{"id":"chatcmpl-e30f5ae7","choices":[{"delta":{"content":"阿里云的超大规模语言模型"}}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","usage":null}
{"id":"chatcmpl-e30f5ae7","choices":[{"delta":{"content":""},"finish_reason":"stop","index":0}],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","usage":null}
{"id":"chatcmpl-e30f5ae7","choices":[],"created":1735113344,"model":"qwen-plus","object":"chat.completion.chunk","usage":{"completion_tokens":17,"prompt_tokens":22,"total_tokens":39,"prompt_tokens_details":{"cached_tokens":0}}}错误码
如果模型调用失败并返回报错信息,请参见 用户错误码指南 进行解决。
