AdityaAdaki commited on
Commit
b11459b
·
1 Parent(s): 80a2c80

enhanced the RAG

Browse files
Files changed (1) hide show
  1. f1_ai.py +41 -16
f1_ai.py CHANGED
@@ -178,17 +178,24 @@ class F1AI:
178
  # Format context from documents
179
  context = "\n\n".join([f"Document {i+1}: {doc.page_content}" for i, doc in enumerate(docs)])
180
 
181
- # Create prompt for the LLM
182
  prompt = f"""
183
- Answer the question based on the provided context. Include relevant citations using [1], [2], etc.
184
- If you're unsure or if the context doesn't contain the information, acknowledge the uncertainty.
185
-
 
 
 
 
 
 
 
186
  Context:
187
  {context}
188
 
189
  Question: {question}
190
-
191
- Answer with citations:
192
  """
193
 
194
  # Get response from LLM
@@ -203,17 +210,27 @@ class F1AI:
203
  else: # LangChain LLM
204
  response_text = self.llm.invoke(prompt)
205
 
206
- # Format sources
207
- sources = [{
208
- "url": doc.metadata["source"],
209
- "chunk_index": doc.metadata.get("chunk_index", 0),
210
- "timestamp": doc.metadata.get("timestamp", "")
211
- } for doc in docs]
 
 
 
 
 
212
 
213
- # Format response
214
  formatted_response = {
215
  "answer": response_text,
216
- "sources": sources
 
 
 
 
 
217
  }
218
 
219
  return formatted_response
@@ -258,8 +275,16 @@ async def main():
258
  console.print(Markdown(response['answer']))
259
 
260
  console.print("\n[bold yellow]Sources:[/bold yellow]")
261
- for i, source in enumerate(response['sources']):
262
- console.print(f"[{i+1}] {source['url']}")
 
 
 
 
 
 
 
 
263
  else:
264
  parser.print_help()
265
 
 
178
  # Format context from documents
179
  context = "\n\n".join([f"Document {i+1}: {doc.page_content}" for i, doc in enumerate(docs)])
180
 
181
+ # Create enhanced prompt for the LLM with better instructions
182
  prompt = f"""
183
+ You are an expert Formula 1 knowledge assistant. Using the provided context, answer the question comprehensively and naturally.
184
+
185
+ Guidelines:
186
+ 1. Provide detailed, well-structured responses that flow naturally
187
+ 2. Use source citations [1], [2], etc. to support key facts
188
+ 3. If information is uncertain or missing from context, acknowledge it
189
+ 4. Organize complex answers with clear paragraphs
190
+ 5. Add relevant examples or explanations when helpful
191
+ 6. dont fill the ouput with citations only
192
+
193
  Context:
194
  {context}
195
 
196
  Question: {question}
197
+
198
+ Provide a comprehensive answer with appropriate citations:
199
  """
200
 
201
  # Get response from LLM
 
210
  else: # LangChain LLM
211
  response_text = self.llm.invoke(prompt)
212
 
213
+ # Process and format sources with better attribution
214
+ sources = []
215
+ for i, doc in enumerate(docs, 1):
216
+ source = {
217
+ "index": i,
218
+ "url": doc.metadata["source"],
219
+ "chunk_index": doc.metadata.get("chunk_index", 0),
220
+ "timestamp": doc.metadata.get("timestamp", ""),
221
+ "excerpt": doc.page_content[:200] + "..." if len(doc.page_content) > 200 else doc.page_content
222
+ }
223
+ sources.append(source)
224
 
225
+ # Format response with enhanced structure
226
  formatted_response = {
227
  "answer": response_text,
228
+ "sources": sources,
229
+ "metadata": {
230
+ "total_sources": len(sources),
231
+ "query_timestamp": datetime.now().isoformat(),
232
+ "response_format_version": "2.0"
233
+ }
234
  }
235
 
236
  return formatted_response
 
275
  console.print(Markdown(response['answer']))
276
 
277
  console.print("\n[bold yellow]Sources:[/bold yellow]")
278
+ for source in response['sources']:
279
+ console.print(f"[{source['index']}] {source['url']}")
280
+ console.print(f"[dim]Excerpt:[/dim] {source['excerpt']}\n")
281
+
282
+ # Print metadata
283
+ console.print("\n[bold blue]Query Info:[/bold blue]")
284
+ console.print(f"Total sources: {response['metadata']['total_sources']}")
285
+ console.print(f"Query time: {response['metadata']['query_timestamp']}")
286
+ console.print(f"Response version: {response['metadata']['response_format_version']}")
287
+
288
  else:
289
  parser.print_help()
290