Upload 2 files
Browse filesFiles changed (2)
- app.py+14
- pipeline.py+28-4
204 unmodified lines205206207208209210356 unmodified lines567568569570571572204 unmodified linesreturn outdef _reply(t0: float, **kw) -> dict:kw.setdefault("search_results", [])kw["response_time"] = round(time.time() - t0, 2)356 unmodified linesbase = {"intent": plan.intent, "plan": plan.model_dump(), "model_used": model, "scope": scope, "request_id": request_id}logx = {"scope": scope, "origin": origin, "request_id": request_id, "session_id": session_id}if plan.intent == "social":return _reply(t0, **base, answer=pipeline.SOCIAL_ANSWER)if plan.intent == "out_of_scope":204 unmodified lines205206207208209210211212213356 unmodified lines570571572573574575576577578579580581582583584585586204 unmodified linesreturn outCLARIFY_PREFIX = "Just to check:"def _reply(t0: float, **kw) -> dict:kw.setdefault("search_results", [])kw["response_time"] = round(time.time() - t0, 2)356 unmodified linesbase = {"intent": plan.intent, "plan": plan.model_dump(), "model_used": model, "scope": scope, "request_id": request_id}logx = {"scope": scope, "origin": origin, "request_id": request_id, "session_id": session_id}# Clarify first when the planner sees genuinely different readings (Nikesh, 30 Nov 2026). Never twice in a row,# never when the user picked a search area on purpose; each option is a full question the block asks on click.opts = [o.strip() for o in (plan.clarify_question and plan.clarify_options or []) if o and o.strip()][:4]just_asked = bool(history) and history[-1].get("role") == "assistant" and \str(history[-1].get("content", "")).startswith(CLARIFY_PREFIX)if len(opts) >= 2 and scope == "all" and not just_asked:log_query(q, "clarify", opts, 0, model, time.time() - t0, **logx)return _reply(t0, **{**base, "intent": "clarify"}, answer=f"{CLARIFY_PREFIX} {plan.clarify_question.strip()}",clarify=True, original_question=q,follow_up_suggestions=[{"label": o, "question": o, "scope": "all"} for o in opts])if plan.intent == "social":return _reply(t0, **base, answer=pipeline.SOCIAL_ANSWER)if plan.intent == "out_of_scope":
82 unmodified lines83848586878815 unmodified lines104105106107108109282 unmodified lines3923933943953963973983994004014024034044052 unmodified lines40840941041141241341482 unmodified linesunit_name: str | None = Field(default=None, description="A KU college, department or research centre named in the ""question as the unit whose people or theses are wanted, exactly as written.")want_count: bool = Field(default=False, description="True if the user asks HOW MANY (a number).")all_outputs: bool = Field(default=False, description="True for totals over ALL KU research outputs with NO topic, ""e.g. 'how many total outputs in 2026', 'how many papers did KU publish last year', ""'publications per year since 2020'. Then keyword_queries must be empty.")15 unmodified linesPure's search box is a keyword index over titles, abstracts, keywords and author names — it does NOT understand natural language.Convert the user's message into a QueryPlan.Rules:- Keyword queries are ALWAYS in English (Khazna metadata is English), even if the user writes in Arabic.- Strip filler ("can you find", "papers about", "KU", "recent", "research on"). Keep domain terms and named entities.282 unmodified linesdef _detect_totals(plan: QueryPlan, question: str) -> None:"""'How many total outputs in 2026' is a count over ALL outputs, not a keyword search for 'total output'."""if plan.intent not in ("count", "topic_search") or plan.author_name:returnif not (plan.want_count or plan.all_outputs or plan.intent == "count"):returnlow = question.lower()people = re.search(rf"\b(?:{_PEOPLE_WORDS})\b", low)if people and plan.intent == "count" or (people and re.search(r"\bhow many\b", low)and not re.search(r"\b(publish|published|wrote|written|papers?|outputs?|publications?)\b", low)):# "how many researchers does KU have" is about people, never about publicationsplan.intent, plan.keyword_queries, plan.all_outputs = "unit_people", [], Falseplan.unit_name = plan.unit_name or _unit_from_text(question) or "Khalifa University"2 unmodified lineselse "researchers" if re.search(r"\b(researchers?|scientists?|postdocs?)\b", low) else "any")returnunit_words = set(_name_tokens(plan.unit_name or ""))kws = [k for k in plan.keyword_queries if not _only_generic(" ".join(w for w in k.split() if w.lower() not in unit_words))]if plan.all_outputs or not kws:plan.intent, plan.all_outputs, plan.keyword_queries, plan.broad_query = "count", True, [], ""if re.search(r"\bopen[- ]access\b|\boa\b", low):82 unmodified lines838485868788899091929315 unmodified lines109110111112113114115116117118119120121122123282 unmodified lines4064074084094104114124134144154164174184194204214224234244254262 unmodified lines42943043143243343443543643743882 unmodified linesunit_name: str | None = Field(default=None, description="A KU college, department or research centre named in the ""question as the unit whose people or theses are wanted, exactly as written.")want_count: bool = Field(default=False, description="True if the user asks HOW MANY (a number).")clarify_question: str = Field(default="", description="ONLY when the message genuinely has two or more different ""readings that would give different answers and none is clearly most likely: one short question to the user. ""Otherwise empty.")clarify_options: list[str] = Field(default_factory=list, description="With clarify_question: 2-4 complete, ""self-contained versions of the user's question, one per reading, phrased so each can be asked as is.")all_outputs: bool = Field(default=False, description="True for totals over ALL KU research outputs with NO topic, ""e.g. 'how many total outputs in 2026', 'how many papers did KU publish last year', ""'publications per year since 2020'. Then keyword_queries must be empty.")15 unmodified linesPure's search box is a keyword index over titles, abstracts, keywords and author names — it does NOT understand natural language.Convert the user's message into a QueryPlan.Clarifying (rare):- Set clarify_question + clarify_options ONLY when the message has two or more readings that need DIFFERENT answersand none is clearly the most likely, e.g. "how many KU authors in 2026?" (people with publications in 2026, or thenumber of publications?), or a unit name that fits several units equally ("engineering": a college or a department?).- Do NOT clarify ordinary topic searches, named people, follow-ups that the conversation resolves, or questions whereone reading is clearly most likely: "how many articles published by KU authors in 2026" is a publication count;"how many researchers does KU have" is a people count. Never clarify twice in a row.- Still fill the rest of the plan with your best reading, in case the clarification is skipped.Rules:- Keyword queries are ALWAYS in English (Khazna metadata is English), even if the user writes in Arabic.- Strip filler ("can you find", "papers about", "KU", "recent", "research on"). Keep domain terms and named entities.282 unmodified linesdef _detect_totals(plan: QueryPlan, question: str) -> None:"""'How many total outputs in 2026' is a count over ALL outputs, not a keyword search for 'total output'."""low = question.lower()pubs = re.search(r"\b(publish(?:ed|es|ing)?|wrote|written|papers?|articles?|outputs?|publications?|journal papers?|"r"conference papers?|books?|chapters?|reviews?)\b", low)counting = re.search(r"\b(how many|number of|count|total)\b", low)# 30 Nov 2026: "how many articles published by KU authors this year" went to the staff count ("authors" is a# people word) and the answer called the 61 people cards "61 articles". Publication words win.if plan.intent == "unit_people" and pubs and counting and not re.search(r"\b(who|list|which)\b", low):plan.intent, plan.want_count, plan.all_outputs = "count", True, Trueplan.people_role = "any"if plan.intent not in ("count", "topic_search") or plan.author_name:returnif not (plan.want_count or plan.all_outputs or plan.intent == "count"):returnpeople = re.search(rf"\b(?:{_PEOPLE_WORDS})\b", low)if people and not pubs and (plan.intent == "count" or re.search(r"\bhow many\b", low)):# "how many researchers does KU have" is about people, never about publicationsplan.intent, plan.keyword_queries, plan.all_outputs = "unit_people", [], Falseplan.unit_name = plan.unit_name or _unit_from_text(question) or "Khalifa University"2 unmodified lineselse "researchers" if re.search(r"\b(researchers?|scientists?|postdocs?)\b", low) else "any")returnunit_words = set(_name_tokens(plan.unit_name or ""))# people words ("by KU authors", "researchers") say WHO, not WHAT: they are never a topic in a totals questionkws = [k for k in plan.keyword_queriesif not _only_generic(" ".join(w for w in k.split() if w.lower() not in unit_wordsand not re.fullmatch(rf"(?:{_PEOPLE_WORDS})", w.lower())))]if plan.all_outputs or not kws:plan.intent, plan.all_outputs, plan.keyword_queries, plan.broad_query = "count", True, [], ""if re.search(r"\bopen[- ]access\b|\boa\b", low):
app.py+14
pipeline.py+28-4