|
39 | 39 | case SampleType.ChatCompletionWithTool:
|
40 | 40 | await ChatWithToolsAsync();
|
41 | 41 | break;
|
| 42 | + case SampleType.ChatCompletionWithFiles: |
| 43 | + await ChatWithFilesAsync(); |
| 44 | + break; |
42 | 45 | }
|
43 | 46 |
|
44 | 47 | return;
|
@@ -97,6 +100,49 @@ async Task ChatStreamAsync()
|
97 | 100 | // ReSharper disable once FunctionNeverReturns
|
98 | 101 | }
|
99 | 102 |
|
| 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 | + |
100 | 146 | async Task ChatWithToolsAsync()
|
101 | 147 | {
|
102 | 148 | var history = new List<ChatMessage>();
|
|
0 commit comments