|
2 | 2 | using System.ClientModel; |
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.Linq; |
5 | | -using System.Reflection; |
6 | 5 | using System.Text; |
7 | 6 | using System.Threading; |
8 | 7 | using System.Threading.Tasks; |
@@ -197,7 +196,13 @@ private static async Task ReviewButton_Click() |
197 | 196 | if (Globals.ThisAddIn.Application.Selection.End - Globals.ThisAddIn.Application.Selection.Start > 0) |
198 | 197 | { |
199 | 198 | var selectionRange = CommonUtils.GetSelectionRange(); |
200 | | - await CommentHandler.AddComment(CommonUtils.GetComments(), selectionRange, Review(paragraphs, selectionRange, prompt)); |
| 199 | + try |
| 200 | + { |
| 201 | + await CommentHandler.AddComment(CommonUtils.GetComments(), selectionRange, Review(paragraphs, selectionRange, prompt)); |
| 202 | + } catch (OperationCanceledException ex) |
| 203 | + { |
| 204 | + CommonUtils.DisplayWarning(ex); |
| 205 | + } |
201 | 206 | hasCommented = true; |
202 | 207 | } |
203 | 208 | else |
@@ -235,14 +240,21 @@ private static async Task AnalyzeText(string systemPrompt, string userPrompt) |
235 | 240 | var selectionRange = Globals.ThisAddIn.Application.Selection.Range; |
236 | 241 | var range = (selectionRange.End - selectionRange.Start > 0) ? selectionRange : throw new InvalidRangeException("No text is selected for analysis!"); |
237 | 242 |
|
238 | | - ChatClient client = new ChatClient(ThisAddIn.Model, ThisAddIn.ApiKey, ThisAddIn.ClientOptions); |
| 243 | + ChatClient client = new ChatClient(ThisAddIn.Model, new ApiKeyCredential(ThisAddIn.ApiKey), ThisAddIn.ClientOptions); |
239 | 244 | var streamingAnswer = client.CompleteChatStreamingAsync( |
240 | 245 | new List<ChatMessage>() { new SystemChatMessage(systemPrompt), new UserChatMessage(@$"{userPrompt}: {range.Text}") }, |
241 | | - new ChatCompletionOptions() { MaxTokens = ThisAddIn.ContextLength }, |
| 246 | + new ChatCompletionOptions() { MaxOutputTokenCount = ThisAddIn.ContextLength }, |
242 | 247 | ThisAddIn.CancellationTokenSource.Token |
243 | 248 | ); |
| 249 | + |
244 | 250 | range.Delete(); |
245 | | - await AddStreamingContentToRange(streamingAnswer, range); |
| 251 | + try |
| 252 | + { |
| 253 | + await AddStreamingContentToRange(streamingAnswer, range); |
| 254 | + } catch (OperationCanceledException ex) |
| 255 | + { |
| 256 | + CommonUtils.DisplayWarning(ex); |
| 257 | + } |
246 | 258 | Globals.ThisAddIn.Application.Selection.SetRange(range.Start, range.End); |
247 | 259 | } |
248 | 260 |
|
|
0 commit comments