-
Notifications
You must be signed in to change notification settings - Fork 1
/
RAGControl.cs
473 lines (422 loc) · 18.9 KB
/
RAGControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System;
using System.ClientModel;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenAI.Chat;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter;
using UglyToad.PdfPig.Exceptions;
using Task = System.Threading.Tasks.Task;
using Word = Microsoft.Office.Interop.Word;
namespace TextForge
{
public partial class RAGControl : UserControl
{
// Public
public static readonly int CHUNK_LEN = CommonUtils.TokensToCharCount(256);
// Private
private ToolTip _fileToolTip = new ToolTip();
private Queue<string> _removalQueue = new Queue<string>();
private ConcurrentDictionary<int, int> _indexFileCount = new ConcurrentDictionary<int, int>();
private BindingList<KeyValuePair<string, string>> _fileList; // Use KeyValuePair for label and filename
private HyperVectorDB.HyperVectorDB _db;
private bool _isIndexing;
private readonly object progressBarLock = new object();
private static readonly CultureLocalizationHelper _cultureHelper = new CultureLocalizationHelper("TextForge.RAGControl", typeof(RAGControl).Assembly);
public RAGControl()
{
try
{
InitializeComponent();
this.Load += (s, e) =>
{
// Run the background task to initialize BindingList and FileListBox
Task.Run(() => InitializeRAGControl());
};
}
catch (Exception ex)
{
CommonUtils.DisplayError(ex);
}
}
private void InitializeRAGControl()
{
FileListBox.Invoke(new Action(() =>
{
_fileList = new BindingList<KeyValuePair<string, string>>();
FileListBox.DataSource = _fileList;
FileListBox.DisplayMember = "Key"; // Display the label (Key)
FileListBox.ValueMember = "Value"; // Internally use the filename (Value)
_fileToolTip.ShowAlways = true; // Always show the tooltip
// Attach MouseMove event to FileListBox to display the full path in the tooltip
FileListBox.MouseMove += FileListBox_MouseMove;
}));
lock (Forge.InitializeDoor)
{
_db = new HyperVectorDB.HyperVectorDB(ThisAddIn.Embedder, Path.GetTempPath());
}
}
private async void AddButton_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Title = _cultureHelper.GetLocalizedString("[AddButton_Click] OpenFileDialog #1 Title"), Filter = "PDF files (*.pdf)|*.pdf", Multiselect = true })
{
List<string> filesToIndex = new List<string>();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string fileName in openFileDialog.FileNames)
{
if (!_fileList.Any(file => file.Value == fileName))
{
_fileList.Add(new KeyValuePair<string, string>("📄 " + Path.GetFileName(fileName), fileName));
filesToIndex.Add(fileName);
if (!RemoveButton.Enabled)
{
this.Invoke((MethodInvoker)delegate
{
RemoveButton.Enabled = true;
});
}
}
}
ChangeProgressBarVisibility(true);
{
int dictCount = _indexFileCount.Count;
_indexFileCount.TryAdd(dictCount, filesToIndex.Count);
lock (progressBarLock)
{
SetProgressBarValue(GetProgressBarValue() / (dictCount + 1));
}
foreach (var filePath in filesToIndex)
{
await IndexDocumentAsync(filePath);
}
int temp;
_indexFileCount.TryRemove(dictCount, out temp);
}
await ChangeProgressBarVisibilityAfterSleep(5, false);
}
}
}
catch (Exception ex)
{
CommonUtils.DisplayError(ex);
}
}
private void RemoveButton_Click(object sender, EventArgs e)
{
try
{
string selectedDocument = FileListBox.SelectedItem.ToString();
if (_isIndexing)
{
if (!_removalQueue.Contains(selectedDocument))
_removalQueue.Enqueue(selectedDocument);
}
else
{
RemoveDocument(selectedDocument);
}
_fileList.RemoveAt(FileListBox.SelectedIndex);
AutoHideRemoveButton();
} catch (Exception ex)
{
CommonUtils.DisplayError(ex);
}
}
private void FileListBox_MouseMove(object sender, MouseEventArgs e)
{
// Get the index of the item under the mouse cursor
int index = FileListBox.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
// Get the KeyValuePair (label, file path) for the item
var item = (KeyValuePair<string, string>)FileListBox.Items[index];
// Show the file path in the tooltip
_fileToolTip.SetToolTip(FileListBox, item.Value);
}
else
{
// Clear the tooltip if not hovering over an item
_fileToolTip.SetToolTip(FileListBox, string.Empty);
}
}
private void AutoHideRemoveButton()
{
if (_fileList.Count == 0)
RemoveButton.Enabled = false;
}
private async Task IndexDocumentAsync(string filePath)
{
IEnumerable<string> fileContent;
try
{
fileContent = await ReadPdfFileAsync(filePath, CHUNK_LEN);
} catch
{
this.Invoke((MethodInvoker)delegate
{
// Find and remove the file entry from _fileList based on the internal filename (filePath)
var fileEntry = _fileList.FirstOrDefault(file => file.Value == filePath);
if (fileEntry.Key != null) // If the file entry is found
{
_fileList.Remove(fileEntry);
}
// Automatically hide the remove button if there are no more files in the list
AutoHideRemoveButton();
});
throw;
}
_db.CreateIndex(filePath);
await Task.Run(() => {
_isIndexing = true;
foreach (var content in fileContent)
AddDocument(filePath, content);
this.Invoke((MethodInvoker)delegate
{
UpdateProgressBar(1);
});
_isIndexing = false;
// Process any queued removal requests
ProcessRemovalQueue();
});
}
private async Task ChangeProgressBarVisibilityAfterSleep(int seconds, bool val)
{
await Task.Delay(seconds * 1000);
this.Invoke((MethodInvoker)delegate
{
ChangeProgressBarVisibility(val);
ResetProgressBar();
});
}
private void ChangeProgressBarVisibility(bool val)
{
this.progressBar1.Visible = val;
}
private void ResetProgressBar()
{
SetProgressBarValue(0);
}
private float GetProgressBarValue()
{
return this.progressBar1.Value / ((float) this.progressBar1.Maximum);
}
private void SetProgressBarValue(float val)
{
this.progressBar1.Value = (int)(val * this.progressBar1.Maximum);
}
private void UpdateProgressBar(float val)
{
lock (progressBarLock)
{
int fileCount = GetIndexFileCount();
// Clipping
int maxProgress = this.progressBar1.Maximum;
int incrementVal = (int)((val * maxProgress) / fileCount);
if (incrementVal + this.progressBar1.Value > maxProgress)
this.progressBar1.Value = maxProgress;
else
this.progressBar1.Value += incrementVal;
}
}
private int GetIndexFileCount()
{
int fileCount = 0;
foreach (var count in _indexFileCount)
fileCount += count.Value;
return fileCount;
}
private void ProcessRemovalQueue()
{
int initialQueueCount = _removalQueue.Count;
for (int i = 0; i < initialQueueCount; i++)
{
string documentToRemove = _removalQueue.Dequeue();
// Check if the document (by filename) exists in the _fileList
var fileEntry = _fileList.FirstOrDefault(file => file.Value == documentToRemove);
// If the document is found, attempt to remove it
if (fileEntry.Key != null)
{
if (!RemoveDocument(documentToRemove)) // Try removing the document
{
// If removal fails, re-enqueue the document and stop processing
_removalQueue.Enqueue(documentToRemove);
break;
}
}
}
}
private bool AddDocument(string filePath, string content)
{
return _db.IndexDocument(filePath, content);
}
private bool RemoveDocument(string filePath)
{
return _db.DeleteIndex(filePath);
}
public static async Task<IEnumerable<string>> ReadPdfFileAsync(string filePath, int chunkLen)
{
return await Task.Run(() =>
{
List<string> chunks = new List<string>();
try
{
PdfDocument doc;
try { doc = PdfDocument.Open(filePath); }
catch (PdfDocumentEncryptedException) { throw new ArgumentException(); }
try { IteratePdfFile(ref doc, ref chunks, chunkLen); }
finally { doc.Dispose(); }
}
catch (ArgumentException)
{
PasswordPrompt passwordDialog = new PasswordPrompt();
if (passwordDialog.ShowDialog() == DialogResult.OK)
{
PdfDocument unlockedDoc = PdfDocument.Open(filePath, new ParsingOptions { Password = passwordDialog.Password });
try { IteratePdfFile(ref unlockedDoc, ref chunks, chunkLen); }
finally { unlockedDoc.Dispose(); }
}
else
{
throw new InvalidDataException(_cultureHelper.GetLocalizedString("[ReadPdfFileAsync] InvalidDataException #1"));
}
}
return chunks;
});
}
private static void IteratePdfFile(ref PdfDocument document, ref List<string> chunks, int chunkLen)
{
IterateInnerPdfFile(ref document, ref chunks, chunkLen);
IReadOnlyList<EmbeddedFile> embeddedFiles;
if (document.Advanced.TryGetEmbeddedFiles(out embeddedFiles))
{
foreach (var embeddedFile in embeddedFiles)
{
if (embeddedFile.Name.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
{
try
{
PdfDocument embedDoc;
try { embedDoc = PdfDocument.Open(embeddedFile.Bytes.ToArray()); }
catch (PdfDocumentEncryptedException) { throw new ArgumentException(); }
try { IteratePdfFile(ref embedDoc, ref chunks, chunkLen); }
finally { embedDoc.Dispose(); }
} catch (ArgumentException)
{
PasswordPrompt passwordDialog = new PasswordPrompt();
if (passwordDialog.ShowDialog() == DialogResult.OK)
{
PdfDocument unlockedDoc = PdfDocument.Open(embeddedFile.Bytes.ToArray(), new ParsingOptions { Password = passwordDialog.Password });
try { IteratePdfFile(ref unlockedDoc, ref chunks, chunkLen); }
finally { unlockedDoc.Dispose(); }
}
else
{
throw new InvalidDataException(_cultureHelper.GetLocalizedString("[ReadPdfFileAsync] InvalidDataException #1"));
}
}
}
}
}
}
private static void IterateInnerPdfFile(ref PdfDocument doc, ref List<string> chunks, int chunkLen)
{
foreach (var page in doc.GetPages())
{
var blocks = DocstrumBoundingBoxes.Instance.GetBlocks(page.GetWords());
foreach (var block in blocks)
chunks.AddRange(CommonUtils.SplitString(block.Text, chunkLen));
}
}
public string GetRAGContext(string query, int maxTokens)
{
if (_fileList.Count == 0) return string.Empty;
var result = _db.QueryCosineSimilarity(query, _fileList.Count * 10); // 10 results per file
StringBuilder ragContext = new StringBuilder();
foreach (var document in result.Documents)
ragContext.AppendLine(document.DocumentString);
return CommonUtils.SubstringTokens(ragContext.ToString(), maxTokens);
}
// UTILS
public static AsyncCollectionResult<StreamingChatCompletionUpdate> AskQuestion(SystemChatMessage systemPrompt, IEnumerable<ChatMessage> messages, Word.Range context, Word.Document doc = null)
{
if (doc == null)
doc = context.Document;
string document = context.Text;
int userPromptLen = GetUserPromptLen(messages);
ChatMessage lastUserPrompt = messages.Last();
var constraints = RAGControl.OptimizeConstraint(
0.8f,
ThisAddIn.ContextLength,
CommonUtils.CharToTokenCount(systemPrompt.Content[0].Text.Length + userPromptLen),
CommonUtils.CharToTokenCount(document.Length)
);
if (constraints["document_content_rag"] == 1f)
document = RAGControl.GetWordDocumentAsRAG(lastUserPrompt.Content[0].Text, context);
string ragQuery =
(constraints["rag_context"] == 0f) ? string.Empty : ThisAddIn.AllTaskPanes[doc].Item3.GetRAGContext(lastUserPrompt.Content[0].Text, (int)(ThisAddIn.ContextLength * constraints["rag_context"]));
List<ChatMessage> chatHistory = new List<ChatMessage>()
{
systemPrompt,
new UserChatMessage($@"Document Content: ""{CommonUtils.SubstringTokens(document, (int)(ThisAddIn.ContextLength * constraints["document_content"]))}""")
};
if (ragQuery != string.Empty)
chatHistory.Add(new UserChatMessage($@"RAG Context: ""{ragQuery}"""));
chatHistory.AddRange(messages);
ChatClient client = new ChatClient(ThisAddIn.Model, new ApiKeyCredential(ThisAddIn.ApiKey), ThisAddIn.ClientOptions);
// https://github.com/ollama/ollama/pull/6504
return client.CompleteChatStreamingAsync(
chatHistory,
new ChatCompletionOptions() { MaxOutputTokenCount = ThisAddIn.ContextLength },
ThisAddIn.CancellationTokenSource.Token
);
}
private static int GetUserPromptLen(IEnumerable<ChatMessage> messageList)
{
int userPromptLen = 0;
foreach (var message in messageList)
userPromptLen += message.Content[0].Text.Length;
return userPromptLen;
}
public static Dictionary<string, float> OptimizeConstraint(float maxPercentage, int contextLength, int promptTokenLen, int documentContentTokenLen)
{
Dictionary<string, float> constraintPairs = new Dictionary<string, float>();
if (promptTokenLen >= contextLength * 0.9)
{
constraintPairs["rag_context"] = 0f;
constraintPairs["document_content"] = (float)(maxPercentage * 0.1);
constraintPairs["document_content_rag"] = (documentContentTokenLen > contextLength * maxPercentage * 0.1) ? 1f : 0f;
}
else
{
float promptPercentage = (float)promptTokenLen / (float)contextLength;
constraintPairs["rag_context"] = (float)( (1 - promptPercentage) * maxPercentage * 0.3);
constraintPairs["document_content"] = (float)((1 - promptPercentage) * maxPercentage * 0.7);
constraintPairs["document_content_rag"] = (documentContentTokenLen > contextLength * (1 - promptPercentage) * maxPercentage * 0.7) ? 1f : 0f;
}
return constraintPairs;
}
public static string GetWordDocumentAsRAG(string query, Word.Range context)
{
// Get RAG context
HyperVectorDB.HyperVectorDB DB = new HyperVectorDB.HyperVectorDB(ThisAddIn.Embedder, Path.GetTempPath());
var chunks = CommonUtils.SplitString(context.Text, CHUNK_LEN);
foreach (var chunk in chunks)
DB.IndexDocument(chunk);
var result = DB.QueryCosineSimilarity(query, CommonUtils.GetWordPageCount() * 3);
StringBuilder ragContext = new StringBuilder();
foreach (var doc in result.Documents)
ragContext.AppendLine(doc.DocumentString);
return ragContext.ToString();
}
}
}