Qwen-MT模型是基于通义千问模型优化的机器翻译大语言模型,擅长中英互译、中文与小语种互译、英文与小语种互译,小语种包括日、韩、法、西、德、葡(巴西)、泰、印尼、越、阿等26种。在多语言互译的基础上,提供术语干预、领域提示、记忆库等能力,提升模型在复杂应用场景下的翻译效果。
支持的模型
如果您对翻译质量有较高要求,建议选择qwen-mt-plus模型;如果您希望翻译速度更快或成本更低,建议选择qwen-mt-turbo模型。
模型名称 | 上下文长度 | 最大输入 | 最大输出 | 输入成本 | 输出成本 | 免费额度 |
(Token数) | (每千Token) | |||||
qwen-mt-plus | 2,048 | 1,024 | 1,024 | 0.03元 | 0.06元 | 50万Token 有效期:百炼开通后180天内 |
qwen-mt-turbo | 0.015元 | 0.03元 | 50万Token 有效期:百炼开通后180天内 |
使用方法
由于翻译场景的特殊性,Qwen-MT 与通用文本生成模型的使用方法相比有以下不同:
需要将源语言(
source_lang
)、目标语言(target_lang
)等通过translation_options
参数传入。具体使用方法请参考下方代码。
不支持指定 System Message,也不支持多轮对话;messages 数组中有且仅有一个 User Message,用于指定需要翻译的语句。
暂不支持通过 DashScope Java SDK 调用。
前提条件
您需要已获取API Key并配置API Key到环境变量。如果通过SDK调用,还需要安装DashScope SDK。
简单示例
OpenAI兼容
此处以将中译英的简单场景为例。通过参考支持的语言,您可以将source_lang
参数设为"Chinese"
,target_lang
参数设为"English"
,需要翻译的语言“我看到这个视频后没有笑”
传入 User Message,发起请求后即可得到翻译的结果。
请求示例
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.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-turbo",
messages=messages,
extra_body={
"translation_options": translation_options
}
)
print(completion.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-mt-turbo",
"messages": [{"role": "user", "content": "看完这个视频我没有笑"}],
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English"
}
}'
响应示例
I didn't laugh after watching this video.
DashScope
此处以将中译英的简单场景为例。通过参考支持的语言,您可以将source_lang
参数设为"Chinese"
,target_lang
参数设为"English"
,需要翻译的语言“我看到这个视频后没有笑”
传入 User Message,发起请求后即可得到翻译的结果。
请求示例
import os
import dashscope
messages = [
{
"role": "user",
"content": "我看到这个视频后没有笑"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
}
response = dashscope.Generation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-mt-turbo",
messages=messages,
result_format='message',
translation_options=translation_options
)
print(response.output.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-mt-turbo",
"input": {
"messages": [
{
"content": "我看到这个视频后没有笑",
"role": "user"
}
]
},
"parameters": {
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English"
}
}'
响应示例
I didn't laugh after watching this video.
流式输出
大模型收到输入后并不是一次性生成最终结果,而是逐步地生成中间结果,最终结果由中间结果拼接而成。使用非流式输出方式需要等待模型生成结束后再将生成的中间结果拼接后返回,而流式输出可以实时地将中间结果返回,您可以在模型进行输出的同时进行阅读,减少等待模型回复的时间。
OpenAI兼容
请求示例
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.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-turbo",
messages=messages,
stream=True,
extra_body={
"translation_options": translation_options
}
)
for chunk in completion:
print(chunk.choices[0].delta.content)
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-mt-turbo",
"messages": [{"role": "user", "content": "看完这个视频我没有笑"}],
"stream": true,
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English"
}
}'
响应示例
I
I didn
I didn't
I didn't laugh after watching this video
I didn't laugh after watching this video.
Qwen-MT模型暂时不支持增量式流式输出。
DashScope
请求示例
import os
import dashscope
messages = [
{
"role": "user",
"content": "我看到这个视频后没有笑"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
}
response = dashscope.Generation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-mt-turbo",
messages=messages,
result_format='message',
stream=True,
translation_options=translation_options
)
for chunk in response:
print(chunk.output.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-SSE: enable" \
-d '{
"model": "qwen-mt-turbo",
"input": {
"messages": [
{
"content": "我看到这个视频后没有笑",
"role": "user"
}
]
},
"parameters": {
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English"
}
}'
响应示例
I
I didn
I didn't
I didn't laugh after watching this video
I didn't laugh after watching this video.
Qwen-MT模型暂时不支持增量式流式输出。
术语干预翻译
如果需要翻译的语句中有较多专业术语,直接使用简单示例中的方法可能无法准确翻译。您可以提前对这些术语进行翻译,并将其提供给Qwen-MT模型作为参考,使其能够结合您提供的术语来进行翻译。
术语的定义与传入方法为:
定义术语数组
您需要创建一个包含术语的JSON数组
terms
,每个术语是一个JSON对象,包含术语和翻译过的术语信息,格式如下:{ "source": "术语", "target": "提前翻译好的术语" }
传入数组
通过
translation_options
参数传入第一步定义好的terms
术语数组。
完成术语的定义与传入后,您可以参考以下代码实现术语干预翻译。
OpenAI兼容
请求示例
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
messages = [
{
"role": "user",
"content": "而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
"terms": [
{
"source": "生物传感器",
"target": "biological sensor"
},
{
"source": "石墨烯",
"target": "graphene"
},
{
"source": "化学元素",
"target": "chemical elements"
},
{
"source": "身体健康状况",
"target": "health status of the body"
}
]
}
completion = client.chat.completions.create(
model="qwen-mt-turbo",
messages=messages,
extra_body={
"translation_options": translation_options
}
)
print(completion.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-mt-turbo",
"messages": [
{
"role": "user",
"content": "而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。"
}
],
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English",
"terms": [
{
"source": "生物传感器",
"target": "biological sensor"
},
{
"source": "石墨烯",
"target": "graphene"
},
{
"source": "化学元素",
"target": "chemical elements"
},
{
"source": "身体健康状况",
"target": "health status of the body"
}
]
}
}'
响应示例
This biological sensor uses graphene, a new material, and its target is chemical elements. Its sensitive "nose" can more deeply and accurately reflect the health status of the body.
DashScope
请求示例
import os
import dashscope
messages = [
{
"role": "user",
"content": "而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
"terms": [
{
"source": "生物传感器",
"target": "biological sensor"
},
{
"source": "石墨烯",
"target": "graphene"
},
{
"source": "化学元素",
"target": "chemical elements"
},
{
"source": "身体健康状况",
"target": "health status of the body"
}
]
}
response = dashscope.Generation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-mt-turbo",
messages=messages,
result_format='message',
translation_options=translation_options
)
print(response.output.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-mt-turbo",
"input": {
"messages": [
{
"content": "而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。",
"role": "user"
}
]
},
"parameters": {
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English",
"terms": [
{
"source": "生物传感器",
"target": "biological sensor"
},
{
"source": "石墨烯",
"target": "graphene"
},
{
"source": "化学元素",
"target": "chemical elements"
},
{
"source": "身体健康状况",
"target": "health status of the body"
}
]
}
}'
响应示例
This biological sensor uses graphene, a new material, and its target is chemical elements. Its sensitive "nose" can more deeply and accurately reflect the health status of the human body.
使用翻译记忆
如果您已经有标准的双语句对并且希望大模型在后续翻译时能参考这些标准译文给出结果,可以使用翻译记忆功能。
翻译记忆语句的定义与传入方法为:
定义翻译记忆数组
您需要创建一个已翻译句子的JSON数组
tm_list
,每个JSON对象包含源语句与对应的已翻译的语句,格式如下:{ "source": "源语句", "targer": "已翻译的语句" }
传入翻译记忆数组
通过
translation_options
参数传入第一步定义好的tm_list
翻译记忆数组。
完成翻译记忆语句的定义与传入后,您可以参考以下代码实现翻译记忆功能。
OpenAI兼容
请求示例
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
messages = [
{
"role": "user",
"content": "通过如下命令可以看出安装thrift的版本信息;"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
"tm_list":[
{"source": "您可以通过如下方式查看集群的内核版本信息:", "target": "You can use one of the following methods to query the engine version of a cluster:"},
{"source": "我们云HBase的thrift环境是0.9.0,所以建议客户端的版本也为 0.9.0,可以从这里下载thrift的0.9.0 版本,下载的源码包我们后面会用到,这里需要先安装thrift编译环境,对于源码安装可以参考thrift官网;", "target": "The version of Thrift used by ApsaraDB for HBase is 0.9.0. Therefore, we recommend that you use Thrift 0.9.0 to create a client. Click here to download Thrift 0.9.0. The downloaded source code package will be used later. You must install the Thrift compiling environment first. For more information, see Thrift official website."},
{"source": "您可以通过PyPI来安装SDK,安装命令如下:", "target": "You can run the following command in Python Package Index (PyPI) to install Elastic Container Instance SDK for Python:"}
]
}
completion = client.chat.completions.create(
model="qwen-mt-turbo",
messages=messages,
extra_body={
"translation_options": translation_options
}
)
print(completion.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-mt-turbo",
"messages": [
{
"role": "user",
"content": "通过如下命令可以看出安装thrift的版本信息;"
}
],
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English",
"tm_list":[
{"source": "您可以通过如下方式查看集群的内核版本信息:", "target": "You can use one of the following methods to query the engine version of a cluster:"},
{"source": "我们云HBase的thrift环境是0.9.0,所以建议客户端的版本也为 0.9.0,可以从这里下载thrift的0.9.0 版本,下载的源码包我们后面会用到,这里需要先安装thrift编译环境,对于源码安装可以参考thrift官网;", "target": "The version of Thrift used by ApsaraDB for HBase is 0.9.0. Therefore, we recommend that you use Thrift 0.9.0 to create a client. Click here to download Thrift 0.9.0. The downloaded source code package will be used later. You must install the Thrift compiling environment first. For more information, see Thrift official website."},
{"source": "您可以通过PyPI来安装SDK,安装命令如下:", "target": "You can run the following command in Python Package Index (PyPI) to install Elastic Container Instance SDK for Python:"}
]
}
}'
响应示例
You can use the following commands to check the version information of thrift installed;
DashScope
请求示例
import os
import dashscope
messages = [
{
"role": "user",
"content": "通过如下命令可以看出安装thrift的版本信息;"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
"tm_list":[
{"source": "您可以通过如下方式查看集群的内核版本信息:", "target": "You can use one of the following methods to query the engine version of a cluster:"},
{"source": "我们云HBase的thrift环境是0.9.0,所以建议客户端的版本也为 0.9.0,可以从这里下载thrift的0.9.0 版本,下载的源码包我们后面会用到,这里需要先安装thrift编译环境,对于源码安装可以参考thrift官网;", "target": "The version of Thrift used by ApsaraDB for HBase is 0.9.0. Therefore, we recommend that you use Thrift 0.9.0 to create a client. Click here to download Thrift 0.9.0. The downloaded source code package will be used later. You must install the Thrift compiling environment first. For more information, see Thrift official website."},
{"source": "您可以通过PyPI来安装SDK,安装命令如下:", "target": "You can run the following command in Python Package Index (PyPI) to install Elastic Container Instance SDK for Python:"}
]}
response = dashscope.Generation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-mt-turbo",
messages=messages,
result_format='message',
translation_options=translation_options
)
print(response.output.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-mt-turbo",
"input": {
"messages": [
{
"content": "通过如下命令可以看出安装thrift的版本信息;",
"role": "user"
}
]
},
"parameters": {
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English",
"tm_list":[
{"source": "您可以通过如下方式查看集群的内核版本信息:", "target": "You can use one of the following methods to query the engine version of a cluster:"},
{"source": "我们云HBase的thrift环境是0.9.0,所以建议客户端的版本也为 0.9.0,可以从这里下载thrift的0.9.0 版本,下载的源码包我们后面会用到,这里需要先安装thrift编译环境,对于源码安装可以参考thrift官网;", "target": "The version of Thrift used by ApsaraDB for HBase is 0.9.0. Therefore, we recommend that you use Thrift 0.9.0 to create a client. Click here to download Thrift 0.9.0. The downloaded source code package will be used later. You must install the Thrift compiling environment first. For more information, see Thrift official website."},
{"source": "您可以通过PyPI来安装SDK,安装命令如下:", "target": "You can run the following command in Python Package Index (PyPI) to install Elastic Container Instance SDK for Python:"}
]
}
}'
响应示例
You can use the following commands to check the version information of thrift installed;
领域提示
如果您希望翻译的风格更符合某个领域的特性,如法律、政务领域翻译用语应当严肃正式,社交领域用语应当口语化,可以用一段自然语言文本描述您的领域,将其提供给大模型作为提示。
领域提示语句暂时只支持英文。
通过translation_options
参数传入定义好的 domains
领域提示语句。
OpenAI兼容
请求示例
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
messages = [
{
"role": "user",
"content": "第二个SELECT语句返回一个数字,表示在没有LIMIT子句的情况下,第一个SELECT语句返回了多少行。"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
"domains": "The sentence is from Ali Cloud IT domain. It mainly involves computer-related software development and usage methods, including many terms related to computer software and hardware. Pay attention to professional troubleshooting terminologies and sentence patterns when translating. Translate into this IT domain style."
}
completion = client.chat.completions.create(
model="qwen-mt-turbo",
messages=messages,
extra_body={
"translation_options": translation_options
}
)
print(completion.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-mt-turbo",
"messages": [
{
"role": "user",
"content": "第二个SELECT语句返回一个数字,表示在没有LIMIT子句的情况下,第一个SELECT语句返回了多少行。"
}
],
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English",
"domains": "The sentence is from Ali Cloud IT domain. It mainly involves computer-related software development and usage methods, including many terms related to computer software and hardware. Pay attention to professional troubleshooting terminologies and sentence patterns when translating. Translate into this IT domain style."
}
}'
响应示例
The second SELECT statement returns a number that indicates how many rows were returned by the first SELECT statement without LIMIT clause.
DashScope
请求示例
import os
import dashscope
messages = [
{
"role": "user",
"content": "第二个SELECT语句返回一个数字,表示在没有LIMIT子句的情况下,第一个SELECT语句返回了多少行。"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
"domains": "The sentence is from Ali Cloud IT domain. It mainly involves computer-related software development and usage methods, including many terms related to computer software and hardware. Pay attention to professional troubleshooting terminologies and sentence patterns when translating. Translate into this IT domain style."
}
response = dashscope.Generation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-mt-turbo",
messages=messages,
result_format='message',
translation_options=translation_options
)
print(response.output.choices[0].message.content)
curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-mt-turbo",
"input": {
"messages": [
{
"content": "第二个SELECT语句返回一个数字,表示在没有LIMIT子句的情况下,第一个SELECT语句返回了多少行。",
"role": "user"
}
]
},
"parameters": {
"translation_options": {
"source_lang": "Chinese",
"target_lang": "English",
"domains": "The sentence is from Ali Cloud IT domain. It mainly involves computer-related software development and usage methods, including many terms related to computer software and hardware. Pay attention to professional troubleshooting terminologies and sentence patterns when translating. Translate into this IT domain style."}
}
}'
响应示例
The second SELECT statement returns a number that indicates how many rows were returned by the first SELECT statement without a LIMIT clause.
支持的语言
您可以通过下表查看支持的语言及其英文全称,语言的英文全称用于在发起请求时指定。
语言 | 英文全称 |
中文 | Chinese |
英语 | English |
日语 | Japanese |
韩语 | Korean |
泰语 | Thai |
法语 | French |
德语 | German |
西班牙语 | Spanish |
阿拉伯语 | Arabic |
印尼语 | Indonesian |
越南语 | Vietnamese |
巴西葡萄牙语 | Portuguese |
意大利语 | Italian |
荷兰语 | Dutch |
俄语 | Russian |
高棉语 | Khmer |
老挝语 | Lao |
缅甸语 | Burmese |
宿务语 | Cebuano |
菲律宾语 | Filipino |
捷克语 | Czech |
波兰语 | Polish |
波斯语 | Persian |
希伯来语 | Hebrew |
土耳其语 | Turkish |
印地语 | Hindi |
孟加拉语 | Bengali |
乌尔都语 | Urdu |
API参考
关于通义千问翻译模型的输入与输出参数,请参考通义千问。
错误码
如果模型调用失败并返回报错信息,请参见错误码进行解决。