Skip to content

Commit 13d5a97

Browse files
committed
docs: update sample and README.md for QWen-Long
1 parent e119dbc commit 13d5a97

File tree

7 files changed

+123
-2
lines changed

7 files changed

+123
-2
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class YourService(IDashScopeClient client)
6262
- Image Synthesis - `CreateWanxImageSynthesisTaskAsync()` and `GetWanxImageSynthesisTaskAsync()`
6363
- Image Generation - `CreateWanxImageGenerationTaskAsync()` and `GetWanxImageGenerationTaskAsync()`
6464
- Background Image Generation - `CreateWanxBackgroundGenerationTaskAsync()` and `GetWanxBackgroundGenerationTaskAsync()`
65-
65+
- File API that used by Qwen-Long - `dashScopeClient.UploadFileAsync()` and `dashScopeClient.DeleteFileAsync`
6666

6767
# Examples
6868

@@ -163,3 +163,35 @@ Console.WriteLine(completion.Output.Choice[0].Message.Content);
163163
```
164164

165165
Append the tool calling result with `tool` role, then model will generate answers based on tool calling result.
166+
167+
168+
## QWen-Long with files
169+
170+
Upload file first.
171+
172+
```csharp
173+
var file = new FileInfo("test.txt");
174+
var uploadedFile = await dashScopeClient.UploadFileAsync(file.OpenRead(), file.Name);
175+
```
176+
177+
Using uploaded file id in messages.
178+
179+
```csharp
180+
var history = new List<ChatMessage>
181+
{
182+
new(uploadedFile.Id), // use array for multiple files, e.g. [file1.Id, file2.Id]
183+
new("user", "Summarize the content of file.")
184+
}
185+
var parameters = new TextGenerationParameters()
186+
{
187+
ResultFormat = ResultFormats.Message
188+
};
189+
var completion = await client.GetQWenChatCompletionAsync(QWenLlm.QWenLong, history, parameters);
190+
Console.WriteLine(completion.Output.Choices[0].Message.Content);
191+
```
192+
193+
Delete file if needed
194+
195+
```csharp
196+
var deletionResult = await dashScopeClient.DeleteFileAsync(uploadedFile.Id);
197+
```

README.zh-Hans.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class YourService(IDashScopeClient client)
6262
- 文生图 - `CreateWanxImageSynthesisTaskAsync()` and `GetWanxImageSynthesisTaskAsync()`
6363
- 人像风格重绘 - `CreateWanxImageGenerationTaskAsync()` and `GetWanxImageGenerationTaskAsync()`
6464
- 图像背景生成 - `CreateWanxBackgroundGenerationTaskAsync()` and `GetWanxBackgroundGenerationTaskAsync()`
65+
- 适用于 QWen-Long 的文件 API `dashScopeClient.UploadFileAsync()` and `dashScopeClient.DeleteFileAsync`
6566

6667

6768
# 示例
@@ -159,3 +160,34 @@ Console.WriteLine(completion.Output.Choice[0].Message.Content) // 现在浙江
159160
```
160161

161162
当模型认为应当调用工具时,返回消息中 `ToolCalls` 会提供调用的详情,本地在调用完成后可以把结果以 `tool` 角色返回。
163+
164+
## 上传文件(QWen-Long)
165+
166+
需要先提前将文件上传到 DashScope 来获得 Id。
167+
168+
```csharp
169+
var file = new FileInfo("test.txt");
170+
var uploadedFile = await dashScopeClient.UploadFileAsync(file.OpenRead(), file.Name);
171+
```
172+
173+
使用文件 Id 初始化一个消息,内部会转换成 system 角色的一个文件引用。
174+
175+
```csharp
176+
var history = new List<ChatMessage>
177+
{
178+
new(uploadedFile.Id), // 多文件情况下可以直接传入文件 Id 数组, 例如:[file1.Id, file2.Id]
179+
new("user", "总结一下文件的内容。")
180+
}
181+
var parameters = new TextGenerationParameters()
182+
{
183+
ResultFormat = ResultFormats.Message
184+
};
185+
var completion = await client.GetQWenChatCompletionAsync(QWenLlm.QWenLong, history, parameters);
186+
Console.WriteLine(completion.Output.Choices[0].Message.Content);
187+
```
188+
189+
如果需要,完成对话后可以使用 API 删除之前上传的文件。
190+
191+
```csharp
192+
var deletionResult = await dashScopeClient.DeleteFileAsync(uploadedFile.Id);
193+
```

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
<ProjectReference Include="..\..\src\Cnblogs.DashScope.Sdk\Cnblogs.DashScope.Sdk.csproj" />
1313
</ItemGroup>
1414

