-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag.py
More file actions
215 lines (168 loc) · 6.54 KB
/
Copy pathrag.py
File metadata and controls
215 lines (168 loc) · 6.54 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
"""
RAG Pipeline for Question Answering
Retrieves relevant chunks from ChromaDB and generates answers using GPT-4o-mini
"""
import os
import logging
from typing import List, Dict, Any, Tuple
import chromadb
import google.generativeai as genai
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Configuration
CHROMA_DB_PATH = "./chroma_db"
COLLECTION_NAME = "rag_documents"
EMBEDDING_MODEL = "models/text-embedding-004"
CHAT_MODEL = "gemini-2.0-flash"
TOP_K = 5 # Number of chunks to retrieve
# Initialize Gemini client
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# System prompt for RAG
SYSTEM_PROMPT = """You are a helpful assistant that answers questions based ONLY on the provided context.
Instructions:
- Use ONLY the information from the provided context to answer questions
- If the context doesn't contain enough information to answer the question, say "I don't know based on the available documents" or "The provided documents don't contain information about that"
- Be concise and accurate
- If you quote from the context, be precise
- Do not make up information or use knowledge outside the provided context
"""
def get_chroma_collection():
"""Get or create ChromaDB collection"""
try:
chroma_client = chromadb.PersistentClient(path=CHROMA_DB_PATH)
collection = chroma_client.get_collection(name=COLLECTION_NAME)
return collection
except Exception as e:
logger.error(f"Error accessing ChromaDB collection: {e}")
logger.error("Make sure you've run the ingestion pipeline first (python ingest.py)")
return None
def create_query_embedding(query: str) -> List[float]:
"""Create embedding for the query using Gemini"""
try:
result = genai.embed_content(
model=EMBEDDING_MODEL,
content=query,
task_type="retrieval_query"
)
return result['embedding']
except Exception as e:
logger.error(f"Error creating query embedding: {e}")
return []
def retrieve_context(query: str, top_k: int = TOP_K) -> List[Dict[str, Any]]:
"""
Retrieve top-k most relevant chunks from ChromaDB
"""
collection = get_chroma_collection()
if not collection:
return []
# Create query embedding
query_embedding = create_query_embedding(query)
if not query_embedding:
logger.error("Failed to create query embedding")
return []
# Query ChromaDB
try:
results = collection.query(
query_embeddings=[query_embedding],
n_results=top_k
)
# Format results
contexts = []
if results and results['documents'] and len(results['documents']) > 0:
for i in range(len(results['documents'][0])):
context = {
'text': results['documents'][0][i],
'metadata': results['metadatas'][0][i] if results['metadatas'] else {},
'distance': results['distances'][0][i] if results['distances'] else None
}
contexts.append(context)
logger.info(f"Retrieved {len(contexts)} relevant chunks")
return contexts
except Exception as e:
logger.error(f"Error retrieving context: {e}")
return []
def generate_answer(query: str, contexts: List[Dict[str, Any]]) -> str:
"""
Generate answer using Gemini with retrieved context
"""
if not contexts:
return "I couldn't find any relevant information in the documents to answer your question."
# Build context string
context_str = "\n\n---\n\n".join([
f"Source: {ctx['metadata'].get('source', 'unknown')}\n{ctx['text']}"
for ctx in contexts
])
# Create prompt with system instructions, context, and question
prompt = f"""{SYSTEM_PROMPT}
Context from documents:
{context_str}
---
Question: {query}
Please answer the question based only on the context provided above."""
try:
# Call Gemini
model = genai.GenerativeModel(CHAT_MODEL)
response = model.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
temperature=0.3, # Lower temperature for more factual responses
max_output_tokens=500,
)
)
# Check if response was blocked
if not response.parts:
logger.error(f"Response blocked. Prompt feedback: {response.prompt_feedback}")
return "I couldn't generate a response. Please try rephrasing your question."
answer = response.text
logger.info("Generated answer successfully")
return answer
except Exception as e:
logger.error(f"Error generating answer: {e}")
logger.error(f"Error type: {type(e).__name__}")
import traceback
logger.error(f"Traceback: {traceback.format_exc()}")
return "I encountered an error while generating the answer. Please try again."
def retrieve_and_answer(question: str) -> Tuple[str, List[Dict[str, Any]]]:
"""
Main RAG pipeline: retrieve context and generate answer
Args:
question: User's question
Returns:
Tuple of (answer, contexts_used)
"""
logger.info(f"Processing question: {question}")
# Retrieve relevant context
contexts = retrieve_context(question)
# Generate answer
answer = generate_answer(question, contexts)
# Return answer and contexts
return answer, contexts
def format_context_for_response(contexts: List[Dict[str, Any]]) -> List[Dict[str, str]]:
"""
Format context data for API response
"""
formatted = []
for ctx in contexts:
formatted.append({
'text': ctx['text'][:200] + '...' if len(ctx['text']) > 200 else ctx['text'],
'source': ctx['metadata'].get('source', 'unknown'),
'chunk_index': str(ctx['metadata'].get('chunk_index', ''))
})
return formatted
if __name__ == "__main__":
# Test the RAG pipeline
test_question = "What is this document about?"
if not os.getenv("GEMINI_API_KEY"):
logger.error("GEMINI_API_KEY not found in environment variables")
exit(1)
print(f"\nQuestion: {test_question}\n")
answer, contexts = retrieve_and_answer(test_question)
print(f"Answer: {answer}\n")
print(f"Used {len(contexts)} context chunks")