Skip to main content
更多模型

Qwen-MT API参考

本文介绍通过OpenAI兼容接口 或 DashScopeAPI 调用 Qwen-MT 模型的输入与输出参数。

相关文档: 翻译能力(Qwen-MT)

OpenAI 兼容

  • 北京地域
  • 新加坡地域
  • 美国(弗吉尼亚)地域
SDK 调用配置的base_url为:https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1HTTP 请求地址:POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions
  • 新加坡地域
  • 美国(弗吉尼亚)地域
  • 北京地域
SDK 调用配置的base_url为:https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1HTTP 请求地址:POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions
阿里云百炼为华北2(北京)、新加坡地域推出了业务空间专属域名,能够为推理请求提供卓越的性能和更高的稳定性,建议迁移至新域名:
  • 华北2(北京)地域:从 https://dashscope.aliyuncs.com 迁移至 https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com
  • 新加坡地域:从 https://dashscope-intl.aliyuncs.com 迁移至 https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
其中 {WorkspaceId} 为您的业务空间 ID,可在阿里云百炼控制台的业务空间详情页面查看。现有域名仍可正常使用。
您需要已 获取与配置 API Key配置API Key到环境变量 。若通过OpenAI SDK进行调用,需要 安装SDK

请求体

  • 基础使用
  • 术语干预
  • 翻译记忆
  • 领域提示
  • Python
  • Node.js
  • curl
import os
from openai import OpenAI

client = OpenAI(
    # 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:api_key="sk-xxx",
    # 新加坡和北京地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 以下是北京地域的base_url
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
messages = [
    {
        "role": "user",
        "content": "我看到这个视频后没有笑"
    }
]
translation_options = {
    "source_lang": "Chinese",
    "target_lang": "English"
}

completion = client.chat.completions.create(
    model="qwen-mt-plus",
    messages=messages,
    extra_body={
        "translation_options": translation_options
    }
)
print(completion.choices[0].message.content)
modelstring(必选)模型名称。支持的模型:qwen-mt-plus、qwen-mt-flash、qwen-mt-lite、qwen-mt-turbo。
messagesarray(必选)消息数组,用于向大模型传递上下文。仅支持传入 User Message。

消息类型

User Messageobject(必选)用户消息,用于传递待翻译的句子。

属性

contentstring(必选)待翻译的句子。rolestring(必选)用户消息的角色,必须设为user
streamboolean(可选) 默认值为 false是否以流式方式输出回复。可选值:
  • false:等待模型生成完整回复后一次性返回。
  • true:模型边生成边返回数据块。客户端需逐块读取,以还原完整回复。
当前仅qwen-mt-flash、qwen-mt-lite模型支持以增量形式返回数据,每次返回仅包含新生成的内容。qwen-mt-plus和qwen-mt-turbo模型以非增量形式返回数据,每次返回当前已经生成的整个序列,暂时无法修改。如:II didnI didn'tI didn't laughI didn't laugh after...
stream_optionsobject(可选)流式输出的配置项,仅在 streamtrue 时生效。

属性

include_usageboolean(可选)默认值为 false是否在最后一个数据块包含Token消耗信息。可选值:
  • true:包含;
  • false:不包含。
max_tokensinteger(可选)用于限制模型输出的最大 Token 数。若生成内容超过此值,响应将被截断。默认值与最大值均为模型的最大输出长度,请参见模型选型
seedinteger(可选)随机数种子。用于确保在相同输入和参数下生成结果可复现。若调用时传入相同的 seed 且其他参数不变,模型将尽可能返回相同结果。取值范围:[0,2 31 −1]
temperaturefloat(可选)默认值为0.65采样温度,控制模型生成文本的多样性。temperature越高,生成的文本更多样,反之,生成的文本更确定。取值范围: [0, 2)temperature与top_p均可以控制生成文本的多样性,建议只设置其中一个值。
top_pfloat(可选)默认值为0.8核采样的概率阈值,控制模型生成文本的多样性。top_p越高,生成的文本更多样。反之,生成的文本更确定。取值范围:(0,1.0]temperature与top_p均可以控制生成文本的多样性,建议只设置其中一个值。
top_kinteger(可选)默认值为1生成过程中采样候选集的大小。例如,取值为50时,仅将单次生成中得分最高的50个Token组成随机采样的候选集。取值越大,生成的随机性越高;取值越小,生成的确定性越高。取值为None或当top_k大于100时,表示不启用top_k策略,此时仅有top_p策略生效。取值需要大于或等于0。该参数非OpenAI标准参数。通过 Python SDK调用时,请放入 extra_body 对象中,配置方式为:extra_body={"top_k": xxx};通过 Node.js SDK或HTTP方式调用时,请作为顶层参数传递。
repetition_penaltyfloat(可选)默认值为1.0模型生成时连续序列中的重复度。提高repetition_penalty时可以降低模型生成的重复度,1.0表示不做惩罚。没有严格的取值范围,只要大于0即可。该参数非OpenAI标准参数。通过 Python SDK调用时,请放入 extra_body 对象中,配置方式为:extra_body={"repetition_penalty": xxx};通过 Node.js SDK或HTTP方式调用时,请作为顶层参数传递。
translation_optionsobject(必选)需配置的翻译参数。

属性

source_lang string (必选)源语言的英文全称,详情请参见支持的语言。若设为auto,模型会自动识别输入的语种。target_lang string (必选)目标语言的英文全称,详情请参见支持的语言terms arrays (可选)使用术语干预功能时需设置的术语数组。
source string (必选)源语言的术语。target string (必选)目标语言的术语。
tm_list arrays (可选)使用翻译记忆功能时需设置的翻译记忆数组。
source string (必选)源语言的语句。target string (必选)目标语言的语句。
domains string (可选)使用领域提示功能时需设置的领域提示语句。
领域提示语句暂时只支持英文。
该参数非OpenAI标准参数。通过 Python SDK调用时,请放入 extra_body 对象中,配置方式为:extra_body={"translation_options": xxx};通过 Node.js SDK或HTTP方式调用时,请作为顶层参数传递。

chat响应对象(非流式输出)

{
  "id": "chatcmpl-999a5d8a-f646-4039-968a-167743ae0f22",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": "I didn't laugh after watching this video.",
        "refusal": null,
        "role": "assistant",
        "annotations": null,
        "audio": null,
        "function_call": null,
        "tool_calls": null
      }
    }
  ],
  "created": 1762346157,
  "model": "qwen-mt-plus",
  "object": "chat.completion",
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 9,
    "prompt_tokens": 53,
    "total_tokens": 62,
    "completion_tokens_details": null,
    "prompt_tokens_details": null
  }
}
idstring本次请求的唯一标识符。
choicesarray模型生成内容的数组。

