跳转到主要内容
对话模型

DashScope API 参考

原生 SDK 与 HTTP API

POST
/api/v1/services/aigc/text-generation/generation
import os
import dashscope

dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
messages = [
  {'role': 'system', 'content': 'You are a helpful assistant.'},
  {'role': 'user', 'content': 'Who are you?'}
]
response = dashscope.Generation.call(
  api_key=os.getenv('DASHSCOPE_API_KEY'),
  model='qwen-plus',
  messages=messages,
  result_format='message'
)
print(response)
{
  "status_code": 200,
  "request_id": "902fee3b-f7f0-9a8c-96a1-6b4ea25af114",
  "code": "",
  "message": "",
  "output": {
    "text": null,
    "finish_reason": null,
    "choices": [
      {
        "finish_reason": "stop",
        "message": {
          "role": "assistant",
          "content": "I am a large-scale language model developed by Alibaba Cloud. My name is Qwen.",
          "tool_calls": null,
          "reasoning_content": null
        }
      }
    ]
  },
  "usage": {
    "input_tokens": 22,
    "output_tokens": 17,
    "total_tokens": 39,
    "image_tokens": null,
    "video_tokens": null,
    "audio_tokens": null
  }
}
获取 API Key将其设置为环境变量。如需使用 SDK,请先安装 SDK

接口地址

  • HTTP(纯文本模型,如 qwen-plus):POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
  • HTTP(多模态模型,如 qwen3.6-plusqwen3-vl-plus):POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
  • SDK base_http_api_urlhttps://dashscope.aliyuncs.com/api/v1
Python SDK
dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
Java SDK
// 方式一:实例化时指定
import com.alibaba.dashscope.protocol.Protocol;
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope.aliyuncs.com/api/v1");

// 方式二:全局设置
import com.alibaba.dashscope.utils.Constants;
Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";

鉴权

string
header
必填

千问云 API Key。详见获取 API Key

请求体

application/json
string
必填

调用的模型名称。支持 Qwen 大语言模型(商业版和开源版)、Qwen-Coder、数学模型、DeepSeek(阿里云直供、硅基流动直供)、Kimi(阿里云直供)、GLM(阿里云直供)、MiniMax(阿里云直供、稀宇科技直供)。模型列表详见文本生成 — Qwen

qwen-plus
object
必填

模型的输入。

object

文本模型的可选生成参数。

响应

200-application/json
integer

请求的状态码。200 表示成功。Java SDK 不返回此字段;调用失败时会抛出包含 status_code 的异常。

string

此请求的唯一标识符。在 Java SDK 中为 requestId

string

错误码。请求成功时为空字符串。仅 Python SDK 返回此字段。

string

可读的错误信息。请求成功时为空字符串。

object

模型的输出。

object

此请求的 token 用量信息。