Spaces:
Sleeping
Sleeping
Update api/server.py
Browse files- api/server.py +22 -2
api/server.py
CHANGED
|
@@ -167,6 +167,19 @@ def _build_upload_hint(sess: Dict[str, Any]) -> str:
|
|
| 167 |
)
|
| 168 |
return "\n".join(lines)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
# ----------------------------
|
| 172 |
# Warmup
|
|
@@ -451,13 +464,20 @@ def chat(req: ChatReq):
|
|
| 451 |
sess["cognitive_state"] = update_cognitive_state_from_message(msg, sess["cognitive_state"])
|
| 452 |
marks_ms["cognitive_update_done"] = (time.time() - t0) * 1000.0
|
| 453 |
|
| 454 |
-
|
|
|
|
|
|
|
|
|
|
| 455 |
rag_context_text, rag_used_chunks = "", []
|
| 456 |
else:
|
| 457 |
rag_context_text, rag_used_chunks = retrieve_relevant_chunks(msg, sess["rag_chunks"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
marks_ms["rag_retrieve_done"] = (time.time() - t0) * 1000.0
|
| 459 |
|
| 460 |
-
#
|
| 461 |
upload_hint = _build_upload_hint(sess)
|
| 462 |
if upload_hint:
|
| 463 |
rag_context_text = (upload_hint + "\n\n---\n\n" + (rag_context_text or "")).strip()
|
|
|
|
| 167 |
)
|
| 168 |
return "\n".join(lines)
|
| 169 |
|
| 170 |
+
# NEW: force RAG on short "document actions" so refs exist
|
| 171 |
+
def _should_force_rag(message: str) -> bool:
|
| 172 |
+
m = (message or "").lower()
|
| 173 |
+
if not m:
|
| 174 |
+
return False
|
| 175 |
+
triggers = [
|
| 176 |
+
"summarize", "summary", "read", "analyze", "explain",
|
| 177 |
+
"the uploaded file", "uploaded", "file", "document", "pdf",
|
| 178 |
+
"slides", "ppt", "syllabus", "lecture",
|
| 179 |
+
"总结", "概括", "阅读", "读一下", "解析", "分析", "这份文件", "上传", "文档", "课件", "讲义",
|
| 180 |
+
]
|
| 181 |
+
return any(t in m for t in triggers)
|
| 182 |
+
|
| 183 |
|
| 184 |
# ----------------------------
|
| 185 |
# Warmup
|
|
|
|
| 464 |
sess["cognitive_state"] = update_cognitive_state_from_message(msg, sess["cognitive_state"])
|
| 465 |
marks_ms["cognitive_update_done"] = (time.time() - t0) * 1000.0
|
| 466 |
|
| 467 |
+
# NEW: do NOT bypass RAG for document actions (so UI refs are preserved)
|
| 468 |
+
force_rag = _should_force_rag(msg)
|
| 469 |
+
|
| 470 |
+
if (len(msg) < 20 and ("?" not in msg)) and (not force_rag):
|
| 471 |
rag_context_text, rag_used_chunks = "", []
|
| 472 |
else:
|
| 473 |
rag_context_text, rag_used_chunks = retrieve_relevant_chunks(msg, sess["rag_chunks"])
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
|
| 478 |
marks_ms["rag_retrieve_done"] = (time.time() - t0) * 1000.0
|
| 479 |
|
| 480 |
+
# NEW: prepend deterministic upload/file-state hint so the model never says “no file”
|
| 481 |
upload_hint = _build_upload_hint(sess)
|
| 482 |
if upload_hint:
|
| 483 |
rag_context_text = (upload_hint + "\n\n---\n\n" + (rag_context_text or "")).strip()
|