Skip to content

Commit 53d0141

Browse files
committed
Bug fixes
1 parent 9d7a6a3 commit 53d0141

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

Forge.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private static AsyncCollectionResult<StreamingChatCompletionUpdate> Review(Word.
307307
ragContext.AppendLine(doc.DocumentString);
308308
allText = ragContext.ToString();
309309
}
310-
UserChatMessage userPrompt = new UserChatMessage($@"Document Content: ""{RAGControl.SubstringWithoutBounds(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.SubstringWithoutBounds(p.Text, (int)(ThisAddIn.ContextLength * 0.2))}""{Environment.NewLine}{prompt}");
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}");
311311

312312
ChatClient client = new ChatClient(ThisAddIn.Model, ThisAddIn.ApiKey, ThisAddIn.ClientOptions);
313313
return client.CompleteChatStreamingAsync(new List<ChatMessage> { SystemPrompt, userPrompt }, null, ThisAddIn.CancellationTokenSource.Token);

GenerateUserControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public static AsyncCollectionResult<StreamingChatCompletionUpdate> AskQuestion(S
6767
string document = (Globals.ThisAddIn.Application.ActiveDocument.Words.Count * 1.4 > ThisAddIn.ContextLength * 0.4) ? RAGControl.GetWordDocumentAsRAG(prompt, context) : context.Text;
6868

6969
// 0.1 of context length leftover to account for UserChatMessage and other stuff
70-
SystemChatMessage systemPromptBounded = new SystemChatMessage(RAGControl.SubstringWithoutBounds(systemPrompt.Content[0].Text, (int)(ThisAddIn.ContextLength * 0.1)));
71-
UserChatMessage fullPrompt = new UserChatMessage($@"{RAGControl.SubstringWithoutBounds(prompt, (int) (ThisAddIn.ContextLength * 0.2))}{Environment.NewLine}RAG Context: ""{ThisAddIn.RagControl.GetRAGContext(prompt, (int)(ThisAddIn.ContextLength * 0.2))}""{Environment.NewLine}Document Content: ""{RAGControl.SubstringWithoutBounds(document, (int)(ThisAddIn.ContextLength * 0.4))}""");
70+
SystemChatMessage systemPromptBounded = new SystemChatMessage(RAGControl.SubstringTokens(systemPrompt.Content[0].Text, (int)(ThisAddIn.ContextLength * 0.1)));
71+
UserChatMessage fullPrompt = new UserChatMessage($@"{RAGControl.SubstringTokens(prompt, (int) (ThisAddIn.ContextLength * 0.2))}{Environment.NewLine}RAG Context: ""{ThisAddIn.RagControl.GetRAGContext(prompt, (int)(ThisAddIn.ContextLength * 0.2))}""{Environment.NewLine}Document Content: ""{RAGControl.SubstringTokens(document, (int)(ThisAddIn.ContextLength * 0.4))}""");
7272

7373
ChatClient client = new ChatClient(ThisAddIn.Model, ThisAddIn.ApiKey, ThisAddIn.ClientOptions);
7474
return client.CompleteChatStreamingAsync(new List<ChatMessage>() { systemPromptBounded, fullPrompt }, null, ThisAddIn.CancellationTokenSource.Token);

OfficeAddInSetup/OfficeAddInSetup.vdproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@
23452345
}
23462346
"{3C67513D-01DD-4637-8A68-80971EB9504F}:_AF016B7856C74F08A06EC9F3AA46C574"
23472347
{
2348-
"DefaultLocation" = "8:[ProgramFilesFolder][Manufacturer]\\[ProductName]"
2348+
"DefaultLocation" = "8:[ProgramFiles64Folder][Manufacturer]\\[ProductName]"
23492349
"Name" = "8:#1925"
23502350
"AlwaysCreate" = "11:FALSE"
23512351
"Condition" = "8:"
@@ -2406,7 +2406,7 @@
24062406
"Name" = "8:Microsoft Visual Studio"
24072407
"ProductName" = "8:TextCraft"
24082408
"ProductCode" = "8:{9A35238E-CCC7-4433-9616-E5DC353A48D3}"
2409-
"PackageCode" = "8:{863A915E-8A6F-4EC1-BBCC-4CAB091463A1}"
2409+
"PackageCode" = "8:{8E29852C-36E0-4844-9D3B-0F51BD125EDA}"
24102410
"UpgradeCode" = "8:{E36CBC33-F0C8-472A-99B5-D882A25CC883}"
24112411
"AspNetVersion" = "8:"
24122412
"RestartWWWService" = "11:FALSE"
@@ -2427,7 +2427,7 @@
24272427
"ARPIconIndex" = "3:0"
24282428
"SearchPath" = "8:"
24292429
"UseSystemSearchPath" = "11:TRUE"
2430-
"TargetPlatform" = "3:0"
2430+
"TargetPlatform" = "3:1"
24312431
"PreBuildEvent" = "8:"
24322432
"PostBuildEvent" = "8:"
24332433
"RunPostBuildEvent" = "3:0"

RAGControl.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public partial class RAGControl : UserControl
2222
private HyperVectorDB.HyperVectorDB _db;
2323
private bool _isIndexing;
2424

25-
public const int CHUNK_LEN = 256;
25+
public static readonly int CHUNK_LEN = TokensToCharCount(256);
2626

2727
public RAGControl()
2828
{
@@ -209,11 +209,21 @@ public static List<string> SplitString(string str, int chunkSize)
209209
return result;
210210
}
211211

212-
public static string SubstringWithoutBounds(string text, int maxLen)
212+
public static string SubstringTokens(string text, int maxTokens)
213+
{
214+
return SubstringWithoutBounds(text, TokensToCharCount(maxTokens));
215+
}
216+
217+
private static string SubstringWithoutBounds(string text, int maxLen)
213218
{
214219
return (maxLen >= text.Length) ? text : text.Substring(0, maxLen);
215220
}
216221

222+
public static int TokensToCharCount(int tokenCount)
223+
{
224+
return tokenCount * 4; // https://platform.openai.com/tokenizer
225+
}
226+
217227
public static string GetWordDocumentAsRAG(string query, Word.Range context)
218228
{
219229
// Get RAG context

ThisAddIn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private async void Document_CommentsEventHandler(Word.Selection selection)
162162
chatHistory.Add(Forge.SystemPrompt);
163163

164164
string documentText = (this.Application.ActiveDocument.Words.Count * 1.2 > _contextLength * 0.4) ? RAGControl.GetWordDocumentAsRAG(c.Range.Text, this.Application.ActiveDocument.Range()) : this.Application.ActiveDocument.Range().Text;
165-
chatHistory.Add(new UserChatMessage($@"Document Content: ""{RAGControl.SubstringWithoutBounds(documentText, (int)(_contextLength * 0.4))}""{Environment.NewLine}Rag Context: {_ragControl.GetRAGContext(c.Range.Text, (int)(ContextLength * 0.2))}Please review the following paragraph extracted from the Document: ""{c.Range.Text}""{Environment.NewLine}Based on the previous AI comments, suggest additional 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."));
165+
chatHistory.Add(new UserChatMessage($@"Document Content: ""{RAGControl.SubstringTokens(documentText, (int)(_contextLength * 0.4))}""{Environment.NewLine}Rag Context: {_ragControl.GetRAGContext(c.Range.Text, (int)(ContextLength * 0.2))}Please review the following paragraph extracted from the Document: ""{c.Range.Text}""{Environment.NewLine}Based on the previous AI comments, suggest additional 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."));
166166
chatHistory.Add(new AssistantChatMessage(c.Range.Text));
167167

168168
for (int i = 1; i <= c.Replies.Count; i++)

0 commit comments

Comments
 (0)