Skip to content

Commit b12ba01

Browse files
committed
feat: update sample
1 parent fce85a6 commit b12ba01

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ An unofficial DashScope SDK maintained by Cnblogs.
1111

1212
# Quick Start
1313

14+
## Using `Microsoft.Extensions.AI`
15+
16+
Install `Cnblogs.Extensions.AI.DashScope` Package
17+
18+
```csharp
19+
var client = new DashScopeClient("your-api-key").AsChatClient("qwen-max");
20+
var completion = await client.CompleteAsync("hello");
21+
Console.WriteLine(completion)
22+
```
23+
1424
## Console App
1525

1626
Install `Cnblogs.DashScope.Sdk` package.

README.zh-Hans.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
# 快速开始
1313

14+
## 使用 `Microsoft.Extensions.AI` 接口
15+
16+
安装 NuGet 包 `Cnblogs.Extensions.AI.DashScope`
17+
18+
```csharp
19+
var client = new DashScopeClient("your-api-key").AsChatClient("qwen-max");
20+
var completion = await client.CompleteAsync("hello");
21+
Console.WriteLine(completion)
22+
```
23+
1424
## 控制台应用
1525

1626
安装 NuGet 包 `Cnblogs.DashScope.Sdk`

sample/Cnblogs.DashScope.Sample/Cnblogs.DashScope.Sample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<ProjectReference Include="..\..\src\Cnblogs.DashScope.Sdk\Cnblogs.DashScope.Sdk.csproj" />
13+
<ProjectReference Include="..\..\src\Cnblogs.Extensions.AI.DashScope\Cnblogs.Extensions.AI.DashScope.csproj" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

sample/Cnblogs.DashScope.Sample/Program.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Cnblogs.DashScope.Sdk.QWen;
77
using Json.Schema;
88
using Json.Schema.Generation;
9+
using Microsoft.Extensions.AI;
10+
using ChatMessage = Cnblogs.DashScope.Core.ChatMessage;
911

1012
const string apiKey = "sk-**";
1113
var dashScopeClient = new DashScopeClient(apiKey);
@@ -42,6 +44,9 @@
4244
case SampleType.ChatCompletionWithFiles:
4345
await ChatWithFilesAsync();
4446
break;
47+
case SampleType.MicrosoftExtensionsAi:
48+
await ChatWithMicrosoftExtensions();
49+
break;
4550
}
4651

4752
return;
@@ -74,10 +79,11 @@ async Task ChatStreamAsync()
7479
Console.Write("user > ");
7580
var input = Console.ReadLine()!;
7681
history.Add(ChatMessage.User(input));
77-
var stream = dashScopeClient.GetQWenChatStreamAsync(
78-
QWenLlm.QWenMax,
79-
history,
80-
new TextGenerationParameters { IncrementalOutput = true, ResultFormat = ResultFormats.Message });
82+
var stream = dashScopeClient
83+
.GetQWenChatStreamAsync(
84+
QWenLlm.QWenMax,
85+
history,
86+
new TextGenerationParameters { IncrementalOutput = true, ResultFormat = ResultFormats.Message });
8187
var role = string.Empty;
8288
var message = new StringBuilder();
8389
await foreach (var modelResponse in stream)
@@ -164,10 +170,10 @@ async Task ChatWithToolsAsync()
164170
var toolCallMessage = response.Output.Choices![0].Message;
165171
history.Add(toolCallMessage);
166172
Console.WriteLine(
167-
$"{toolCallMessage.Role} > {toolCallMessage.ToolCalls![0].Function!.Name}{toolCallMessage.ToolCalls[0].Function!.Arguments}");
173+
$"{toolCallMessage.Role} > {toolCallMessage.ToolCalls![0].Function.Name}{toolCallMessage.ToolCalls[0].Function.Arguments}");
168174

169175
var toolResponse = GetWeather(
170-
JsonSerializer.Deserialize<WeatherReportParameters>(toolCallMessage.ToolCalls[0].Function!.Arguments!)!);
176+
JsonSerializer.Deserialize<WeatherReportParameters>(toolCallMessage.ToolCalls[0].Function.Arguments!)!);
171177
var toolMessage = ChatMessage.Tool(toolResponse, nameof(GetWeather));
172178
history.Add(toolMessage);
173179
Console.WriteLine($"{toolMessage.Role} > {toolMessage.Content}");
@@ -186,3 +192,10 @@ string GetWeather(WeatherReportParameters parameters)
186192
};
187193
}
188194
}
195+
196+
async Task ChatWithMicrosoftExtensions()
197+
{
198+
var chatClient = dashScopeClient.AsChatClient("qwen-max");
199+
var response = await chatClient.CompleteAsync("你好,很高兴认识你");
200+
Console.WriteLine(JsonSerializer.Serialize(response));
201+
}

sample/Cnblogs.DashScope.Sample/SampleType.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ public enum SampleType
1717
ChatCompletionWithTool,
1818

1919
[Description("Conversation with files")]
20-
ChatCompletionWithFiles
20+
ChatCompletionWithFiles,
21+
22+
[Description("Completion with Microsoft.Extensions.AI")]
23+
MicrosoftExtensionsAi
2124
}

0 commit comments

Comments
 (0)