Durga-7780 commited on
Commit
cdc12ef
·
verified ·
1 Parent(s): fe1d488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -286,10 +286,9 @@ async def chat(
286
  context_pieces.append(
287
  f"Source: {src} (page {page}, chunk {chunk})\n{doc}"
288
  )
289
- if not context_pieces:
290
- return {"answer": "I don't know"}
291
  context_text = "\n\n---\n\n".join(context_pieces) if context_pieces else ""
292
-
293
  # Parse history if provided
294
  history_msgs = []
295
  if history:
@@ -316,14 +315,13 @@ async def chat(
316
 
317
  # Core system prompt
318
  system_prompt = (
319
- "You are a STRICT RAG chatbot. "
320
- "Use ONLY the information in the provided context to answer factual questions. "
321
- "Chat history is ONLY for understanding conversation flow, NOT for facts. "
322
- "If the answer is not present in the context, reply EXACTLY with: 'I don't know'. "
323
- "Do NOT guess. Do NOT use outside knowledge."
324
  )
325
 
326
-
327
  # ❗ STRICT: If no RAG context, answer immediately
328
  if not context_pieces:
329
  return {"answer": "I don't know"}
@@ -338,10 +336,11 @@ async def chat(
338
  messages_payload.extend(history_msgs)
339
 
340
  # Add RAG context once (NO DUPLICATES)
341
- messages_payload.append({
342
- "role": "system",
343
- "content": f"Context:\n{context_text}"
344
- })
 
345
 
346
  # Add user's question
347
  messages_payload.append({
 
286
  context_pieces.append(
287
  f"Source: {src} (page {page}, chunk {chunk})\n{doc}"
288
  )
289
+
 
290
  context_text = "\n\n---\n\n".join(context_pieces) if context_pieces else ""
291
+ has_context = bool(context_pieces)
292
  # Parse history if provided
293
  history_msgs = []
294
  if history:
 
315
 
316
  # Core system prompt
317
  system_prompt = (
318
+ "You are a helpful study chatbot. "
319
+ "If context from uploaded documents is provided, use ONLY that context to answer. "
320
+ "If no relevant context is found, provide the best general answer using your own knowledge. "
321
+ "Never say 'I don't know' unless the question is unclear. "
322
+ "Chat history is ONLY for understanding conversation flow."
323
  )
324
 
 
325
  # ❗ STRICT: If no RAG context, answer immediately
326
  if not context_pieces:
327
  return {"answer": "I don't know"}
 
336
  messages_payload.extend(history_msgs)
337
 
338
  # Add RAG context once (NO DUPLICATES)
339
+ if has_context:
340
+ messages_payload.append({
341
+ "role": "system",
342
+ "content": f"Context:\n{context_text}"
343
+ })
344
 
345
  # Add user's question
346
  messages_payload.append({