Skip to content

Commit 4ac7612

Browse files
committed
feat: add translation examples
1 parent 866ba20 commit 4ac7612

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

README.zh-Hans.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,91 @@ Deleting file2...Success
12911291
*/
12921292
```
12931293

1294+
### 翻译能力(Qwen-MT)
1295+
1296+
翻译能力主要通过 `Parameters` 里的 `TranslationOptions` 进行配置。
1297+
1298+
有关支持的语言列表,请参考官方文档:[通义千问翻译模型-大模型服务平台百炼(Model Studio)-阿里云帮助中心](https://help.aliyun.com/zh/model-studio/machine-translation)
1299+
1300+
示例输入:
1301+
1302+
```csharp
1303+
var messages = new List<TextChatMessage>
1304+
{
1305+
// 只能包含一条消息,即翻译的文本
1306+
TextChatMessage.User(
1307+
"博客园创立于2004年1月,是一个面向开发者群体的技术社区。博客园专注于为开发者服务,致力于为开发者打造一个纯净的技术学习与交流社区,帮助开发者持续学习专业知识,不断提升专业技能。博客园的使命是帮助开发者用代码改变世界。")
1308+
};
1309+
var completion = await client.GetTextCompletionAsync(
1310+
new ModelRequest<TextGenerationInput, ITextGenerationParameters>()
1311+
{
1312+
Model = "qwen-mt-turbo",
1313+
Input = new TextGenerationInput() { Messages = messages },
1314+
Parameters = new TextGenerationParameters()
1315+
{
1316+
ResultFormat = "message",
1317+
TranslationOptions = new TextGenerationTranslationOptions()
1318+
{
1319+
// 领域提示,有关源文本的背景信息
1320+
Domains =
1321+
"This is a summary of a website for programmers, use formal and professional tones",
1322+
// 源语言。源文本包含多种语言或不确定具体语言时,可填入 "auto"
1323+
SourceLang = "zh",
1324+
// 目标语言
1325+
TargetLang = "en",
1326+
// 术语表
1327+
Terms = [new TranslationReference("博客园", "Cnblogs.com")],
1328+
// 翻译示例,翻译风格会向示例靠拢
1329+
TmList = [new TranslationReference("代码改变世界", "Coding Changes the World")]
1330+
}
1331+
}
1332+
});
1333+
```
1334+
1335+
完整代码
1336+
1337+
```csharp
1338+
var messages = new List<TextChatMessage>
1339+
{
1340+
TextChatMessage.User(
1341+
"博客园创立于2004年1月,是一个面向开发者群体的技术社区。博客园专注于为开发者服务,致力于为开发者打造一个纯净的技术学习与交流社区,帮助开发者持续学习专业知识,不断提升专业技能。博客园的使命是帮助开发者用代码改变世界。")
1342+
};
1343+
Console.WriteLine("User > " + messages[0].Content);
1344+
var completion = await client.GetTextCompletionAsync(
1345+
new ModelRequest<TextGenerationInput, ITextGenerationParameters>()
1346+
{
1347+
Model = "qwen-mt-plus",
1348+
Input = new TextGenerationInput() { Messages = messages },
1349+
Parameters = new TextGenerationParameters()
1350+
{
1351+
ResultFormat = "message",
1352+
TranslationOptions = new TextGenerationTranslationOptions()
1353+
{
1354+
Domains =
1355+
"This is a summary of a website for programmers, use formal and professional tones",
1356+
SourceLang = "zh",
1357+
TargetLang = "en",
1358+
Terms = [new TranslationReference("博客园", "Cnblogs.com")],
1359+
TmList = [new TranslationReference("代码改变世界", "Coding Changes the World")]
1360+
}
1361+
}
1362+
});
1363+
Console.WriteLine("Assistant > " + completion.Output.Choices![0].Message.Content);
1364+
var usage = completion.Usage;
1365+
if (usage != null)
1366+
{
1367+
Console.WriteLine($"Usage: in({usage.InputTokens})/out({usage.OutputTokens})/total({usage.TotalTokens})");
1368+
}
1369+
1370+
messages.Add(TextChatMessage.Assistant(completion.Output.Choices[0].Message.Content));
1371+
1372+
/*
1373+
User > 博客园创立于2004年1月,是一个面向开发者群体的技术社区。博客园专注于为开发者服务,致力于为开发者打造一个纯净的技术学习与交流社区,帮助开发者持续学习专业知识,不断提升专业技能。博客园的使命是帮助开发者用代码改变世界。
1374+
Assistant > Cnblogs.com was founded in January 2004 and is a technology community aimed at developers. Cnblogs.com focuses on serving developers, committed to creating a pure technology learning and communication community for them, helping developers continuously learn professional knowledge and improve their professional skills. Cnblogs.com's mission is to help developers change the world through coding.
1375+
Usage: in(207)/out(72)/total(279)
1376+
*/
1377+
```
1378+
12941379

12951380

12961381
## 多模态
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Cnblogs.DashScope.Core;
2+
3+
namespace Cnblogs.DashScope.Sample.Text;
4+
5+
public class TranslationSample : ISample
6+
{
7+
/// <inheritdoc />
8+
public string Description => "Translate with Qwen-MT models";
9+
10+
/// <inheritdoc />
11+
public async Task RunAsync(IDashScopeClient client)
12+
{
13+
var messages = new List<TextChatMessage>
14+
{
15+
TextChatMessage.User(
16+
"博客园创立于2004年1月,是一个面向开发者群体的技术社区。博客园专注于为开发者服务,致力于为开发者打造一个纯净的技术学习与交流社区,帮助开发者持续学习专业知识,不断提升专业技能。博客园的使命是帮助开发者用代码改变世界。")
17+
};
18+
Console.WriteLine("User > " + messages[0].Content);
19+
var completion = await client.GetTextCompletionAsync(
20+
new ModelRequest<TextGenerationInput, ITextGenerationParameters>()
21+
{
22+
Model = "qwen-mt-plus",
23+
Input = new TextGenerationInput() { Messages = messages },
24+
Parameters = new TextGenerationParameters()
25+
{
26+
ResultFormat = "message",
27+
TranslationOptions = new TextGenerationTranslationOptions()
28+
{
29+
Domains =
30+
"This is a summary of a website for programmers, use formal and professional tones",
31+
SourceLang = "zh",
32+
TargetLang = "en",
33+
Terms = [new TranslationReference("博客园", "Cnblogs.com")],
34+
TmList = [new TranslationReference("代码改变世界", "Coding Changes the World")]
35+
}
36+
}
37+
});
38+
Console.WriteLine("Assistant > " + completion.Output.Choices![0].Message.Content);
39+
var usage = completion.Usage;
40+
if (usage != null)
41+
{
42+
Console.WriteLine($"Usage: in({usage.InputTokens})/out({usage.OutputTokens})/total({usage.TotalTokens})");
43+
}
44+
45+
messages.Add(TextChatMessage.Assistant(completion.Output.Choices[0].Message.Content));
46+
}
47+
}
48+
49+
/*
50+
User > 博客园创立于2004年1月,是一个面向开发者群体的技术社区。博客园专注于为开发者服务,致力于为开发者打造一个纯净的技术学习与交流社区,帮助开发者持续学习专业知识,不断提升专业技能。博客园的使命是帮助开发者用代码改变世界。
51+
Assistant > Cnblogs.com was founded in January 2004 and is a technology community aimed at developers. Cnblogs.com focuses on serving developers, committed to creating a pure technology learning and communication community for them, helping developers continuously learn professional knowledge and improve their professional skills. Cnblogs.com's mission is to help developers change the world through coding.
52+
Usage: in(207)/out(72)/total(279)
53+
*/

src/Cnblogs.DashScope.Core/Internals/DashScopeDefaults.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json;
1+
using System.Text.Encodings.Web;
2+
using System.Text.Json;
23
using System.Text.Json.Serialization;
34

45
namespace Cnblogs.DashScope.Core.Internals;
@@ -26,5 +27,6 @@ public static class DashScopeDefaults
2627
{
2728
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
2829
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
30+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
2931
};
3032
}

0 commit comments

Comments
 (0)