15+
<ItemGroup>
16+
<None Update="test.txt">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
1521
</Project>

sample/Cnblogs.DashScope.Sample/Program.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
case SampleType.ChatCompletionWithTool:
4040
await ChatWithToolsAsync();
4141
break;
42+
case SampleType.ChatCompletionWithFiles:
43+
await ChatWithFilesAsync();
44+
break;
4245
}
4346

4447
return;
@@ -97,6 +100,49 @@ async Task ChatStreamAsync()
97100
// ReSharper disable once FunctionNeverReturns
98101
}
99102

103+
async Task ChatWithFilesAsync()
104+
{
105+
var history = new List<ChatMessage>();
106+
Console.WriteLine("uploading file \"test.txt\" ");
107+
var file = new FileInfo("test.txt");
108+
var uploadedFile = await dashScopeClient.UploadFileAsync(file.OpenRead(), file.Name);
109+
Console.WriteLine("file uploaded, id: " + uploadedFile.Id);
110+
Console.WriteLine();
111+
112+
var fileMessage = new ChatMessage(uploadedFile.Id);
113+
history.Add(fileMessage);
114+
Console.WriteLine("system > " + fileMessage.Content);
115+
var userPrompt = new ChatMessage("user", "该文件的内容是什么");
116+
history.Add(userPrompt);
117+
Console.WriteLine("user > " + userPrompt.Content);
118+
var stream = dashScopeClient.GetQWenChatStreamAsync(
119+
QWenLlm.QWenLong,
120+
history,
121+
new TextGenerationParameters { IncrementalOutput = true, ResultFormat = ResultFormats.Message });
122+
var role = string.Empty;
123+
var message = new StringBuilder();
124+
await foreach (var modelResponse in stream)
125+
{
126+
var chunk = modelResponse.Output.Choices![0];
127+
if (string.IsNullOrEmpty(role) && string.IsNullOrEmpty(chunk.Message.Role) == false)
128+
{
129+
role = chunk.Message.Role;
130+
Console.Write(chunk.Message.Role + " > ");
131+
}
132+
133+
message.Append(chunk.Message.Content);
134+
Console.Write(chunk.Message.Content);
135+
}
136+
137+
Console.WriteLine();
138+
history.Add(new ChatMessage(role, message.ToString()));
139+
140+
Console.WriteLine();
141+
Console.WriteLine("Deleting file by id: " + uploadedFile.Id);
142+
var result = await dashScopeClient.DeleteFileAsync(uploadedFile.Id);
143+
Console.WriteLine("Deletion result: " + result.Deleted);
144+
}
145+
100146
async Task ChatWithToolsAsync()
101147
{
102148
var history = new List<ChatMessage>();

sample/Cnblogs.DashScope.Sample/SampleType.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ public enum SampleType
1414
ChatCompletion,
1515

1616
[Description("Conversation with tools")]
17-
ChatCompletionWithTool
17+
ChatCompletionWithTool,
18+
19+
[Description("Conversation with files")]
20+
ChatCompletionWithFiles
1821
}

sample/Cnblogs.DashScope.Sample/SampleTypeDescriptor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static string GetDescription(this SampleType sampleType)
1010
SampleType.TextCompletionSse => "Simple prompt completion with incremental output",
1111
SampleType.ChatCompletion => "Conversation between user and assistant",
1212
SampleType.ChatCompletionWithTool => "Function call sample",
13+
SampleType.ChatCompletionWithFiles => "File upload sample using qwen-long",
1314
_ => throw new ArgumentOutOfRangeException(nameof(sampleType), sampleType, "Unsupported sample option")
1415
};
1516
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
测试内容。

0 commit comments

Comments
 (0)