-
Notifications
You must be signed in to change notification settings - Fork 0
/
rag.go
499 lines (411 loc) · 12 KB
/
rag.go
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package main
import (
"cmp"
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
"slices"
"strconv"
"sync"
"time"
"github.com/philippgille/chromem-go"
)
type rag struct {
vectordb *chromem.DB
convoLLM llm
genTitleLLM llm
embedder embedder
chats []chat
}
const (
ragResultsCount = 20
ragSimiliarityThreshold = 0.5
ragNeededCount = 10
chunkSize = 500 // characters per chunk
chunkOverlap = 50 // overlap between chunks
)
func generateSessionTitle(ctx context.Context, llm llm, chats []chat) (string, error) {
cs := []chat{
{
Role: roleSystem,
Content: `
Generate ONE line containing ONLY the title. No markdown, no quotes, no explanations.
Rules for the title:
1. EXACTLY 3-6 words
2. NO punctuation marks or special characters
3. NO formatting symbols or markdown
4. Start with action verb or topic noun
5. Use simple everyday words
6. NO technical terms unless absolutely necessary
Examples of good titles:
Building Smart Home Network
Learn Python Programming Basics
Planning Family Summer Vacation
Bad titles (don't do these):
- "Setting up Docker containers" (has quotes)
* Technical Infrastructure Review (has bullet point)
Implementation of ML Models (too technical)
This is a very long title about programming (too many words)
`,
},
}
cs = append(cs, chats...)
cs = append(cs, chat{
Role: roleUser,
Content: `
Based on this conversation, create a clear and concise title that captures its main focus. The title should be immediately understandable to someone new to the discussion.
`,
})
slog.Info("Gen Title Prompt", "chats", cs)
res := llm.chat(ctx, cs)
if res.err != nil {
return "", res.err
}
return res.content, nil
}
func ragSystemPrompt(docs []chromem.Result) string {
knowledge := ""
for _, doc := range docs {
filename := ""
if name, ok := doc.Metadata["filename"]; ok {
filename = "[" + name + "]"
}
knowledge += "\n---\n" + filename + "\n" + doc.Content + "\n"
}
return `
I am an AI assistant who deeply understands and embodies this knowledge:
` + knowledge + `
GUIDELINES:
1. Speak naturally as if this knowledge is your own experience and expertise
2. Never use phrases like "based on documents", "according to", "from the documents", or similar references
3. You can expand the conversation with relevant external knowledge
4. Answer directly and confidently, as if you're sharing your own knowledge
5. Be conversational and engaging
RESPONSE FORMAT:
- First provide your complete answer
- Then, if and only if you used specific information from the provided documents, add:
* Start a new line
* Add "Sources: " followed by the relevant filenames in square brackets
* Example: "Sources: [file1.txt] [file2.md]"
- If you didn't use any specific information from the documents, do not add a Sources line at all`
}
func chunkDocument(doc chromem.Document) []chromem.Document {
content := doc.Content
var chunks []chromem.Document
if len(content) <= chunkSize {
return []chromem.Document{doc}
}
for i := 0; i < len(content); i += chunkSize - chunkOverlap {
end := i + chunkSize
if end > len(content) {
end = len(content)
}
chunk := chromem.Document{
ID: fmt.Sprintf("%s-chunk-%d", doc.ID, len(chunks)),
Content: content[i:end],
Metadata: map[string]string{
"filename": doc.Metadata["filename"],
"originalID": doc.ID,
"chunkIndex": fmt.Sprintf("%d", len(chunks)),
},
}
chunks = append(chunks, chunk)
if end == len(content) {
break
}
}
return chunks
}
func newRAG(vectordb *chromem.DB, convoLLM, genTitleLLM llm, embedder embedder) *rag {
return &rag{
vectordb: vectordb,
convoLLM: convoLLM,
genTitleLLM: genTitleLLM,
embedder: embedder,
}
}
func (r *rag) clearChats() {
r.chats = nil
}
func mergeChunks(docs []chromem.Result) []chromem.Result {
// Group chunks by originalID
chunkGroups := make(map[string][]chromem.Result)
for _, doc := range docs {
originalID := doc.Metadata["originalID"]
if originalID == "" {
originalID = doc.ID // Handle non-chunked documents
}
chunkGroups[originalID] = append(chunkGroups[originalID], doc)
}
// Merge chunks and compute average similarity
var mergedDocs []chromem.Result
for _, chunks := range chunkGroups {
if len(chunks) == 1 {
mergedDocs = append(mergedDocs, chunks[0])
continue
}
// Sort chunks by chunkIndex
slices.SortFunc(chunks, func(a, b chromem.Result) int {
aCI, _ := strconv.Atoi(a.Metadata["chunkIndex"])
bCI, _ := strconv.Atoi(b.Metadata["chunkIndex"])
return cmp.Compare(aCI, bCI)
})
// Merge content and compute average similarity
var totalSim float32
var mergedContent string
for i, chunk := range chunks {
totalSim++
if i == 0 {
// For first chunk, use it completely
mergedContent = chunk.Content
continue
}
currentChunkIndex, _ := strconv.Atoi(chunk.Metadata["chunkIndex"])
previousChunkIndex, _ := strconv.Atoi(chunks[i-1].Metadata["chunkIndex"])
if previousChunkIndex+1 != currentChunkIndex {
// If the chunk index is not sequential, skip it
continue
}
// For subsequent chunks, remove the overlapping part
currentContent := chunk.Content
if len(currentContent) > chunkOverlap {
// Skip the first chunkOverlap characters as they're duplicates
mergedContent += currentContent[chunkOverlap:]
}
}
mergedDocs = append(mergedDocs, chromem.Result{
ID: chunks[0].ID,
Content: mergedContent,
Similarity: totalSim / float32(len(chunks)),
Metadata: chunks[0].Metadata,
})
}
return mergedDocs
}
func getContextString(chats []chat) string {
if len(chats) == 0 {
return ""
}
// Get last few message pairs (user + assistant) for context
// Start from the most recent and work backwards
contextPairs := 2 // Number of user-assistant pairs to include
context := ""
for i := len(chats) - 1; i >= 0 && contextPairs > 0; i-- {
if chats[i].Role == roleUser {
context = "User: " + chats[i].Content + "\n" + context
if i+1 < len(chats) && chats[i+1].Role == roleAssistant {
context = "Assistant: " + chats[i+1].Content + "\n" + context
contextPairs--
}
}
}
return context
}
func (r *rag) chat(ctx context.Context, msg string, index int, documents []document, responses chan<- llmResponseMsg) {
r.chats = append(r.chats, chat{
Role: roleUser,
Content: msg,
})
var ragDocs []chromem.Result
// Combine current message with context from previous messages
contextString := getContextString(r.chats[:len(r.chats)-1]) // Exclude current message
searchText := msg
if contextString != "" {
searchText = contextString + "\n" + msg
}
for _, doc := range documents {
rds, err := doc.retrieve(ctx, r.vectordb, searchText, r.embedder.embeddingFunc())
if err != nil {
responses <- llmResponseMsg{
chatIndex: index,
err: err,
}
return
}
ragDocs = append(ragDocs, rds...)
}
// First sort by similarity to get the best matches
slices.SortFunc(ragDocs, func(a, b chromem.Result) int {
return cmp.Compare(b.Similarity, a.Similarity)
})
// Take more results initially to account for merging
initialCount := ragNeededCount * 2
if len(ragDocs) > initialCount {
ragDocs = ragDocs[:initialCount]
}
// Merge overlapping chunks
ragDocs = mergeChunks(ragDocs)
// Final sort and trim after merging
slices.SortFunc(ragDocs, func(a, b chromem.Result) int {
return cmp.Compare(b.Similarity, a.Similarity)
})
if len(ragDocs) > ragNeededCount {
ragDocs = ragDocs[:ragNeededCount]
}
ragPrompt := ragSystemPrompt(ragDocs)
cs := make([]chat, len(r.chats))
copy(cs, r.chats)
cs = slices.Insert(cs, 0, chat{
Role: roleSystem,
Content: ragPrompt,
})
slog.Info("RAG prompt", "chats", cs)
res := r.convoLLM.chatStream(ctx, cs)
newChat := chat{
Role: roleAssistant,
}
for r := range res {
if r.err != nil {
responses <- llmResponseMsg{
chatIndex: index,
err: r.err,
}
return
}
responses <- llmResponseMsg{
chatIndex: index,
content: r.content,
isThinking: false,
}
newChat.Content += r.content
}
r.chats = append(r.chats, newChat)
responses <- llmResponseMsg{
done: true,
}
}
func (r *rag) genTitle() (string, error) {
title, err := generateSessionTitle(context.Background(), r.genTitleLLM, r.chats)
if err != nil {
return "", fmt.Errorf("error generating session title: %w", err)
}
if title == "" {
return "", errors.New("empty title generated")
}
return title, nil
}
func (r *rag) scanDocument(ctx context.Context, doc document, progress chan<- documentScanLogMsg) {
documents := make(chan chromem.Document)
go r.scanFiles(doc.Path, documents, progress)
go r.storeDocument(ctx, doc, documents, progress)
}
func (r *rag) scanFiles(path string, documents chan<- chromem.Document, progress chan<- documentScanLogMsg) {
progress <- documentScanLogMsg{
content: fmt.Sprintf("Scanning %s", path),
}
var wg sync.WaitGroup
semaphore := make(chan struct{}, runtime.NumCPU())
if err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip git directories
if f.IsDir() && f.Name() == ".git" {
return filepath.SkipDir
}
if f.IsDir() {
return nil
}
wg.Add(1)
go func(p string) {
semaphore <- struct{}{}
defer func() {
<-semaphore
wg.Done()
}()
fileData, err := os.ReadFile(p)
if err != nil {
return
}
// Avoid processing empty files
if len(fileData) == 0 {
return
}
documents <- chromem.Document{
ID: p,
Content: string(fileData),
Metadata: map[string]string{
"filename": filepath.Base(path),
},
}
}(path)
return nil
}); err != nil {
progress <- documentScanLogMsg{
content: fmt.Sprintf("Error scanning %s: %s", path, err),
err: err,
}
return
}
wg.Wait()
close(documents)
}
func (r *rag) storeDocument(ctx context.Context, doc document, documents <-chan chromem.Document, progress chan<- documentScanLogMsg) {
var chunkedDocs []chromem.Document
originalFileCount := 0
for docItem := range documents {
if ctx.Err() != nil {
progress <- documentScanLogMsg{
content: fmt.Sprintf("Error adding documents to collection: %s", ctx.Err()),
err: fmt.Errorf("error adding documents to collection: %w", ctx.Err()),
}
return
}
chunks := chunkDocument(docItem)
chunkedDocs = append(chunkedDocs, chunks...)
originalFileCount++
progress <- documentScanLogMsg{
content: fmt.Sprintf("Scanning %s (created %d chunks)", docItem.ID, len(chunks)),
}
}
progress <- documentScanLogMsg{
content: fmt.Sprintf("Scanned %d files into %d chunks, embedding...", originalFileCount, len(chunkedDocs)),
}
collName := doc.vectorDBCollectionName()
docName := doc.Name
coll, err := r.vectordb.CreateCollection(collName,
map[string]string{"docName": docName}, r.embedder.embeddingFunc())
if err != nil {
progress <- documentScanLogMsg{
content: fmt.Sprintf("Error creating collection: %s", err),
err: fmt.Errorf("error creating collection: %w", err),
}
return
}
if err := coll.AddDocuments(ctx, chunkedDocs, runtime.NumCPU()); err != nil {
progress <- documentScanLogMsg{
content: fmt.Sprintf("Error adding documents to collection: %s", err),
err: fmt.Errorf("error adding documents to collection: %w", err),
}
return
}
progress <- documentScanLogMsg{
content: "Embedding complete",
done: true,
scannedFileCount: originalFileCount,
lastScanTime: time.Now(),
}
}
func (m mainModel) refreshRAG() (mainModel, error) {
if !m.llmIsConfigured() {
return m, nil
}
convo, err := llmFromSetting(m.convoLLMSetting, m.providers)
if err != nil {
return mainModel{}, fmt.Errorf("failed to load convo llm: %w", err)
}
genTitle, err := llmFromSetting(m.genTitleLLMSetting, m.providers)
if err != nil {
return mainModel{}, fmt.Errorf("failed to load title gen llm: %w", err)
}
embedder, err := embedderFromSetting(m.embedderLLMSetting, m.providers)
if err != nil {
return mainModel{}, fmt.Errorf("failed to load embedder llm: %w", err)
}
m.rag = newRAG(m.vectordb, convo, genTitle, embedder)
return m, nil
}