forked from dewitt4/github-process-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_engine.py
More file actions
271 lines (223 loc) · 8.88 KB
/
rag_engine.py
File metadata and controls
271 lines (223 loc) · 8.88 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
"""
RAG (Retrieval-Augmented Generation) Engine using ChromaDB.
Handles document processing, embedding generation, and context retrieval.
"""
import os
import chromadb
from chromadb.config import Settings
import google.generativeai as genai
from docx import Document
from PyPDF2 import PdfReader
from logger import logger
from config import Config
class RAGEngine:
"""RAG engine for document processing and retrieval."""
def __init__(self):
"""Initialize the RAG engine with ChromaDB and Gemini embeddings."""
try:
# Initialize ChromaDB
self.client = chromadb.PersistentClient(
path=Config.CHROMA_DB_PATH,
settings=Settings(anonymized_telemetry=False)
)
# Get or create collection
self.collection = self.client.get_or_create_collection(
name="rag_documents",
metadata={"description": "RAG document embeddings"}
)
# Configure Gemini for embeddings
genai.configure(api_key=Config.GEMINI_API_KEY)
logger.info("RAG Engine initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize RAG Engine: {e}")
raise
def extract_text(self, file_path, file_type):
"""
Extract text content from uploaded file.
Args:
file_path: Path to the file
file_type: File extension (txt, pdf, docx)
Returns:
Extracted text content
"""
try:
if file_type == 'txt':
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
elif file_type == 'pdf':
reader = PdfReader(file_path)
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
return text
elif file_type == 'docx':
doc = Document(file_path)
text = ""
for paragraph in doc.paragraphs:
text += paragraph.text + "\n"
return text
else:
raise ValueError(f"Unsupported file type: {file_type}")
except Exception as e:
logger.error(f"Error extracting text from {file_path}: {e}")
raise
def chunk_text(self, text, chunk_size=None, overlap=None):
"""
Split text into overlapping chunks.
Args:
text: Text to chunk
chunk_size: Maximum characters per chunk
overlap: Number of overlapping characters
Returns:
List of text chunks
"""
chunk_size = chunk_size or Config.CHUNK_SIZE
overlap = overlap or Config.CHUNK_OVERLAP
chunks = []
start = 0
text_length = len(text)
while start < text_length:
end = start + chunk_size
chunk = text[start:end]
# Try to break at sentence or word boundary
if end < text_length:
# Look for sentence end
last_period = chunk.rfind('.')
last_newline = chunk.rfind('\n')
break_point = max(last_period, last_newline)
if break_point > chunk_size * 0.5: # Only if we're past halfway
chunk = chunk[:break_point + 1]
end = start + break_point + 1
chunks.append(chunk.strip())
start = end - overlap
return [c for c in chunks if c] # Filter empty chunks
def generate_embedding(self, text):
"""
Generate embedding for text using Gemini.
Args:
text: Text to embed
Returns:
Embedding vector
"""
try:
result = genai.embed_content(
model=Config.GEMINI_EMBEDDING_MODEL,
content=text,
task_type="retrieval_document"
)
return result['embedding']
except Exception as e:
logger.error(f"Error generating embedding: {e}")
raise
def add_document(self, file_path, filename):
"""
Process and add document to ChromaDB.
Args:
file_path: Path to the uploaded file
filename: Original filename
Returns:
Number of chunks added
"""
try:
# Extract file type
file_type = filename.rsplit('.', 1)[1].lower()
# Extract text
logger.info(f"Extracting text from {filename}")
text = self.extract_text(file_path, file_type)
if not text.strip():
raise ValueError("No text content found in document")
# Chunk text
logger.info(f"Chunking document {filename}")
chunks = self.chunk_text(text)
logger.info(f"Created {len(chunks)} chunks from {filename}")
# Generate embeddings and add to ChromaDB
documents = []
metadatas = []
ids = []
embeddings = []
for i, chunk in enumerate(chunks):
chunk_id = f"{filename}_chunk_{i}"
# Generate embedding
embedding = self.generate_embedding(chunk)
documents.append(chunk)
metadatas.append({
"filename": filename,
"chunk_index": i,
"total_chunks": len(chunks)
})
ids.append(chunk_id)
embeddings.append(embedding)
# Add to collection
self.collection.add(
documents=documents,
metadatas=metadatas,
ids=ids,
embeddings=embeddings
)
logger.info(f"Successfully added {len(chunks)} chunks from {filename} to RAG database")
return len(chunks)
except Exception as e:
logger.error(f"Error adding document {filename}: {e}")
raise
def retrieve_context(self, query, top_k=None):
"""
Retrieve relevant context for a query.
Args:
query: User query
top_k: Number of results to retrieve
Returns:
List of relevant text chunks with metadata
"""
try:
top_k = top_k or Config.TOP_K_RESULTS
# Generate query embedding
query_embedding = genai.embed_content(
model=Config.GEMINI_EMBEDDING_MODEL,
content=query,
task_type="retrieval_query"
)['embedding']
# Query ChromaDB
results = self.collection.query(
query_embeddings=[query_embedding],
n_results=top_k
)
# Format results
context_chunks = []
if results and results['documents']:
for i, doc in enumerate(results['documents'][0]):
metadata = results['metadatas'][0][i] if results['metadatas'] else {}
context_chunks.append({
'text': doc,
'metadata': metadata,
'distance': results['distances'][0][i] if results['distances'] else None
})
logger.info(f"Retrieved {len(context_chunks)} context chunks for query")
return context_chunks
except Exception as e:
logger.error(f"Error retrieving context: {e}")
return []
def get_stats(self):
"""Get statistics about the RAG database."""
try:
count = self.collection.count()
return {
'total_chunks': count,
'collection_name': self.collection.name
}
except Exception as e:
logger.error(f"Error getting stats: {e}")
return {'total_chunks': 0}
def clear_database(self):
"""Clear all documents from the RAG database."""
try:
# Delete and recreate collection
self.client.delete_collection("rag_documents")
self.collection = self.client.get_or_create_collection(
name="rag_documents",
metadata={"description": "RAG document embeddings"}
)
logger.info("RAG database cleared successfully")
return True
except Exception as e:
logger.error(f"Error clearing database: {e}")
return False