Skip to content

Commit b11d4ac

Browse files
committed
docs: add json output sample
1 parent 3d7ce60 commit b11d4ac

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

README.zh-Hans.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,87 @@ Usage: in(302)/out(19)/total(321)
968968
*/
969969
```
970970

971+
### 结构化输出(JSON 输出)
972+
973+
设置 `Parameter` 里的 `ResponseFormat` (注意不是 `ResultFormat` )为 JSON 即可强制大模型以 JSON 格式输出。
974+
975+
示例请求:
976+
977+
```csharp
978+
var request = new ModelRequest<TextGenerationInput, ITextGenerationParameters>()
979+
{
980+
Model = "qwen-plus",
981+
Input = new TextGenerationInput() { Messages = messages },
982+
Parameters = new TextGenerationParameters()
983+
{
984+
ResultFormat = "message",
985+
ResponseFormat = DashScopeResponseFormat.Json,
986+
IncrementalOutput = true
987+
}
988+
}
989+
```
990+
991+
示例代码,大模型会以 JSON 输出用户输入的字数信息:
992+
993+
```csharp
994+
var messages = new List<TextChatMessage>();
995+
messages.Add(TextChatMessage.System("使用 JSON 输出用户输入的字数信息"));
996+
while (true)
997+
{
998+
Console.Write("User > ");
999+
var input = Console.ReadLine();
1000+
if (string.IsNullOrEmpty(input))
1001+
{
1002+
Console.WriteLine("Please enter a user input.");
1003+
return;
1004+
}
1005+
1006+
messages.Add(TextChatMessage.User(input));
1007+
var completion = client.GetTextCompletionStreamAsync(
1008+
new ModelRequest<TextGenerationInput, ITextGenerationParameters>()
1009+
{
1010+
Model = "qwen-plus",
1011+
Input = new TextGenerationInput() { Messages = messages },
1012+
Parameters = new TextGenerationParameters()
1013+
{
1014+
ResultFormat = "message",
1015+
ResponseFormat = DashScopeResponseFormat.Json,
1016+
IncrementalOutput = true
1017+
}
1018+
});
1019+
var reply = new StringBuilder();
1020+
var firstChunk = true;
1021+
TextGenerationTokenUsage? usage = null;
1022+
await foreach (var chunk in completion)
1023+
{
1024+
var choice = chunk.Output.Choices![0];
1025+
if (firstChunk)
1026+
{
1027+
firstChunk = false;
1028+
Console.Write("Assistant > ");
1029+
}
1030+
1031+
Console.Write(choice.Message.Content);
1032+
reply.Append(choice.Message.Content);
1033+
usage = chunk.Usage;
1034+
}
1035+
1036+
Console.WriteLine();
1037+
messages.Add(TextChatMessage.Assistant(reply.ToString()));
1038+
if (usage != null)
1039+
{
1040+
Console.WriteLine(
1041+
$"Usage: in({usage.InputTokens})/out({usage.OutputTokens})/total({usage.TotalTokens})");
1042+
}
1043+
}
1044+
1045+
/*
1046+
User > 你好
1047+
Assistant > {"word_count": 2}
1048+
Usage: in(25)/out(7)/total(32)
1049+
*/
1050+
```
1051+
9711052

9721053

9731054
### 多模态
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Text;
2+
using Cnblogs.DashScope.Core;
3+
4+
namespace Cnblogs.DashScope.Sample.Text;
5+
6+
public class JsonOutputSample : ISample
7+
{
8+
/// <inheritdoc />
9+
public string Description => "JSON output text sample";
10+
11+
/// <inheritdoc />
12+
public async Task RunAsync(IDashScopeClient client)
13+
{
14+
var messages = new List<TextChatMessage>();
15+
messages.Add(TextChatMessage.System("使用 JSON 输出用户输入的字数信息"));
16+
while (true)
17+
{
18+
Console.Write("User > ");
19+
var input = Console.ReadLine();
20+
if (string.IsNullOrEmpty(input))
21+
{
22+
Console.WriteLine("Please enter a user input.");
23+
return;
24+
}
25+
26+
messages.Add(TextChatMessage.User(input));
27+
var completion = client.GetTextCompletionStreamAsync(
28+
new ModelRequest<TextGenerationInput, ITextGenerationParameters>()
29+
{
30+
Model = "qwen-plus",
31+
Input = new TextGenerationInput() { Messages = messages },
32+
Parameters = new TextGenerationParameters()
33+
{
34+
ResultFormat = "message",
35+
ResponseFormat = DashScopeResponseFormat.Json,
36+
IncrementalOutput = true
37+
}
38+
});
39+
var reply = new StringBuilder();
40+
var firstChunk = true;
41+
TextGenerationTokenUsage? usage = null;
42+
await foreach (var chunk in completion)
43+
{
44+
var choice = chunk.Output.Choices![0];
45+
if (firstChunk)
46+
{
47+
firstChunk = false;
48+
Console.Write("Assistant > ");
49+
}
50+
51+
Console.Write(choice.Message.Content);
52+
reply.Append(choice.Message.Content);
53+
usage = chunk.Usage;
54+
}
55+
56+
Console.WriteLine();
57+
messages.Add(TextChatMessage.Assistant(reply.ToString()));
58+
if (usage != null)
59+
{
60+
Console.WriteLine(
61+
$"Usage: in({usage.InputTokens})/out({usage.OutputTokens})/total({usage.TotalTokens})");
62+
}
63+
}
64+
}
65+
}
66+
67+
/*
68+
User > 你好
69+
Assistant > {"word_count": 2}
70+
Usage: in(25)/out(7)/total(32)
71+
*/

0 commit comments

Comments
 (0)