-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple_rag.go
377 lines (335 loc) · 10.8 KB
/
simple_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
// SimpleRAG provides a minimal, easy-to-use interface for RAG operations.
// It simplifies the configuration and usage of the RAG system while maintaining
// core functionality. This implementation is ideal for:
// - Quick prototyping
// - Simple document retrieval needs
// - Learning the RAG system
//
// Example usage:
//
// config := raggo.DefaultConfig()
// config.APIKey = "your-api-key"
//
// rag, err := raggo.NewSimpleRAG(config)
// if err != nil {
// log.Fatal(err)
// }
//
// // Add documents
// err = rag.AddDocuments(context.Background(), "path/to/docs")
//
// // Search
// response, err := rag.Search(context.Background(), "your query")
package raggo
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/teilomillet/gollm"
)
// SimpleRAG provides a minimal interface for RAG operations.
// It encapsulates the core functionality while hiding complexity.
type SimpleRAG struct {
retriever *Retriever // Handles document retrieval
collection string // Name of the vector collection
apiKey string // API key for services
model string // Embedding model name
vectorDB *VectorDB // Vector database connection
llm gollm.LLM // Language model interface
}
// SimpleRAGConfig holds configuration for SimpleRAG.
// It provides essential configuration options while using
// sensible defaults for other settings.
type SimpleRAGConfig struct {
Collection string // Name of the vector collection
APIKey string // API key for services (e.g., OpenAI)
Model string // Embedding model name
ChunkSize int // Size of text chunks in tokens
ChunkOverlap int // Overlap between consecutive chunks
TopK int // Number of results to retrieve
MinScore float64 // Minimum similarity score threshold
LLMModel string // Language model for text generation
DBType string // Type of vector database (e.g., "milvus", "chromem")
DBAddress string // Address for the vector database
Dimension int // Dimension of embedding vectors
}
// DefaultConfig returns a default configuration for SimpleRAG.
// It provides reasonable defaults for all settings:
// - OpenAI's text-embedding-3-small for embeddings
// - Milvus as the vector database
// - Balanced chunk size and overlap
// - Conservative similarity threshold
func DefaultConfig() SimpleRAGConfig {
return SimpleRAGConfig{
Collection: "documents",
Model: "text-embedding-3-small",
ChunkSize: 200,
ChunkOverlap: 50,
TopK: 5,
MinScore: 0.1,
LLMModel: "gpt-4o-mini",
DBType: "milvus",
DBAddress: "localhost:19530",
Dimension: 1536, // Default dimension for text-embedding-3-small
}
}
// NewSimpleRAG creates a new SimpleRAG instance with minimal configuration.
// It performs the following setup:
// 1. Validates and applies configuration
// 2. Initializes the language model
// 3. Sets up the vector database connection
// 4. Prepares the retrieval system
//
// Returns an error if:
// - API key is missing
// - LLM initialization fails
// - Vector database connection fails
func NewSimpleRAG(config SimpleRAGConfig) (*SimpleRAG, error) {
if config.APIKey == "" {
config.APIKey = os.Getenv("OPENAI_API_KEY")
if config.APIKey == "" {
return nil, fmt.Errorf("OpenAI API key is required")
}
}
if config.Collection == "" {
config.Collection = DefaultConfig().Collection
}
if config.Model == "" {
config.Model = DefaultConfig().Model
}
if config.LLMModel == "" {
config.LLMModel = DefaultConfig().LLMModel
}
if config.DBType == "" {
config.DBType = DefaultConfig().DBType
}
if config.DBAddress == "" {
config.DBAddress = DefaultConfig().DBAddress
}
if config.Dimension == 0 {
config.Dimension = DefaultConfig().Dimension
}
// Initialize LLM
llm, err := gollm.NewLLM(
gollm.SetProvider("openai"),
gollm.SetModel(config.LLMModel),
gollm.SetAPIKey(config.APIKey),
)
if err != nil {
return nil, fmt.Errorf("failed to initialize LLM: %w", err)
}
// Initialize vector database
vectorDB, err := NewVectorDB(
WithType(config.DBType),
WithAddress(config.DBAddress),
WithDimension(config.Dimension),
WithTimeout(5*time.Minute),
)
if err != nil {
return nil, fmt.Errorf("failed to create vector database: %w", err)
}
// Connect to the database
ctx := context.Background()
err = vectorDB.Connect(ctx)
if err != nil {
return nil, fmt.Errorf("failed to connect to vector database: %w", err)
}
// Check and drop existing collection
exists, err := vectorDB.HasCollection(ctx, config.Collection)
if err != nil {
return nil, fmt.Errorf("failed to check collection: %w", err)
}
if exists {
log.Println("Dropping existing collection")
err = vectorDB.DropCollection(ctx, config.Collection)
if err != nil {
return nil, fmt.Errorf("failed to drop collection: %w", err)
}
}
// Create retriever with configured options
retriever, err := NewRetriever(
WithRetrieveDB(config.DBType, config.DBAddress),
WithRetrieveCollection(config.Collection),
WithTopK(config.TopK),
WithMinScore(config.MinScore),
WithHybrid(false), // Start with simple search
WithRetrieveEmbedding(
"openai",
config.Model,
config.APIKey,
),
WithRetrieveDimension(config.Dimension),
)
if err != nil {
return nil, fmt.Errorf("failed to create retriever: %w", err)
}
return &SimpleRAG{
retriever: retriever,
collection: config.Collection,
apiKey: config.APIKey,
model: config.Model,
vectorDB: vectorDB,
llm: llm,
}, nil
}
// AddDocuments processes and stores documents in the vector database.
// The function:
// 1. Validates the source path
// 2. Processes documents into chunks
// 3. Generates embeddings
// 4. Stores vectors in the database
//
// The source parameter can be:
// - A single file path
// - A directory path (all documents will be processed)
// - A glob pattern (e.g., "docs/*.pdf")
func (s *SimpleRAG) AddDocuments(ctx context.Context, source string) error {
if ctx == nil {
ctx = context.Background()
}
log.Printf("Adding documents from source: %s", source)
// Check if source is a directory
fileInfo, err := os.Stat(source)
if err != nil {
return fmt.Errorf("failed to stat source: %w", err)
}
if fileInfo.IsDir() {
// Read all files in directory
files, err := os.ReadDir(source)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
// Process each file
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".txt") {
filePath := filepath.Join(source, file.Name())
err := Register(ctx, filePath,
WithCollection(s.collection, true),
WithChunking(DefaultConfig().ChunkSize, DefaultConfig().ChunkOverlap),
WithEmbedding("openai", s.model, s.apiKey),
WithVectorDB(s.vectorDB.Type(), map[string]string{
"address": s.vectorDB.Address(),
"dimension": fmt.Sprintf("%d", s.vectorDB.Dimension()),
}),
)
if err != nil {
return fmt.Errorf("failed to add document %s: %w", file.Name(), err)
}
log.Printf("Successfully processed file: %s", file.Name())
}
}
} else {
// Register single file
err := Register(ctx, source,
WithCollection(s.collection, true),
WithChunking(DefaultConfig().ChunkSize, DefaultConfig().ChunkOverlap),
WithEmbedding("openai", s.model, s.apiKey),
WithVectorDB(s.vectorDB.Type(), map[string]string{
"address": s.vectorDB.Address(),
"dimension": fmt.Sprintf("%d", s.vectorDB.Dimension()),
}),
)
if err != nil {
return fmt.Errorf("failed to add document: %w", err)
}
}
// Create and load index only once after all documents are processed
err = s.vectorDB.CreateIndex(ctx, s.collection, "Embedding", Index{
Type: "HNSW",
Metric: "L2",
Parameters: map[string]interface{}{
"M": 16,
"efConstruction": 256,
},
})
if err != nil {
return fmt.Errorf("failed to create index: %w", err)
}
// Load the collection
err = s.vectorDB.LoadCollection(ctx, s.collection)
if err != nil {
return fmt.Errorf("failed to load collection: %w", err)
}
log.Printf("Successfully added documents from: %s", source)
return nil
}
// Search performs a semantic search query and generates a response.
// The process:
// 1. Embeds the query into a vector
// 2. Finds similar documents in the vector database
// 3. Uses the LLM to generate a response based on retrieved context
//
// Returns:
// - A natural language response incorporating retrieved information
// - An error if the search or response generation fails
func (s *SimpleRAG) Search(ctx context.Context, query string) (string, error) {
if ctx == nil {
ctx = context.Background()
}
log.Printf("Performing search with query: %s", query)
// Get the total number of documents in the collection
hasCollection, err := s.vectorDB.HasCollection(ctx, s.collection)
if err != nil {
return "", fmt.Errorf("failed to check collection: %w", err)
}
if !hasCollection {
return "", fmt.Errorf("collection %s does not exist", s.collection)
}
// Load collection to ensure it's ready for search
err = s.vectorDB.LoadCollection(ctx, s.collection)
if err != nil {
return "", fmt.Errorf("failed to load collection: %w", err)
}
// Set the retriever's TopK based on the config or dynamically
if s.retriever.config.TopK <= 0 {
// Do a test search with topK=1 to get number of documents
testResults, err := s.vectorDB.Search(ctx, s.collection, map[string]Vector{"test": make(Vector, s.vectorDB.Dimension())}, 1, "L2", nil)
if err != nil {
return "", fmt.Errorf("failed to get collection size: %w", err)
}
// Set TopK to min(20, numDocs) if not specified
s.retriever.config.TopK = 20
if len(testResults) < 20 {
s.retriever.config.TopK = len(testResults)
}
}
log.Printf("Using TopK=%d for search", s.retriever.config.TopK)
results, err := s.retriever.Retrieve(ctx, query)
if err != nil {
return "", fmt.Errorf("failed to search: %w", err)
}
log.Printf("Found %d results", len(results))
// Prepare context from results
var contexts []string
for _, result := range results {
contexts = append(contexts, result.Content)
}
// Generate response using LLM
prompt := fmt.Sprintf(`Here are some relevant sections from our documentation:
%s
Based on this information, please answer the following question: %s
If the information isn't found in the provided context, please say so clearly.`,
strings.Join(contexts, "\n\n---\n\n"),
query,
)
resp, err := s.llm.Generate(ctx, gollm.NewPrompt(prompt))
if err != nil {
return "", fmt.Errorf("failed to generate response: %w", err)
}
return resp, nil
}
// Close releases all resources held by the SimpleRAG instance.
// This includes:
// - Vector database connection
// - Language model resources
// - Any temporary files
func (s *SimpleRAG) Close() error {
if s.vectorDB != nil {
s.vectorDB.Close()
}
return s.retriever.Close()
}