Skip to content

Qwen 模型(OpenAI 兼容) ​

通过兼容 OpenAI 格式的 Chat API 调用通义千问系列模型,支持文本对话、多模态输入、流式输出、工具调用、联网搜索等能力。

接口地址 ​

http
POST /v1/chat/completions

认证 ​

方式说明
Bearer Token在请求头中携带 Authorization: Bearer <API Key> 用于验证账户信息。

请求头 ​

参数名类型必填说明
Content-Typestring是请求体的媒介类型,请设置为 application/json
Authorizationstring是Bearer API Key,用于验证账户信息

请求体 ​

参数名类型必填默认值说明
modelstring是-模型名称,支持的模型:Qwen 大语言模型等
messagesarray是-传递给大模型的上下文,按对话顺序排列
streamboolean否false是否以流式输出方式回复
stream_optionsobject否-流式输出配置,仅在 stream 为 true 时生效
temperaturefloat否0.7采样温度,控制生成多样性,取值范围 [0, 2)
top_pfloat否0.8核采样概率阈值,取值范围 (0, 1.0]
top_kinteger否20采样候选 Token 数量,非 OpenAI 标准参数,SDK 调用时放入 extra_body
max_tokensinteger否-模型回答最大长度(不包含思维链),即将废弃,请用 max_completion_tokens
max_completion_tokensinteger否-模型输出最大长度(包含思维链和回答)
response_formatobject否{"type":"text"}返回内容格式,可选 text 或 json_object
toolsarray否-工具对象数组,供模型在 Function Calling 中调用
tool_choicestring/object否auto工具选择策略,可选 auto、none 或指定工具
parallel_tool_callsboolean否false是否开启并行工具调用
enable_searchboolean否false是否开启联网搜索,非 OpenAI 标准参数,SDK 调用时放入 extra_body
search_optionsobject否-联网搜索策略
enable_thinkingboolean否-是否开启思考模式,适用于混合思考模型,非 OpenAI 标准参数
thinking_budgetinteger否-思考过程最大 Token 数,非 OpenAI 标准参数
reasoning_effortstring否-推理力度,可选 low/medium/high/max/xhigh,非 OpenAI 标准参数
seedinteger否1234随机数种子,用于结果可复现,取值范围 [0, 2^31-1]
stopstring/array否-停止词,生成文本中出现指定字符串时终止
ninteger否1生成响应数量,取值范围 1-4
logprobsboolean否false是否返回输出 Token 的对数概率
top_logprobsinteger否0返回候选 Token 个数,取值范围 [0, 5]
repetition_penaltyfloat否1.05重复度惩罚,大于 0 即可,非 OpenAI 标准参数
presence_penaltyfloat否0.0内容重复度控制,取值范围 [-2.0, 2.0]

messages 消息类型 ​

消息类型role说明
System Messagesystem系统消息,设定模型角色、语气、任务约束,一般放第一位
User Messageuser用户消息,传递问题或上下文
Assistant Messageassistant模型回复,用于多轮对话上下文回传
Tool Messagetool工具输出信息

QwQ 模型不建议设置 System Message,QVQ 模型设置 System Message 不会生效。

stream_options 参数 ​

参数名类型必填默认值说明
include_usageboolean否false是否在最后一个数据块包含 Token 消耗信息

search_options 参数 ​

参数名类型默认值说明
forced_searchbooleanfalse是否强制开启联网搜索
search_strategystringturbo搜索策略,可选 turbo/max/agent/agent_max
enable_search_extensionbooleanfalse是否开启垂域搜索

模型说明 ​

支持的模型 ​

  • qwen-plus、qwen-max、qwen-turbo、qwen-flash
  • qwen3.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();

响应体 ​

非流式响应 ​

参数名类型说明
idstring本次调用的唯一标识符
choicesarray模型生成内容的数组
choices[].message.contentstring模型的回复内容
choices[].message.reasoning_contentstring模型的思维链内容(思考模式)
choices[].message.rolestring消息角色,固定为 assistant
choices[].message.tool_callsarray工具调用信息(Function Calling 时返回)
choices[].finish_reasonstring停止原因:stop/length/tool_calls
choices[].indexinteger在 choices 数组中的索引
choices[].logprobsobjectToken 概率信息
createdinteger请求创建时的 Unix 时间戳(秒)
modelstring本次请求使用的模型
objectstring固定为 chat.completion
usageobjectToken 消耗信息
usage.prompt_tokensinteger输入 Token 数
usage.completion_tokensinteger输出 Token 数
usage.total_tokensinteger总 Token 数
usage.prompt_tokens_details.cached_tokensinteger命中缓存的 Token 数
usage.completion_tokens_details.text_tokensinteger输出文本 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 字段返回增量内容:

参数名类型说明
idstring本次调用的唯一标识符,每个 chunk 相同
choices[].delta.contentstring增量消息内容
choices[].delta.reasoning_contentstring增量思维链内容
choices[].delta.rolestring角色,只在第一个 chunk 中有值
choices[].delta.tool_callsarray增量工具调用信息
choices[].finish_reasonstring停止原因,未结束时为 null
createdinteger请求创建时间戳
modelstring本次请求使用的模型
objectstring固定为 chat.completion.chunk
usageobjectToken 消耗信息,仅在 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}}}

错误码 ​

如果模型调用失败并返回报错信息,请参见 用户错误码指南 进行解决。