Qwen-MT模型是基于Qwen3模型优化的机器翻译大语言模型,支持92个语种(包括中、英、日、韩、法、西、德、泰、印尼、越、阿等)互译,且提供了术语干预、领域提示、记忆库等能力,提升模型在复杂应用场景下的翻译效果。
工作方式
设置语种:参考支持的语言,在
translation_options
中设置源语种 (source_lang
) 和目标语种 (target_lang
)。若需自动检测源语种,可将source_lang
设为auto
。指明源语种有利于提升翻译准确率。
传入待翻译内容:
messages
数组中有且仅有一个role
为user
的消息,其content
为待翻译文本。
以 OpenAI 与 DashScope 的 Python SDK 为例,介绍如何调用 Qwen-MT 模型。
OpenAI 兼容
# 导入依赖与创建客户端...
completion = client.chat.completions.create(
model="qwen-mt-turbo", # 选择模型
# messages 有且仅有一个 role 为 user 的消息,其 content 为待翻译文本
messages=[{"role": "user", "content": "我看到这个视频后没有笑"}],
# 由于 translation_options 非 OpenAI 标准参数,需要通过 extra_body 传入
extra_body={"translation_options": {"source_lang": "Chinese", "target_lang": "English"}},
)
DashScope
# 导入依赖...
response = dashscope.Generation.call(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="qwen-mt-turbo", # 选择模型
messages=[{"role": "user", "content": "我看到这个视频后没有笑"}], # messages: role为user, content为待翻译文本
translation_options={"source_lang": "Chinese", "target_lang": "English"}, # 配置翻译选项
result_format="message"
)
使用限制:
仅支持单轮翻译:模型专为翻译任务设计,不支持多轮对话。
不支持设置 System Message:不支持通过
system
角色的消息来设定全局行为。所有翻译配置均在translation_options
中定义。
模型与价格
Qwen-MT 包含 qwen-mt-plus 与 qwen-mt-turbo 模型:
qwen-mt-plus:旗舰模型,多语言综合效果更好,结构复杂的文本理解更精准;
qwen-mt-turbo:高性价比模型,轻量级,响应速度快。
模型名称 | 上下文长度 | 最大输入 | 最大输出 | 输入成本 | 输出成本 | 免费额度 |
(Token数) | (每千Token) | |||||
qwen-mt-plus 属于Qwen3-MT | 4,096 | 2,048 | 2,048 | 0.0018 | 0.0054 | 各100万Token 有效期:百炼开通后90天内 |
qwen-mt-turbo 属于Qwen3-MT | 0.0007 | 0.00195 |
模型限流条件请参见限流。
快速开始
此处以将“我看到这个视频后没有笑”
翻译为英文的简单场景为例。
您需要已获取API Key并配置API Key到环境变量。如果通过OpenAI SDK或DashScope SDK进行调用,还需要安装SDK。
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": "auto",
"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": "auto",
"target_lang": "English"
}
}'
响应示例
I didn't laugh after watching this video.
DashScope
请求示例
DashScope Java SDK 版本需要不低于 2.20.6。
import os
import dashscope
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-turbo",
messages=messages,
result_format='message',
translation_options=translation_options
)
print(response.output.choices[0].message.content)
// DashScope SDK 版本需要不低于 2.20.6
import java.lang.System;
import java.util.Collections;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.TranslationOptions;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("我看到这个视频后没有笑")
.build();
TranslationOptions options = TranslationOptions.builder()
.sourceLang("auto")
.targetLang("English")
.build();
GenerationParam param = GenerationParam.builder()
// 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-mt-plus")
.messages(Collections.singletonList(userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.translationOptions(options)
.build();
return gen.call(param);
}
public static void main(String[] args) {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("错误信息:"+e.getMessage());
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
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": "auto",
"target_lang": "English"
}
}
}'
响应示例
I didn't laugh after watching this video.
流式输出
流式输出可以实时返回翻译内容,减少用户等待时间。OpenAI 兼容与 DashScope 方式均仅支持非增量流式输出,每次返回当前已经生成的整个序列,最后返回的数据为生成的完整结果。
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,
stream_options={"include_usage": True},
extra_body={"translation_options": translation_options},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta.content)
else:
print("="*20+"usage 消耗"+"="*20)
print(chunk.usage)
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
I didn’t laugh after
I didn’t laugh after watching
I didn’t laugh after watching this
I didn’t laugh after watching this video
I didn’t laugh after watching this video.
I didn’t laugh after watching this video.
I didn’t laugh after watching this video.
====================usage 消耗====================
CompletionUsage(completion_tokens=9, prompt_tokens=56, total_tokens=65, completion_tokens_details=None, prompt_tokens_details=None)
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)
// DashScope SDK 版本需要不低于 2.20.6
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.TranslationOptions;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.reactivex.Flowable;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
private static void handleGenerationResult(GenerationResult message) {
String content = message.getOutput().getChoices().get(0).getMessage().getContent();
System.out.println(content);
}
public static void streamCallWithMessage(Generation gen, Message userMsg)
throws NoApiKeyException, ApiException, InputRequiredException {
GenerationParam param = buildGenerationParam(userMsg);
Flowable<GenerationResult> result = gen.streamCall(param);
result.blockingForEach(message -> handleGenerationResult(message));
}
private static GenerationParam buildGenerationParam(Message userMsg) {
TranslationOptions options = TranslationOptions.builder()
.sourceLang("auto")
.targetLang("English")
.build();
return GenerationParam.builder()
// 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-mt-plus")
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.translationOptions(options)
.incrementalOutput(true)
.messages(Arrays.asList(userMsg))
.build();
}
public static void main() {
try {
Generation gen = new Generation();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("我看到这个视频后没有笑").build();
streamCallWithMessage(gen, userMsg);
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
logger.error("An exception occurred: {}", e.getMessage());
}
System.exit(0);
}
}
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模型暂时不支持增量式流式输出。
提升翻译效果
标准翻译能应对日常沟通等简单需求,但在面对专业、高要求的翻译任务时可能遇到以下问题:
术语不统一:产品名或行业术语翻译错误。
风格不匹配:译文风格不符合法律、营销等特定领域的规范。
可通过术语干预、翻译记忆与领域提示功能解决这些问题。
术语干预
当文本包含品牌名、产品名或专业术语时,为保证翻译的准确性和一致性,可通过 terms
字段提供一个术语表,引导模型参考术语表进行翻译。
术语的定义与传入方法为:
定义术语
创建一个JSON数组,并将其赋值给
terms
字段。数组中的每一个对象代表一条术语,格式如下:{ "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": "而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。"
}
]
# --- 第一次请求:不使用 terms 参数 ---
print("--- [不使用 terms] 的翻译结果 ---")
translation_options_without_terms = {
"source_lang": "auto",
"target_lang": "English"
}
completion_without_terms = client.chat.completions.create(
model="qwen-mt-plus",
messages=messages,
extra_body={
"translation_options": translation_options_without_terms
}
)
print(completion_without_terms.choices[0].message.content)
print("\n" + "="*50 + "\n") # 用于分割,方便对比
# --- 第二次请求:使用 terms 参数 ---
print("--- [使用 terms] 的翻译结果 ---")
translation_options_with_terms = {
"source_lang": "Chinese",
"target_lang": "English",
"terms": [
{
"source": "生物传感器",
"target": "biological sensor"
},
{
"source": "身体健康状况",
"target": "health status of the body"
}
]
}
completion_with_terms = client.chat.completions.create(
model="qwen-mt-plus",
messages=messages,
extra_body={
"translation_options": translation_options_with_terms
}
)
print(completion_with_terms.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": "health status of the body"
}
]
}
}'
响应示例
加入术语后,翻译结果与传入的术语一致:“生物传感器”(biological sensor)和“身体健康状况”(the health status of the body)。
--- [不使用 terms] 的翻译结果 ---
This set of biosensors uses graphene, a new material, whose target substance is chemical elements. Its sensitive "sense of smell" allows it to more deeply and accurately reflect one's health condition.
==================================================
--- [使用 terms] 的翻译结果 ---
This biological sensor uses a new material called graphene. Its target is chemical elements, and its sensitive "sense of smell" enables it to reflect the health status of the body more deeply and accurately.
DashScope
请求示例
import os
import dashscope
messages = [
{
"role": "user",
"content": "而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。"
}
]
translation_options = {
"source_lang": "Chinese",
"target_lang": "English",
"terms": [
{
"source": "生物传感器",
"target": "biological sensor"
},
{
"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)
// DashScope SDK 版本需要不低于 2.20.6
import java.lang.System;
import java.util.Collections;
import java.util.Arrays;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.TranslationOptions;
import com.alibaba.dashscope.aigc.generation.TranslationOptions.Term;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。")
.build();
Term term1 = Term.builder()
.source("生物传感器")
.target("biological sensor")
.build();
Term term2 = Term.builder()
.source("身体健康状况")
.target("health status of the body")
.build();
TranslationOptions options = TranslationOptions.builder()
.sourceLang("auto")
.targetLang("English")
.terms(Arrays.asList(term1, term2))
.build();
GenerationParam param = GenerationParam.builder()
// 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-mt-plus")
.messages(Collections.singletonList(userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.translationOptions(options)
.build();
return gen.call(param);
}
public static void main() {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("错误信息:"+e.getMessage());
}
System.exit(0);
}
}
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": "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.
翻译记忆
如果您希望模型参考特定的翻译风格或句式,可通过 tm_list
字段提供“源文-译文”句对作为示例。模型将在本次翻译任务中模仿这些示例的风格。
定义翻译记忆
您需要创建一个已翻译句子的JSON数组
tm_list
,每个JSON对象包含源语句与对应的已翻译的语句,格式如下:{ "source": "源语句", "target": "已翻译的语句" }
传入翻译记忆
通过
translation_options
参数传入翻译记忆数组。
可参考以下代码实现翻译记忆功能。
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-plus",
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 run the following command to view the version of Thrift that is 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)
// DashScope SDK 版本需要不低于 2.20.6
import java.lang.System;
import java.util.Collections;
import java.util.Arrays;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.TranslationOptions;
import com.alibaba.dashscope.aigc.generation.TranslationOptions.Tm;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("通过如下命令可以看出安装thrift的版本信息;")
.build();
Tm tm1 = Tm.builder()
.source("您可以通过如下方式查看集群的内核版本信息:")
.target("You can use one of the following methods to query the engine version of a cluster:")
.build();
Tm tm2 = Tm.builder()
.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.")
.build();
Tm tm3 = Tm.builder()
.source("您可以通过PyPI来安装SDK,安装命令如下:")
.target("You can run the following command in Python Package Index (PyPI) to install Elastic Container Instance SDK for Python:")
.build();
TranslationOptions options = TranslationOptions.builder()
.sourceLang("auto")
.targetLang("English")
.tmList(Arrays.asList(tm1, tm2, tm3))
.build();
GenerationParam param = GenerationParam.builder()
// 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-mt-plus")
.messages(Collections.singletonList(userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.translationOptions(options)
.build();
return gen.call(param);
}
public static void main() {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("错误信息:"+e.getMessage());
}
System.exit(0);
}
}
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
参数传入领域提示语句。
领域提示语句当前只支持英文。
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语句返回了多少行。"
}
]
# --- 第一次请求:不使用 domains 参数 ---
print("--- [不使用 domains] 的翻译结果 ---")
translation_options_without_domains = {
"source_lang": "Chinese",
"target_lang": "English",
}
completion_without_domains = client.chat.completions.create(
model="qwen-mt-plus",
messages=messages,
extra_body={
"translation_options": translation_options_without_domains
}
)
print(completion_without_domains.choices[0].message.content)
print("\n" + "="*50 + "\n") # 用于分割,方便对比
# --- 第二次请求:使用 domains 参数 (您的原始代码) ---
print("--- [使用 domains] 的翻译结果 ---")
translation_options_with_domains = {
"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_with_domains = client.chat.completions.create(
model="qwen-mt-plus",
messages=messages,
extra_body={
"translation_options": translation_options_with_domains
}
)
print(completion_with_domains.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."
}
}'
响应示例
--- [不使用 domains] 的翻译结果 ---
The second SELECT statement returns a number indicating how many rows the first SELECT statement would return without the LIMIT clause.
==================================================
--- [使用 domains] 的翻译结果 ---
The second SELECT statement returns a number that indicates how many rows the first SELECT statement would have returned if it had not included a 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)
// DashScope SDK 版本需要不低于 2.20.6
import java.lang.System;
import java.util.Collections;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.TranslationOptions;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("第二个SELECT语句返回一个数字,表示在没有LIMIT子句的情况下,第一个SELECT语句返回了多少行。")
.build();
TranslationOptions options = TranslationOptions.builder()
.sourceLang("auto")
.targetLang("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.")
.build();
GenerationParam param = GenerationParam.builder()
// 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-mt-plus")
.messages(Collections.singletonList(userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.translationOptions(options)
.build();
return gen.call(param);
}
public static void main() {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("错误信息:"+e.getMessage());
}
System.exit(0);
}
}
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.
应用于生产环境
控制输入 Token 数量
Qwen-MT 模型的输入 Token 上限为 2,048。若输入内容较长,可参考以下策略控制输入 Token 数:
分段翻译:翻译长文本时请分段处理。按语义(如段落或完整句子)分割文本,而非按字符数。这能保证上下文完整性,提升翻译质量。
传入最相关的参考内容:术语、翻译记忆和领域提示会作为输入 Token 拼接到提示词中。请仅提供与当前任务最相关的参考内容,避免使用庞大、通用的列表。
根据场景设置
source_lang
在不确定翻译原文的语种时(如社交聊天场景时存在多语言文本)可设置
source_lang
为auto,模型将自动识别源语种
。在语种固定、准确度要求高的场景(如技术文档或操作手册),建议始终指定
source_lang
,明确指定源语种可提升翻译准确率。
支持的语言
下表中的英文名或代码用于在发起请求时指定。
当不确定源语种时,可为source_lang
参数赋值auto
以自动检测。
语言 | 英文名 | 代码 |
英语 | English | en |
简体中文 | Chinese | zh |
繁体中文 | Traditional Chinese | zh_tw |
俄语 | Russian | ru |
日语 | Japanese | ja |
韩语 | Korean | ko |
西班牙语 | Spanish | es |
法语 | French | fr |
葡萄牙语 | Portuguese | pt |
德语 | German | de |
意大利语 | Italian | it |
泰语 | Thai | th |
越南语 | Vietnamese | vi |
印度尼西亚语 | Indonesian | id |
马来语 | Malay | ms |
阿拉伯语 | Arabic | ar |
印地语 | Hindi | hi |
希伯来语 | Hebrew | he |
缅甸语 | Burmese | my |
泰米尔语 | Tamil | ta |
乌尔都语 | Urdu | ur |
孟加拉语 | Bengali | bn |
波兰语 | Polish | pl |
荷兰语 | Dutch | nl |
罗马尼亚语 | Romanian | ro |
土耳其语 | Turkish | tr |
高棉语 | Khmer | km |
老挝语 | Lao | lo |
粤语 | Cantonese | yue |
捷克语 | Czech | cs |
希腊语 | Greek | el |
瑞典语 | Swedish | sv |
匈牙利语 | Hungarian | hu |
丹麦语 | Danish | da |
芬兰语 | Finnish | fi |
乌克兰语 | Ukrainian | uk |
保加利亚语 | Bulgarian | bg |
塞尔维亚语 | Serbian | sr |
泰卢固语 | Telugu | te |
南非荷兰语 | Afrikaans | af |
亚美尼亚语 | Armenian | hy |
阿萨姆语 | Assamese | as |
阿斯图里亚斯语 | Asturian | ast |
巴斯克语 | Basque | eu |
白俄罗斯语 | Belarusian | be |
波斯尼亚语 | Bosnian | bs |
加泰罗尼亚语 | Catalan | ca |
宿务语 | Cebuano | ceb |
克罗地亚语 | Croatian | hr |
埃及阿拉伯语 | Egyptian Arabic | arz |
爱沙尼亚语 | Estonian | et |
加利西亚语 | Galician | gl |
格鲁吉亚语 | Georgian | ka |
古吉拉特语 | Gujarati | gu |
冰岛语 | Icelandic | is |
爪哇语 | Javanese | jv |
卡纳达语 | Kannada | kn |
哈萨克语 | Kazakh | kk |
拉脱维亚语 | Latvian | lv |
立陶宛语 | Lithuanian | lt |
卢森堡语 | Luxembourgish | lb |
马其顿语 | Macedonian | mk |
马加希语 | Maithili | mai |
马耳他语 | Maltese | mt |
马拉地语 | Marathi | mr |
美索不达米亚阿拉伯语 | Mesopotamian Arabic | acm |
摩洛哥阿拉伯语 | Moroccan Arabic | ary |
内志阿拉伯语 | Najdi Arabic | ars |
尼泊尔语 | Nepali | ne |
北阿塞拜疆语 | North Azerbaijani | az |
北黎凡特阿拉伯语 | North Levantine Arabic | apc |
北乌兹别克语 | Northern Uzbek | uz |
书面语挪威语 | Norwegian Bokmål | nb |
新挪威语 | Norwegian Nynorsk | nn |
奥克语 | Occitan | oc |
奥里亚语 | Odia | or |
邦阿西楠语 | Pangasinan | pag |
西西里语 | Sicilian | scn |
信德语 | Sindhi | sd |
僧伽罗语 | Sinhala | si |
斯洛伐克语 | Slovak | sk |
斯洛文尼亚语 | Slovenian | sl |
南黎凡特阿拉伯语 | South Levantine Arabic | ajp |
斯瓦希里语 | Swahili | sw |
他加禄语 | Tagalog | tl |
塔伊兹-亚丁阿拉伯语 | Ta’izzi-Adeni Arabic | acq |
托斯克阿尔巴尼亚语 | Tosk Albanian | sq |
突尼斯阿拉伯语 | Tunisian Arabic | aeb |
威尼斯语 | Venetian | vec |
瓦莱语 | Waray | war |
威尔士语 | Welsh | cy |
西波斯语 | Western Persian | fa |
API参考
Qwen-MT 模型的输入与输出参数请参考通义千问。
错误码
如果模型调用失败并返回报错信息,请参见错误信息进行解决。