-
Notifications
You must be signed in to change notification settings - Fork 2
/
contextual.go
98 lines (88 loc) · 2.89 KB
/
contextual.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
// Package raggo provides advanced Retrieval-Augmented Generation (RAG) capabilities
// with contextual awareness and memory management.
package raggo
import (
"context"
"fmt"
)
// ContextualStoreOptions configures how documents are processed and stored with
// contextual information. It provides settings for:
// - Vector database collection management
// - Document chunking and processing
// - Embedding model configuration
// - Batch processing controls
//
// This configuration is designed to optimize the balance between processing
// efficiency and context preservation.
type ContextualStoreOptions struct {
// Collection specifies the vector database collection name
Collection string
// APIKey is the authentication key for the embedding provider
APIKey string
// ChunkSize determines the size of text chunks in tokens
// Larger chunks preserve more context but use more memory
ChunkSize int
// ChunkOverlap controls how much text overlaps between chunks
// More overlap helps maintain context across chunk boundaries
ChunkOverlap int
// BatchSize sets how many documents to process simultaneously
// Higher values increase throughput but use more memory
BatchSize int
// ModelName specifies which language model to use for context generation
// This model enriches chunks with additional contextual information
ModelName string
}
// StoreWithContext processes documents and stores them with enhanced contextual information.
// It uses a combination of:
// - Semantic chunking for document segmentation
// - Language model enrichment for context generation
// - Vector embedding for efficient retrieval
// - Batch processing for performance
//
// The function automatically handles:
// - Default configuration values
// - Resource management
// - Error handling and reporting
// - Context-aware processing
//
// Example usage:
//
// opts := raggo.ContextualStoreOptions{
// Collection: "my_docs",
// APIKey: os.Getenv("OPENAI_API_KEY"),
// ChunkSize: 512,
// BatchSize: 100,
// }
//
// err := raggo.StoreWithContext(ctx, "path/to/docs", opts)
func StoreWithContext(ctx context.Context, source string, opts ContextualStoreOptions) error {
// Use default values if not specified
if opts.ChunkSize == 0 {
opts.ChunkSize = 512
}
if opts.ChunkOverlap == 0 {
opts.ChunkOverlap = 64
}
if opts.BatchSize == 0 {
opts.BatchSize = 100
}
if opts.ModelName == "" {
opts.ModelName = "gpt-4o-mini"
}
// Initialize RAG with context-aware configuration
rag, err := NewRAG(
WithMilvus(opts.Collection),
WithOpenAI(opts.APIKey),
func(c *RAGConfig) {
c.ChunkSize = opts.ChunkSize
c.ChunkOverlap = opts.ChunkOverlap
c.BatchSize = opts.BatchSize
},
)
if err != nil {
return fmt.Errorf("failed to initialize RAG: %w", err)
}
defer rag.Close()
// Process and store documents with enhanced context
return rag.ProcessWithContext(ctx, source, opts.ModelName)
}