-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_test.go
More file actions
626 lines (498 loc) · 13.9 KB
/
rag_test.go
File metadata and controls
626 lines (498 loc) · 13.9 KB
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package sdk
import (
"context"
"errors"
"strings"
"testing"
)
// Mock EmbeddingModel.
type MockEmbeddingModel struct {
EmbedFunc func(ctx context.Context, texts []string) ([]Vector, error)
DimFunc func() int
}
func (m *MockEmbeddingModel) Embed(ctx context.Context, texts []string) ([]Vector, error) {
if m.EmbedFunc != nil {
return m.EmbedFunc(ctx, texts)
}
// Default: return simple vectors
vectors := make([]Vector, len(texts))
for i := range vectors {
vectors[i] = Vector{Values: []float64{0.1, 0.2, 0.3}}
}
return vectors, nil
}
func (m *MockEmbeddingModel) Dimensions() int {
if m.DimFunc != nil {
return m.DimFunc()
}
return 3
}
// Mock VectorStore.
type MockVectorStore struct {
UpsertFunc func(ctx context.Context, vectors []Vector) error
QueryFunc func(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error)
DeleteFunc func(ctx context.Context, ids []string) error
}
func (m *MockVectorStore) Upsert(ctx context.Context, vectors []Vector) error {
if m.UpsertFunc != nil {
return m.UpsertFunc(ctx, vectors)
}
return nil
}
func (m *MockVectorStore) Query(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error) {
if m.QueryFunc != nil {
return m.QueryFunc(ctx, vector, limit, filter)
}
return []VectorMatch{}, nil
}
func (m *MockVectorStore) Delete(ctx context.Context, ids []string) error {
if m.DeleteFunc != nil {
return m.DeleteFunc(ctx, ids)
}
return nil
}
func TestNewRAG(t *testing.T) {
vectorStore := &MockVectorStore{}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
if rag == nil {
t.Fatal("expected RAG to be created")
}
if rag.topK != 10 {
t.Errorf("expected default topK 10, got %d", rag.topK)
}
if rag.similarityThresh != 0.7 {
t.Errorf("expected default similarity threshold 0.7, got %f", rag.similarityThresh)
}
if rag.chunkSize != 512 {
t.Errorf("expected default chunk size 512, got %d", rag.chunkSize)
}
}
func TestNewRAG_WithOptions(t *testing.T) {
vectorStore := &MockVectorStore{}
embedder := &MockEmbeddingModel{}
opts := &RAGOptions{
TopK: 20,
SimilarityThresh: 0.8,
Rerank: true,
IncludeMetadata: false,
ChunkSize: 256,
ChunkOverlap: 25,
MaxContextTokens: 1000,
}
rag := NewRAG(vectorStore, embedder, nil, nil, opts)
if rag.topK != 20 {
t.Errorf("expected topK 20, got %d", rag.topK)
}
if rag.similarityThresh != 0.8 {
t.Errorf("expected similarity threshold 0.8, got %f", rag.similarityThresh)
}
if !rag.rerank {
t.Error("expected rerank to be enabled")
}
if rag.includeMetadata {
t.Error("expected includeMetadata to be false")
}
if rag.chunkSize != 256 {
t.Errorf("expected chunk size 256, got %d", rag.chunkSize)
}
}
func TestRAG_IndexDocument(t *testing.T) {
upsertCalled := 0
vectorStore := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
upsertCalled++
return nil
},
}
embedder := &MockEmbeddingModel{}
opts := &RAGOptions{
ChunkSize: 100,
ChunkOverlap: 10,
}
rag := NewRAG(vectorStore, embedder, nil, nil, opts)
doc := Document{
ID: "doc1",
Content: "Short test content",
Metadata: map[string]any{
"source": "test",
},
}
err := rag.IndexDocument(context.Background(), doc)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if upsertCalled == 0 {
t.Error("expected upsert to be called")
}
}
func TestRAG_IndexDocument_WithChunks(t *testing.T) {
upsertCalled := 0
var vectorsReceived []Vector
vectorStore := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
upsertCalled++
vectorsReceived = vectors
return nil
},
}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
doc := Document{
ID: "doc1",
Content: "Test",
Chunks: []DocumentChunk{
{ID: "chunk1", Content: "First chunk", Index: 0},
{ID: "chunk2", Content: "Second chunk", Index: 1},
},
}
err := rag.IndexDocument(context.Background(), doc)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if upsertCalled != 1 {
t.Errorf("expected upsert to be called once (batch), got %d", upsertCalled)
}
if len(vectorsReceived) != 2 {
t.Errorf("expected 2 vectors in batch, got %d", len(vectorsReceived))
}
}
func TestRAG_IndexDocument_EmbedError(t *testing.T) {
vectorStore := &MockVectorStore{}
embedder := &MockEmbeddingModel{
EmbedFunc: func(ctx context.Context, texts []string) ([]Vector, error) {
return nil, errors.New("embedding error")
},
}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
doc := Document{
ID: "doc1",
Content: "Test content",
}
err := rag.IndexDocument(context.Background(), doc)
if err == nil {
t.Error("expected error from embedding")
}
if !strings.Contains(err.Error(), "failed to generate embeddings") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestRAG_IndexDocument_UpsertError(t *testing.T) {
vectorStore := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
return errors.New("upsert error")
},
}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
doc := Document{
ID: "doc1",
Content: "Test content",
}
err := rag.IndexDocument(context.Background(), doc)
if err == nil {
t.Error("expected error from upsert")
}
if !strings.Contains(err.Error(), "failed to store embeddings") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestRAG_Retrieve(t *testing.T) {
vectorStore := &MockVectorStore{
QueryFunc: func(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error) {
return []VectorMatch{
{
ID: "chunk1",
Score: 0.9,
Metadata: map[string]any{
"content": "Relevant content",
"chunk_index": 0,
},
},
{
ID: "chunk2",
Score: 0.8,
Metadata: map[string]any{
"content": "Another relevant content",
"chunk_index": 1,
},
},
}, nil
},
}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
result, err := rag.Retrieve(context.Background(), "test query")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result.Query != "test query" {
t.Error("expected query to be set")
}
if len(result.Documents) != 2 {
t.Errorf("expected 2 documents, got %d", len(result.Documents))
}
if result.Documents[0].Score != 0.9 {
t.Errorf("expected score 0.9, got %f", result.Documents[0].Score)
}
if result.Documents[0].Document.Content != "Relevant content" {
t.Error("expected content to be extracted from metadata")
}
}
func TestRAG_Retrieve_EmbedError(t *testing.T) {
vectorStore := &MockVectorStore{}
embedder := &MockEmbeddingModel{
EmbedFunc: func(ctx context.Context, texts []string) ([]Vector, error) {
return nil, errors.New("embedding error")
},
}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
_, err := rag.Retrieve(context.Background(), "test query")
if err == nil {
t.Error("expected error from embedding")
}
if !strings.Contains(err.Error(), "failed to generate query embedding") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestRAG_Retrieve_SearchError(t *testing.T) {
vectorStore := &MockVectorStore{
QueryFunc: func(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error) {
return nil, errors.New("search error")
},
}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
_, err := rag.Retrieve(context.Background(), "test query")
if err == nil {
t.Error("expected error from search")
}
if !strings.Contains(err.Error(), "vector search failed") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestRAG_Retrieve_WithReranking(t *testing.T) {
vectorStore := &MockVectorStore{
QueryFunc: func(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error) {
return []VectorMatch{
{
ID: "chunk1",
Score: 0.8,
Metadata: map[string]any{
"content": "quantum computing basics",
"chunk_index": 0,
},
},
{
ID: "chunk2",
Score: 0.9,
Metadata: map[string]any{
"content": "introduction to quantum physics",
"chunk_index": 1,
},
},
}, nil
},
}
embedder := &MockEmbeddingModel{}
opts := &RAGOptions{
Rerank: true,
}
rag := NewRAG(vectorStore, embedder, nil, nil, opts)
result, err := rag.Retrieve(context.Background(), "quantum computing")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// After reranking, the document with more matching terms should be ranked higher
// "quantum computing basics" has both "quantum" and "computing"
if result.Documents[0].Rank != 1 {
t.Errorf("expected rank 1 for first document, got %d", result.Documents[0].Rank)
}
}
func TestRAG_ChunkDocument(t *testing.T) {
rag := NewRAG(nil, nil, nil, nil, &RAGOptions{
ChunkSize: 50,
ChunkOverlap: 10,
})
doc := Document{
ID: "doc1",
Content: "This is a long document that needs to be chunked into smaller pieces for processing",
}
chunks := rag.chunkDocument(doc)
if len(chunks) == 0 {
t.Error("expected chunks to be created")
}
// Check that chunks have content
for _, chunk := range chunks {
if chunk.Content == "" {
t.Error("expected chunk to have content")
}
if chunk.ID == "" {
t.Error("expected chunk to have ID")
}
}
// Check that chunks are ordered
for i, chunk := range chunks {
if chunk.Index != i {
t.Errorf("expected chunk index %d, got %d", i, chunk.Index)
}
}
}
func TestRAG_ChunkDocument_ShortContent(t *testing.T) {
rag := NewRAG(nil, nil, nil, nil, &RAGOptions{
ChunkSize: 100,
ChunkOverlap: 0, // No overlap for this test
})
doc := Document{
ID: "doc1",
Content: "Short content",
}
chunks := rag.chunkDocument(doc)
if len(chunks) < 1 {
t.Error("expected at least 1 chunk for short content")
}
// Short content should fit in one or few chunks
if len(chunks) > 2 {
t.Errorf("expected at most 2 chunks for short content, got %d", len(chunks))
}
}
func TestRAG_BuildContext(t *testing.T) {
rag := NewRAG(nil, nil, nil, nil, &RAGOptions{
MaxContextTokens: 1000,
})
documents := []RetrievedDocument{
{
Document: DocumentChunk{
Content: "First document content",
},
Score: 0.9,
},
{
Document: DocumentChunk{
Content: "Second document content",
},
Score: 0.8,
},
}
context := rag.buildContext(documents)
if context == "" {
t.Error("expected context to be built")
}
if !strings.Contains(context, "First document content") {
t.Error("expected first document in context")
}
if !strings.Contains(context, "Second document content") {
t.Error("expected second document in context")
}
if !strings.Contains(context, "0.90") || !strings.Contains(context, "0.80") {
t.Error("expected scores in context")
}
}
func TestRAG_BuildContext_TokenLimit(t *testing.T) {
rag := NewRAG(nil, nil, nil, nil, &RAGOptions{
MaxContextTokens: 10, // Very small limit
})
// Create a document with content longer than token limit
longContent := strings.Repeat("word ", 100)
documents := []RetrievedDocument{
{
Document: DocumentChunk{
Content: longContent,
},
Score: 0.9,
},
{
Document: DocumentChunk{
Content: "This should not be included",
},
Score: 0.8,
},
}
context := rag.buildContext(documents)
// Second document should not be included due to token limit
if strings.Contains(context, "This should not be included") {
t.Error("expected second document to be excluded due to token limit")
}
}
func TestRAG_RerankDocuments(t *testing.T) {
rag := NewRAG(nil, nil, nil, nil, nil)
documents := []RetrievedDocument{
{
Document: DocumentChunk{
Content: "document about cats",
},
Score: 0.8,
Rank: 1,
},
{
Document: DocumentChunk{
Content: "document about dogs and cats",
},
Score: 0.7,
Rank: 2,
},
}
reranked := rag.rerankDocuments("dogs cats", documents)
// Document with both terms should be ranked higher after reranking
// The second document has both "dogs" and "cats" so it should get boosted
hasDogsAndCats := false
for i, doc := range reranked {
if doc.Document.Content == "document about dogs and cats" {
hasDogsAndCats = true
// It should be ranked highly (either first or second is acceptable after reranking)
if i > 1 {
t.Error("expected document with more matching terms to be ranked in top 2")
}
}
}
if !hasDogsAndCats {
t.Error("expected document with dogs and cats in results")
}
// Check that ranks are updated
if reranked[0].Rank != 1 {
t.Error("expected ranks to be updated with rank 1 for first document")
}
}
func TestRAG_DeleteDocument(t *testing.T) {
vectorStore := &MockVectorStore{}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
err := rag.DeleteDocument(context.Background(), "doc1")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}
func TestRAG_UpdateDocument(t *testing.T) {
upsertCalled := 0
vectorStore := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
upsertCalled++
return nil
},
}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
doc := Document{
ID: "doc1",
Content: "Updated content",
}
err := rag.UpdateDocument(context.Background(), doc)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if upsertCalled == 0 {
t.Error("expected upsert to be called after update")
}
}
func TestRAG_GetStats(t *testing.T) {
vectorStore := &MockVectorStore{}
embedder := &MockEmbeddingModel{}
rag := NewRAG(vectorStore, embedder, nil, nil, nil)
stats, err := rag.GetStats(context.Background())
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if stats == nil {
t.Error("expected stats to be returned")
}
}