11using System ;
22using System . ClientModel ;
33using System . Collections . Generic ;
4- using System . IO ;
54using System . Linq ;
65using System . Text ;
76using System . Threading ;
109using Microsoft . Office . Tools ;
1110using Microsoft . Office . Tools . Ribbon ;
1211using OpenAI . Chat ;
13- using OpenAI . Models ;
1412using Task = System . Threading . Tasks . Task ;
1513using Word = Microsoft . Office . Interop . Word ;
1614
@@ -90,6 +88,7 @@ private void ModelListDropDown_SelectionChanged(object sender, RibbonControlEven
9088 try
9189 {
9290 ThisAddIn . Model = ModelListDropDown . SelectedItem . Label ;
91+ ThisAddIn . ContextLength = RAGControl . GetContextLength ( ThisAddIn . Model ) ;
9392 UpdateCheckbox ( ) ;
9493 }
9594 catch ( Exception ex )
@@ -178,19 +177,27 @@ private async void WritingToolsGallery_ButtonClick(object sender, RibbonControlE
178177 private static async Task ReviewButton_Click ( )
179178 {
180179 Word . Paragraphs paragraphs = Globals . ThisAddIn . Application . ActiveDocument . Paragraphs ;
181-
182180 const string prompt = "As an expert writing assistant, suggest specific improvements to the paragraph, focusing on clarity, coherence, structure, grammar, and overall effectiveness. Ensure that your suggestions are detailed and aimed at improving the paragraph within the context of the entire Document." ;
181+
182+ bool hasCommented = false ;
183183 if ( Globals . ThisAddIn . Application . Selection . End - Globals . ThisAddIn . Application . Selection . Start > 0 )
184184 {
185185 await AddComment ( Globals . ThisAddIn . Application . ActiveDocument . Comments , Globals . ThisAddIn . Application . Selection . Range , Review ( paragraphs , Globals . ThisAddIn . Application . Selection . Range , prompt ) ) ;
186+ hasCommented = true ;
186187 }
187188 else
188189 {
189190 foreach ( Word . Paragraph p in paragraphs )
190191 // It isn't a paragraph if it doesn't contain a full stop.
191192 if ( p . Range . Text . Contains ( '.' ) )
193+ {
192194 await AddComment ( Globals . ThisAddIn . Application . ActiveDocument . Comments , p . Range , Review ( paragraphs , p . Range , prompt ) ) ;
195+ hasCommented = true ;
196+ }
197+
193198 }
199+ if ( ! hasCommented )
200+ MessageBox . Show ( "Review complete!" , "Action Completed" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
194201 }
195202
196203 private static async Task ProofreadButton_Click ( )
@@ -215,7 +222,11 @@ private static async Task AnalyzeText(string systemPrompt, string userPrompt)
215222 var range = ( selectionRange . End - selectionRange . Start > 0 ) ? selectionRange : throw new InvalidRangeException ( "No text is selected for analysis!" ) ;
216223
217224 ChatClient client = new ChatClient ( ThisAddIn . Model , ThisAddIn . ApiKey , ThisAddIn . ClientOptions ) ;
218- var streamingAnswer = client . CompleteChatStreamingAsync ( new SystemChatMessage ( systemPrompt ) , new UserChatMessage ( @$ "{ userPrompt } : { range . Text } ") ) ;
225+ var streamingAnswer = client . CompleteChatStreamingAsync (
226+ new List < ChatMessage > ( ) { new SystemChatMessage ( systemPrompt ) , new UserChatMessage ( @$ "{ userPrompt } : { range . Text } ") } ,
227+ new ChatCompletionOptions ( ) { MaxTokens = ThisAddIn . ContextLength } ,
228+ ThisAddIn . CancellationTokenSource . Token
229+ ) ;
219230 range . Delete ( ) ;
220231 await AddStreamingContentToRange ( streamingAnswer , range ) ;
221232 Globals . ThisAddIn . Application . Selection . SetRange ( range . Start , range . End ) ;
@@ -250,14 +261,6 @@ private void UpdateCheckbox()
250261 DefaultCheckBox . Checked = ( Properties . Settings . Default . DefaultModel == ThisAddIn . Model ) ;
251262 }
252263
253- public static List < string > GetModels ( ModelClient model )
254- {
255- List < string > models = new List < string > ( ) ;
256- foreach ( OpenAIModelInfo info in model . GetModels ( ) . Value )
257- models . Add ( info . Id ) ;
258- return models ;
259- }
260-
261264 public static async Task AddComment ( Word . Comments comments , Word . Range range , AsyncCollectionResult < StreamingChatCompletionUpdate > streamingContent )
262265 {
263266 Word . Comment c = comments . Add ( range , string . Empty ) ;
@@ -286,31 +289,9 @@ await Task.Run(async () =>
286289
287290 private static AsyncCollectionResult < StreamingChatCompletionUpdate > Review ( Word . Paragraphs context , Word . Range p , string prompt )
288291 {
289- const int promptWordLen = 50 ;
290292 var docRange = Globals . ThisAddIn . Application . ActiveDocument . Range ( ) ;
291- int documentLength = ( int ) ( docRange . Words . Count * 1.3 ) ;
292- string allText = docRange . Text ;
293-
294- if ( ThisAddIn . ContextLength - documentLength - p . Words . Count - promptWordLen - ( int ) ( ThisAddIn . ContextLength * 0.3 ) < 0 )
295- {
296- HyperVectorDB . HyperVectorDB DB = new HyperVectorDB . HyperVectorDB ( ThisAddIn . Embedder , Path . GetTempPath ( ) ) ;
297- foreach ( Word . Paragraph paragraph in context )
298- {
299- if ( paragraph . Range == p ) continue ;
300- var chunks = RAGControl . SplitString ( paragraph . Range . Text , RAGControl . CHUNK_LEN ) ;
301- foreach ( var chunk in chunks )
302- DB . IndexDocument ( chunk ) ;
303- }
304- var result = DB . QueryCosineSimilarity ( p . Text , 3 ) ;
305- StringBuilder ragContext = new StringBuilder ( ) ;
306- foreach ( var doc in result . Documents )
307- ragContext . AppendLine ( doc . DocumentString ) ;
308- allText = ragContext . ToString ( ) ;
309- }
310- UserChatMessage userPrompt = new UserChatMessage ( $@ "Document Content: ""{ RAGControl . SubstringTokens ( allText , ( int ) ( ThisAddIn . ContextLength * 0.4 ) ) } ""{ Environment . NewLine } RAG Context: ""{ ThisAddIn . RagControl . GetRAGContext ( p . Text , ( int ) ( ThisAddIn . ContextLength * 0.3 ) ) } ""{ Environment . NewLine } Please review the following paragraph extracted from the Document: ""{ RAGControl . SubstringTokens ( p . Text , ( int ) ( ThisAddIn . ContextLength * 0.2 ) ) } ""{ Environment . NewLine } { prompt } ") ;
311-
312- ChatClient client = new ChatClient ( ThisAddIn . Model , ThisAddIn . ApiKey , ThisAddIn . ClientOptions ) ;
313- return client . CompleteChatStreamingAsync ( new List < ChatMessage > { SystemPrompt , userPrompt } , null , ThisAddIn . CancellationTokenSource . Token ) ;
293+ UserChatMessage commentPrompt = new UserChatMessage ( $@ "Please review the following paragraph extracted from the Document: ""{ RAGControl . SubstringTokens ( p . Text , ( int ) ( ThisAddIn . ContextLength * 0.2 ) ) } ""{ Environment . NewLine } { prompt } ") ;
294+ return RAGControl . AskQuestion ( SystemPrompt , new List < UserChatMessage > { commentPrompt } , docRange ) ;
314295 }
315296 }
316297}
0 commit comments