Update app.py
Browse files
app.py
CHANGED
|
@@ -68,6 +68,40 @@ KU_GENERAL_RE = re.compile(r"\b(khalifa university|ku)\b.*\b(admission|admission
|
|
| 68 |
LIBRARY_HOURS_URL = "https://library.ku.ac.ae/hours"
|
| 69 |
KU_MAIN_URL = "https://www.ku.ac.ae/"
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
GROUNDED_LIBRARY_MAP = {
|
| 72 |
"ill": "interlibrary loan ILL document delivery full text unavailable article not available borrow from another library",
|
| 73 |
"fulltext": "full text libkey nomad article access pdf unavailable interlibrary loan",
|
|
@@ -967,8 +1001,7 @@ async def agent_query(req: AgentRequest):
|
|
| 967 |
|
| 968 |
# ---- Early greeting handling ----
|
| 969 |
q_clean = question.strip()
|
| 970 |
-
|
| 971 |
-
if PURE_GREETING_RE.match(q_clean) or q_norm in {"hi", "hello", "hey", "good morning", "good afternoon", "good evening"}:
|
| 972 |
answer = (
|
| 973 |
"Hi! I’m <strong>LibBee</strong>, the Khalifa University Library AI Assistant.<br><br>"
|
| 974 |
"I can help you find articles and books, search databases, access full text, "
|
|
@@ -992,6 +1025,26 @@ async def agent_query(req: AgentRequest):
|
|
| 992 |
"source_mode": "social",
|
| 993 |
}
|
| 994 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 995 |
|
| 996 |
if _looks_library_hours_question(question):
|
| 997 |
elapsed = time.time() - start
|
|
|
|
| 68 |
LIBRARY_HOURS_URL = "https://library.ku.ac.ae/hours"
|
| 69 |
KU_MAIN_URL = "https://www.ku.ac.ae/"
|
| 70 |
|
| 71 |
+
GREETING_FOLLOWUP_RE = re.compile(r"^(yes|yes please|yes check|sure|okay|ok|yeah|yep|please do|go ahead)$", re.IGNORECASE)
|
| 72 |
+
|
| 73 |
+
def _last_assistant_message(history):
|
| 74 |
+
if not history:
|
| 75 |
+
return ""
|
| 76 |
+
for m in reversed(history):
|
| 77 |
+
if m.get("role") == "assistant":
|
| 78 |
+
return m.get("content", "") or ""
|
| 79 |
+
return ""
|
| 80 |
+
|
| 81 |
+
def _is_greeting_menu_followup(question: str, history) -> bool:
|
| 82 |
+
q = (question or "").strip()
|
| 83 |
+
if not GREETING_FOLLOWUP_RE.match(q):
|
| 84 |
+
return False
|
| 85 |
+
last = _last_assistant_message(history).lower()
|
| 86 |
+
return (
|
| 87 |
+
"i’m <strong>libbee</strong>" in last
|
| 88 |
+
or "i'm <strong>libbee</strong>" in last
|
| 89 |
+
or "are you looking for one of these" in last
|
| 90 |
+
or "khalifa university library ai assistant" in last
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
def _greeting_menu_clarify_answer() -> str:
|
| 94 |
+
return (
|
| 95 |
+
"Sure — what would you like help with?<br><br>"
|
| 96 |
+
"You can choose one of these or type your question directly:<br>"
|
| 97 |
+
"• Find articles and books<br>"
|
| 98 |
+
"• Get full text for an article<br>"
|
| 99 |
+
"• Submit an Interlibrary Loan (ILL) request<br>"
|
| 100 |
+
"• Contact a librarian<br>"
|
| 101 |
+
"• Check library hours"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
GROUNDED_LIBRARY_MAP = {
|
| 106 |
"ill": "interlibrary loan ILL document delivery full text unavailable article not available borrow from another library",
|
| 107 |
"fulltext": "full text libkey nomad article access pdf unavailable interlibrary loan",
|
|
|
|
| 1001 |
|
| 1002 |
# ---- Early greeting handling ----
|
| 1003 |
q_clean = question.strip()
|
| 1004 |
+
if PURE_GREETING_RE.match(q_clean):
|
|
|
|
| 1005 |
answer = (
|
| 1006 |
"Hi! I’m <strong>LibBee</strong>, the Khalifa University Library AI Assistant.<br><br>"
|
| 1007 |
"I can help you find articles and books, search databases, access full text, "
|
|
|
|
| 1025 |
"source_mode": "social",
|
| 1026 |
}
|
| 1027 |
|
| 1028 |
+
# ---- Follow-up to the greeting menu ----
|
| 1029 |
+
if _is_greeting_menu_followup(question, history):
|
| 1030 |
+
answer = _greeting_menu_clarify_answer()
|
| 1031 |
+
elapsed = time.time() - start
|
| 1032 |
+
return {
|
| 1033 |
+
"answer": answer,
|
| 1034 |
+
"intent": "social_greeting",
|
| 1035 |
+
"tools_used": [],
|
| 1036 |
+
"search_results": [],
|
| 1037 |
+
"sources": [],
|
| 1038 |
+
"model_used": req.model,
|
| 1039 |
+
"response_time": round(elapsed, 2),
|
| 1040 |
+
"corrected_query": question,
|
| 1041 |
+
"natural_query": question,
|
| 1042 |
+
"database_query": question,
|
| 1043 |
+
"original_question": question,
|
| 1044 |
+
"is_follow_up": True,
|
| 1045 |
+
"source_mode": "social",
|
| 1046 |
+
}
|
| 1047 |
+
|
| 1048 |
|
| 1049 |
if _looks_library_hours_question(question):
|
| 1050 |
elapsed = time.time() - start
|