属性

finish_reasonstring模型停止生成的原因。有两种情况:
  • 自然停止输出时为stop
  • 生成长度过长而结束为length
indexinteger当前对象在choices数组中的索引。messageobject模型输出的消息。

属性

content string模型翻译结果。refusal string该参数当前固定为nullrole string消息的角色,固定为assistantaudio object该参数当前固定为nullfunction_call object该参数当前固定为nulltool_calls array该参数当前固定为null
createdinteger本次请求被创建时的时间戳。
modelstring本次请求使用的模型。
object string始终为chat.completion
service_tier string该参数当前固定为null
system_fingerprintstring该参数当前固定为null
usage object本次请求的 Token 消耗信息。
completion_tokens integer模型输出的 Token 数。prompt_tokens integer输入的 Token 数。total_tokens integer消耗的总 Token 数,为prompt_tokenscompletion_tokens的总和。completion_tokens_details object该参数当前固定为nullprompt_tokens_details object该参数当前固定为null

chat响应chunk对象(流式输出)

{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": "", "function_call": null, "refusal": null, "role": "assistant", "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": "I", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": " didn", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": "'t", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": " laugh", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": " after", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": " watching", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": " this", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": " video", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": ".", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": "", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": "stop", "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [{"delta": {"content": "", "function_call": null, "refusal": null, "role": null, "tool_calls": null}, "finish_reason": "stop", "index": 0, "logprobs": null}], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": null}
{"id": "chatcmpl-d8aa6596-b366-4ed0-9f6d-2e89247f554e", "choices": [], "created": 1762504029, "model": "qwen-mt-flash", "object": "chat.completion.chunk", "service_tier": null, "system_fingerprint": null, "usage": {"completion_tokens": 9, "prompt_tokens": 56, "total_tokens": 65, "completion_tokens_details": null, "prompt_tokens_details": null}}
idstring本次调用的唯一标识符。每个chunk对象有相同的 id。
choicesarray模型生成内容的数组。若设置include_usage参数为true,则在最后一个chunk中为空。

属性

delta object流式返回的输出内容。

属性

content string翻译结果,qwen-mt-flash和qwen-mt-lite为增量式更新,qwen-mt-plus和qwen-mt-turbo为非增量式更新。function_call object该参数当前固定为nullrefusal object该参数当前固定为nullrole string消息对象的角色,只在第一个chunk中有值。
finish_reason string模型停止生成的原因。有三种情况:
  • 自然停止输出时为stop
  • 生成未结束时为null
  • 生成长度过长而结束为length
index integer当前响应在choices数组中的索引。
createdinteger本次请求被创建时的时间戳。每个chunk有相同的时间戳。
modelstring本次请求使用的模型。
object string始终为chat.completion.chunk
service_tier string该参数当前固定为null
system_fingerprintstring该参数当前固定为null
usage object本次请求消耗的Token。只在include_usagetrue时,在最后一个chunk返回。
completion_tokens integer模型输出的 Token 数。prompt_tokens integer输入 Token 数。total_tokens integer总 Token 数,为prompt_tokenscompletion_tokens的总和。completion_tokens_details object该参数当前固定为nullprompt_tokens_details object该参数当前固定为null

DashScope

  • 北京地域
  • 新加坡地域
  • 美国(弗吉尼亚)地域
HTTP 请求地址:POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generationSDK 调用无需配置 base_url,其默认值为https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1
  • 新加坡地域
  • 美国(弗吉尼亚)地域
  • 北京地域
HTTP 请求地址:POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generationSDK调用配置的base_url
  • Python代码
  • Java代码
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
您需要已 获取与配置 API Key配置API Key到环境变量 。若通过DashScope SDK进行调用,需要 安装DashScope SDK

请求体

  • 基础使用
  • 术语干预
  • 翻译记忆
  • 领域提示
  • Python
  • Java
  • curl
import os
import dashscope

# 若使用新加坡地域的模型,请将{WorkspaceId}替换为真实的业务空间ID,并释放下列注释
# dashscope.base_http_api_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"

messages = [
    {
        "role": "user",
        "content": "我看到这个视频后没有笑"
    }
]
translation_options = {
    "source_lang": "auto",
    "target_lang": "English",
}
response = dashscope.Generation.call(
    # 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model="qwen-mt-plus",
    messages=messages,
    result_format='message',
    translation_options=translation_options
)
print(response.output.choices[0].message.content)
modelstring(必选)模型名称。支持的模型:qwen-mt-plus、qwen-mt-flash、qwen-mt-lite、qwen-mt-turbo。
messagesarray(必选)消息数组,用于向大模型传递上下文。仅支持传入 User Message。

消息类型

User Messageobject(必选)用户消息,用于传递待翻译的句子。

属性

contentstring(必选)待翻译的句子。rolestring(必选)用户消息的角色,必须设为user
max_tokensinteger(可选)用于限制模型输出的最大 Token 数。若生成内容超过此值,响应将被截断。默认值与最大值均为模型的最大输出长度,请参见模型选型
Java SDK中为maxTokens*。*通过HTTP调用时,请将 max_tokens放入 parameters 对象中。
seedinteger(可选)随机数种子。用于确保在相同输入和参数下生成结果可复现。若调用时传入相同的 seed 且其他参数不变,模型将尽可能返回相同结果。取值范围:[0,2 31 −1]
通过HTTP调用时,请将 seed放入 parameters 对象中。
temperaturefloat(可选)默认值为0.65采样温度,控制模型生成文本的多样性。temperature越高,生成的文本更多样,反之,生成的文本更确定。取值范围: [0, 2)temperature与top_p均可以控制生成文本的多样性,建议只设置其中一个值。
通过HTTP调用时,请将 temperature放入 parameters 对象中。
top_pfloat(可选)默认值为0.8核采样的概率阈值,控制模型生成文本的多样性。top_p越高,生成的文本更多样。反之,生成的文本更确定。取值范围:(0,1.0]temperature与top_p均可以控制生成文本的多样性,建议只设置其中一个值。
Java SDK中为topP*。*通过HTTP调用时,请将 top_p放入 parameters 对象中。
repetition_penaltyfloat(可选)默认值为1.0模型生成时连续序列中的重复度。提高repetition_penalty时可以降低模型生成的重复度,1.0表示不做惩罚。没有严格的取值范围,只要大于0即可。
Java SDK中为repetitionPenalty*。*通过HTTP调用时,请将 repetition_penalty放入 parameters 对象中。
top_kinteger(可选)默认值为1生成过程中采样候选集的大小。例如,取值为50时,仅将单次生成中得分最高的50个Token组成随机采样的候选集。取值越大,生成的随机性越高;取值越小,生成的确定性越高。取值为None或当top_k大于100时,表示不启用top_k策略,此时仅有top_p策略生效。取值需要大于或等于0。
Java SDK中为topK*。*通过HTTP调用时,请将 top_k放入 parameters 对象中。
streamboolean(可选)是否以流式方式输出回复。可选值:
  • false:等待模型生成完整回复后一次性返回。
  • true:模型边生成边返回数据块。客户端需逐块读取,以还原完整回复。
当前仅qwen-mt-flash、qwen-mt-lite模型支持以增量形式返回数据,每次返回仅包含新生成的内容。qwen-mt-plus和qwen-mt-turbo模型以非增量形式返回数据,每次返回当前已经生成的整个序列,暂时无法修改。如:II didnI didn'tI didn't laughI didn't laugh after...
该参数仅支持Python SDK。通过Java SDK实现流式输出请通过streamCall接口调用;通过HTTP实现流式输出请在Header中指定X-DashScope-SSEenable
translation_optionsobject(必选)需配置的翻译参数。

属性

source_lang string (必选)源语言的英文全称,详情请参见支持的语言。若设为auto,模型会自动识别输入的语种。target_lang string (必选)目标语言的英文全称,详情请参见支持的语言terms arrays (可选)使用术语干预功能时需设置的术语数组。
source string (必选)源语言的术语。target string (必选)目标语言的术语。
tm_list arrays (可选)使用翻译记忆功能时需设置的翻译记忆数组。
source string (必选)源语言的语句。target string (必选)目标语言的语句。
domains string (可选)使用领域提示功能时需设置的领域提示语句。
领域提示语句暂时只支持英文。
Java SDK中为translationOptions。通过HTTP调用时,请将 translation_options放入 parameters 对象中。

chat响应对象(流式与非流式输出格式一致)

{
  "status_code": 200,
  "request_id": "9b4ec3b2-6d29-40a6-a08b-7e3c9a51c289",
  "code": "",
  "message": "",
  "output": {
    "text": null,
    "finish_reason": "stop",
    "choices": [
      {
        "finish_reason": "stop",
        "message": {
          "role": "assistant",
          "content": "I didn't laugh after watching this video."
        }
      }
    ],
    "model_name": "qwen-mt-plus"
  },
  "usage": {
    "input_tokens": 53,
    "output_tokens": 9,
    "total_tokens": 62
  }
}
status_codestring本次请求的状态码。200 表示请求成功,否则表示请求失败。
Java SDK 通过GenerationResult.getStatusCode()返回该参数(成功时为 200)。调用失败会抛出异常,可通过ApiException.getStatus().getStatusCode()获取错误状态码。
request_idstring本次调用的唯一标识符。
Java SDK返回参数为requestId。
codestring错误码,调用成功时为空值。
只有Python SDK返回该参数。
outputobject调用结果信息。
textstring该参数当前固定为nullfinish_reasonstring模型结束生成的原因。有以下情况:
  • 正在生成时为null
  • 模型输出自然结束为stop
  • 因生成长度过长而结束为length
choicesarray模型的输出信息。
finish_reasonstring有以下情况:
  • 正在生成时为null
  • 因模型输出自然结束为stop
  • 因生成长度过长而结束为length
messageobject模型输出的消息对象。
rolestring输出消息的角色,固定为assistantcontentstring翻译的结果。
model_namestring本次请求使用的模型名称。
usageobject本次请求使用的Token信息。
input_tokens integer输入 Token 数。output_tokens integer输出 Token 数。total_tokens integer总 Token 数,为input_tokensoutput_tokens之和

错误码

如果模型调用失败并返回报错信息,请参见错误码进行解决。 .aliyun-docs-content .one-codeblocks pre { max-height: calc(80vh - 136px) !important; height: auto; } .tab-item { font-size: 12px !important; /你可以根据需要调整字体大小/ padding: 0px 5px !important; } .expandable-content { border-left: none !important; border-right: none !important; border-bottom: none !important; }