Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 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
|
| 320 |
-
"
|
| 321 |
-
"
|
| 322 |
-
"
|
| 323 |
-
"
|
| 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 |
-
|
| 342 |
-
|
| 343 |
-
|
| 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({
|