Skip to Content
接口调用阿里云百炼 API

阿里云百炼 API

阿里云百炼模型服务通过 DashScope 提供。使用 OpenAI 兼容客户端时,请使用 DashScope 兼容模式地址,并通过 DashScope API Key 鉴权。

官方文档:

鉴权

  • Header:Authorization: Bearer $DASHSCOPE_API_KEY
  • Header:Content-Type: application/json

Base URL

请按百炼业务空间所在地域选择地址。

地域Base URL
北京https://dashscope.aliyuncs.com/compatible-mode/v1
新加坡https://dashscope-intl.aliyuncs.com/compatible-mode/v1

Chat Completions

  • 方法:POST
  • 路径:/chat/completions
  • 北京地域完整地址:https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

请求参数

参数类型必填说明
modelstring是模型名称,例如 qwen-plus、qwen-turbo,或其他已开通的百炼模型。
messagesarray是OpenAI 兼容消息数组。
messages[].rolestring是system、user、assistant 或 tool。
messages[].contentstring | array是文本或所选模型支持的多模态内容。
temperaturenumber否采样温度。
top_pnumber否核采样参数。
max_tokensinteger否最大输出 Token 数。
streamboolean否是否使用流式响应。
toolsarray否OpenAI 兼容格式的函数工具。
tool_choicestring | object否工具调用策略。

请求示例

curl https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ -d '{ "model": "qwen-plus", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "用一句话介绍GGPU平台。" } ], "temperature": 0.7, "max_tokens": 512 }'

流式调用

请求体中设置 stream: true,客户端按 SSE 流读取返回。

curl https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ -d '{ "model": "qwen-plus", "messages": [ { "role": "user", "content": "请分三点说明 API 网关的作用。" } ], "stream": true }'

原生异步视频生成

DashScope 视频生成不是 OpenAI 兼容的 Chat Completions 请求。请使用 DashScope 原生异步接口格式:顶层 model,input 放提示词内容,parameters 放生成参数。

  • 方法:POST
  • 路径:/api/v1/services/aigc/video-generation/video-synthesis
  • 北京地域完整地址:https://dashscope.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis
  • 必需 Header:X-DashScope-Async: enable

视频请求参数

参数类型必填说明
modelstring是视频模型名称,例如 wanx2.1-t2v-turbo。
input.promptstring是视频生成提示词。
input.negative_promptstring否反向提示词。
parameters.sizestring否输出尺寸,例如 1280*720,以模型支持为准。
parameters.durationinteger否视频时长,单位秒,以模型支持为准。
parameters.prompt_extendboolean否是否由 DashScope 扩写提示词。
parameters.watermarkboolean否是否添加水印。
curl https://dashscope.aliyuncs.com/api/v1/services/aigc/video-generation/video-synthesis \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ -H "X-DashScope-Async: enable" \ -d '{ "model": "wanx2.1-t2v-turbo", "input": { "prompt": "日出时分的平静海面", "negative_prompt": "低画质" }, "parameters": { "size": "1280*720", "duration": 5, "prompt_extend": true, "watermark": false } }'

GGPU网关映射

通过GGPU调用时,请保留同样的 OpenAI 兼容请求体,把 Base URL 替换为GGPU网关地址,并把 DashScope API Key 替换为GGPU访问凭证明文。

curl http://127.0.0.1:18080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <access-credential-secret>" \ -d '{ "model": "qwen-plus", "messages": [ { "role": "user", "content": "用一句话介绍GGPU平台。" } ] }'

通过GGPU调用 DashScope 原生视频生成时,请保留同样的原生异步请求体,并调用对应的网关路径:

curl http://127.0.0.1:18080/api/v1/services/aigc/video-generation/video-synthesis \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <access-credential-secret>" \ -d '{ "model": "wanx2.1-t2v-turbo", "input": { "prompt": "日出时分的平静海面" }, "parameters": { "size": "1280*720", "duration": 5 } }'