nikeshn commited on
Commit
50ad613
Β·
verified Β·
1 Parent(s): f2d4cec

Upload 2 files

Browse files
Files changed (2) hide show
  1. src/api/agent.py +17 -159
  2. src/api/search.py +137 -316
src/api/agent.py CHANGED
@@ -112,9 +112,6 @@ class AgentResponse(BaseModel):
112
  ai_tool_query: Optional[str] = None
113
  primo_boolean_query: Optional[str] = None
114
  primo_search_url: Optional[str] = None
115
- primo_ai_url: Optional[str] = None
116
- pubmed_search_url: Optional[str] = None
117
- contact_html: Optional[str] = None
118
 
119
 
120
  # ── Constants ─────────────────────────────────────────────────────────────────
@@ -203,10 +200,6 @@ CURRENT_INFO_RE = re.compile(
203
  r"|news|update|updated|prime minister|ceo|minister|king|ruler|who won)\b",
204
  re.IGNORECASE,
205
  )
206
- DATABASE_RECOMMENDATION_RE = re.compile(
207
- r"\b(best database|best databases|which database|which databases|recommended databases?|database should i use|databases should i use|database for|databases for)\b",
208
- re.IGNORECASE,
209
- )
210
  SUMMARY_RE = re.compile(
211
  r"\b(summar(y|ize|ise|izing|ising)|overview|explain|what is|what are|describe"
212
  r"|tell me about|give me a summary|give me an overview|give me a brief"
@@ -1038,125 +1031,29 @@ def _tool_urls(context: SearchContextPayload) -> Dict[str, str]:
1038
  }
1039
 
1040
 
 
 
 
 
 
 
 
 
 
 
1041
  def _database_guidance_for_topic(context: SearchContextPayload) -> str:
1042
  topic = _escape(context.display_topic or context.topic)
 
1043
  if context.intent == "search_medical":
1044
  return (
1045
- f"For deeper research on <strong>{topic}</strong>, the KU-subscribed AI research tools and freemium tools are listed at the end of this answer. "
1046
- "Direct links to PRIMO Library Discovery and PubMed are also provided there. "
1047
- "Some freemium tools may require account creation."
1048
  )
1049
  return (
1050
- f"For deeper research on <strong>{topic}</strong>, the KU-subscribed AI research tools and freemium tools are listed at the end of this answer. "
1051
- "Direct links to PRIMO Library Discovery and, for medical topics, PubMed are also provided there. "
1052
- "Some freemium tools may require account creation."
1053
- )
1054
-
1055
-
1056
- def _is_database_recommendation_question(question: str) -> bool:
1057
- return bool(DATABASE_RECOMMENDATION_RE.search(question or ""))
1058
-
1059
-
1060
- def _primary_contact_for_question(question: str, intent: str) -> dict:
1061
- q = (question or '').lower()
1062
- walter = {
1063
- 'label': 'Systems & website support',
1064
- 'name': 'Walter Brian Hall',
1065
- 'role': 'Digital & Technology Services Librarian / Systems Librarian',
1066
- 'email': 'walter.hall@ku.ac.ae',
1067
- 'phone': '+971 2 312 3163',
1068
- }
1069
- rani = {
1070
- 'label': 'E-resources support',
1071
- 'name': 'Rani Anand',
1072
- 'role': 'E-Resources Librarian',
1073
- 'email': 'rani.anand@ku.ac.ae',
1074
- 'phone': '+971 2 312 3935',
1075
- }
1076
- nikesh = {
1077
- 'label': 'Research support',
1078
- 'name': 'Nikesh Narayanan',
1079
- 'role': 'Research & Access Services Librarian',
1080
- 'email': 'nikesh.narayanan@ku.ac.ae',
1081
- 'phone': '+971 2 312 3980',
1082
- }
1083
- muna = {
1084
- 'label': 'Public services',
1085
- 'name': 'Muna Ahmad Mohammad Al Blooshi',
1086
- 'role': 'Manager, Public Services',
1087
- 'email': 'muna.alblooshi@ku.ac.ae',
1088
- 'phone': '+971 2 312 4212',
1089
- }
1090
-
1091
- if any(t in q for t in ['system', 'systems', 'website', 'web site', 'technical', 'technology', 'digital', 'error', 'bug', 'broken page', 'page not loading']):
1092
- return walter
1093
- if any(t in q for t in ['database access', 'off campus', 'remote access', 'proxy', 'login problem', 'login issue', 'cannot access', "can't access", 'full text', 'libkey', 'vendor', 'e-resource', 'eresource', 'subscription access']):
1094
- return rani
1095
- if intent in {'search_academic', 'search_medical'} or any(t in q for t in ['research', 'literature', 'systematic review', 'orcid', 'open access', 'apc', 'bibliometric', 'scopus', 'scival', 'impact', 'citation', 'ai tool', 'best database', 'best databases', 'which database', 'which databases']):
1096
- return nikesh
1097
- return muna
1098
-
1099
-
1100
- def _contact_footer_html(question: str, intent: str) -> str:
1101
- primary = _primary_contact_for_question(question, intent)
1102
- return (
1103
- '<div style="margin-top:14px;padding:12px 14px;background:#f8fafc;border:1px solid #dbeafe;border-radius:12px;font-size:.8rem;line-height:1.6">'
1104
- '<strong>Library contact points</strong><br>'
1105
- f'General help: <a href="{ASK_LIBRARIAN_URL}" target="_blank" style="color:#003366;font-weight:700">Ask a Librarian</a> Β· '
1106
- '<a href="mailto:libse@ku.ac.ae" style="color:#003366">libse@ku.ac.ae</a><br>'
1107
- f'{_escape(primary["label"])}: <strong>{_escape(primary["name"])}</strong> β€” {_escape(primary["role"])} Β· '
1108
- f'<a href="mailto:{primary["email"]}" style="color:#003366">{_escape(primary["email"])}</a> Β· '
1109
- f'<a href="tel:{primary["phone"].replace(" ", "")}" style="color:#003366">{_escape(primary["phone"])}</a>'
1110
- '</div>'
1111
- )
1112
-
1113
-
1114
- async def _database_recommendation_answer(question: str, history: List[ChatMessage], model: str) -> Tuple[str, List[dict]]:
1115
- try:
1116
- from app import rag_service
1117
- rag_query = f"{question} recommended databases by subject Khalifa University Library databases"
1118
- rag_results = await rag_service.hybrid_search(rag_query, top_k=5)
1119
- except Exception as e:
1120
- logger.error(f"Database recommendation search failed: {e}")
1121
- rag_results = []
1122
-
1123
- if not rag_results:
1124
- answer = (
1125
- 'I could not pull the subject database guidance from the library knowledge base right now.<br><br>'
1126
- 'A good general starting point is <strong>PRIMO Library Discovery</strong>, then move to subject databases such as Scopus, Web of Science, IEEE Xplore, ACM Digital Library, PubMed, or Embase depending on your topic.'
1127
- )
1128
- return answer + _source_badge('ku_library', 'https://library.ku.ac.ae/eresources'), []
1129
-
1130
- context_text = "\n\n---\n\n".join(r['content'] for r in rag_results)
1131
- settings = get_settings()
1132
- if not settings.openai_api_key:
1133
- answer = 'I found KU Library database guidance for this subject in the knowledge base.'
1134
- answer += _source_badge('rag', 'https://library.ku.ac.ae/eresources')
1135
- sources = [{'title': r.get('title', 'Library guide'), 'url': r.get('source', '')} for r in rag_results if r.get('source')]
1136
- return answer, sources
1137
-
1138
- system = (
1139
- 'You are LibBee, the Khalifa University Library AI Assistant. '
1140
- 'Answer as an expert academic librarian using ONLY the context provided. '
1141
- "Recommend the best KU databases for the user's subject. "
1142
- 'Start with one short sentence. Then list 3 to 6 databases using HTML <br>, with each database name in <strong> and a brief explanation of why or when to use it. '
1143
- 'Where appropriate, include PRIMO Library Discovery as a starting point and then subject-specific databases. '
1144
- 'End with one short sentence offering to search directly in PRIMO Library Discovery.'
1145
  )
1146
- try:
1147
- llm = _get_llm(model, temperature=0.2, max_tokens=420)
1148
- response = await llm.ainvoke([
1149
- {'role': 'system', 'content': system + "\n\nCONTEXT:\n" + context_text},
1150
- {'role': 'user', 'content': question},
1151
- ])
1152
- answer = response.content.strip()
1153
- except Exception as e:
1154
- logger.error(f"Database recommendation answer failed: {e}")
1155
- answer = 'I found subject-specific database guidance in the library knowledge base, but I had trouble formatting it into a recommendation right now.'
1156
-
1157
- answer += _source_badge('rag', 'https://library.ku.ac.ae/eresources')
1158
- sources = [{'title': r.get('title', 'Library guide'), 'url': r.get('source', '')} for r in rag_results if r.get('source')]
1159
- return answer, sources
1160
 
1161
 
1162
  # ── Research Snapshot ─────────────────────────────────────────────────────────
@@ -1379,7 +1276,7 @@ async def _generate_topic_follow_ups(topic: str, model: str) -> List[dict]:
1379
  if not isinstance(questions, list):
1380
  return []
1381
  return [
1382
- {"label": q.strip("?") + "?", "question": f"Find research articles on {_normalize_whitespace(q.strip().rstrip("?"))} in the context of {topic}", "action": None}
1383
  for q in questions[:3]
1384
  if isinstance(q, str) and q.strip()
1385
  ]
@@ -1619,7 +1516,6 @@ async def agent_query(request: Request, req: AgentRequest):
1619
  response_time=time.time() - start_time,
1620
  follow_up_question=follow_up_question,
1621
  follow_up_suggestions=follow_up_suggestions,
1622
- contact_html=_contact_footer_html(question, "library_info"),
1623
  )
1624
 
1625
  staff_role_match = match_staff_role(question) if should_attempt_staff_lookup(question) else None
@@ -1634,7 +1530,6 @@ async def agent_query(request: Request, req: AgentRequest):
1634
  response_time=time.time() - start_time,
1635
  follow_up_question=follow_up_question,
1636
  follow_up_suggestions=follow_up_suggestions,
1637
- contact_html=_contact_footer_html(question, "library_info"),
1638
  )
1639
 
1640
  if _is_greeting_menu_followup(question, history):
@@ -1660,7 +1555,6 @@ async def agent_query(request: Request, req: AgentRequest):
1660
  response_time=time.time() - start_time,
1661
  follow_up_question=follow_up_question,
1662
  follow_up_suggestions=follow_up_suggestions,
1663
- contact_html=_contact_footer_html(question, "library_info"),
1664
  )
1665
 
1666
  if _looks_campus_question(question):
@@ -1674,27 +1568,6 @@ async def agent_query(request: Request, req: AgentRequest):
1674
  response_time=time.time() - start_time,
1675
  follow_up_question=follow_up_question,
1676
  follow_up_suggestions=follow_up_suggestions,
1677
- contact_html=_contact_footer_html(question, "library_info"),
1678
- )
1679
-
1680
- if _is_database_recommendation_question(question):
1681
- answer, sources = await _database_recommendation_answer(question, history, model)
1682
- follow_up_question = "Would you like me to search one of these databases directly or show the AI research tools for this topic?"
1683
- follow_up_suggestions = [
1684
- {"label": "Search PRIMO Library Discovery", "question": "Search PRIMO Library Discovery for this topic"},
1685
- {"label": "Show AI research tools", "question": "Show the AI research tools for this topic"},
1686
- ]
1687
- return AgentResponse(
1688
- answer=answer,
1689
- intent="library_info",
1690
- tools_used=["database_recommendation_rag"],
1691
- sources=sources,
1692
- ask_librarian=False,
1693
- model_used=model,
1694
- response_time=time.time() - start_time,
1695
- follow_up_question=follow_up_question,
1696
- follow_up_suggestions=follow_up_suggestions,
1697
- contact_html=_contact_footer_html(question, "library_info"),
1698
  )
1699
 
1700
  is_follow_up, follow_up_action, base_context = _detect_follow_up(question, client_state)
@@ -1735,9 +1608,6 @@ async def agent_query(request: Request, req: AgentRequest):
1735
  follow_up_suggestions=follow_up_suggestions,
1736
  search_context=_context_to_dict(context),
1737
  is_follow_up=True,
1738
- primo_ai_url=f"{PRIMO_AI_URL}{quote(context.ai_tool_query or context.topic)}",
1739
- pubmed_search_url=(f"https://pubmed.ncbi.nlm.nih.gov/?term={quote(context.topic)}" if context.intent == "search_medical" else None),
1740
- contact_html=_contact_footer_html(question, context.intent),
1741
  )
1742
 
1743
  answer, search_results, sources, source_url = await _run_search_mode(question, context, model, True)
@@ -1760,9 +1630,6 @@ async def agent_query(request: Request, req: AgentRequest):
1760
  follow_up_suggestions=follow_up_suggestions,
1761
  search_context=_context_to_dict(context),
1762
  is_follow_up=True,
1763
- primo_ai_url=f"{PRIMO_AI_URL}{quote(context.ai_tool_query or context.topic)}",
1764
- pubmed_search_url=(f"https://pubmed.ncbi.nlm.nih.gov/?term={quote(context.topic)}" if context.intent == "search_medical" else None),
1765
- contact_html=_contact_footer_html(question, context.intent),
1766
  )
1767
 
1768
  cls_result = await _llm_classify(question, history, model)
@@ -1818,7 +1685,6 @@ async def agent_query(request: Request, req: AgentRequest):
1818
  response_time=time.time() - start_time,
1819
  follow_up_question=follow_up_question,
1820
  follow_up_suggestions=follow_up_suggestions,
1821
- contact_html=_contact_footer_html(question, "library_info"),
1822
  )
1823
 
1824
  if intent in ("search_academic", "search_medical"):
@@ -1868,9 +1734,6 @@ async def agent_query(request: Request, req: AgentRequest):
1868
  follow_up_question=follow_up_question,
1869
  follow_up_suggestions=follow_up_suggestions,
1870
  search_context=_context_to_dict(context),
1871
- primo_ai_url=f"{PRIMO_AI_URL}{quote(context.ai_tool_query or context.topic)}",
1872
- pubmed_search_url=(f"https://pubmed.ncbi.nlm.nih.gov/?term={quote(context.topic)}" if intent == "search_medical" else None),
1873
- contact_html=_contact_footer_html(question, intent),
1874
  )
1875
 
1876
  answer, search_results, sources, source_url = await _run_search_mode(question, context, model, False)
@@ -1892,9 +1755,6 @@ async def agent_query(request: Request, req: AgentRequest):
1892
  follow_up_question=follow_up_question,
1893
  follow_up_suggestions=follow_up_suggestions,
1894
  search_context=_context_to_dict(context),
1895
- primo_ai_url=f"{PRIMO_AI_URL}{quote(context.ai_tool_query or context.topic)}",
1896
- pubmed_search_url=(f"https://pubmed.ncbi.nlm.nih.gov/?term={quote(context.topic)}" if intent == "search_medical" else None),
1897
- contact_html=_contact_footer_html(question, intent),
1898
  )
1899
 
1900
  if intent == "general_recent":
@@ -1908,7 +1768,6 @@ async def agent_query(request: Request, req: AgentRequest):
1908
  response_time=time.time() - start_time,
1909
  follow_up_question=follow_up_question,
1910
  follow_up_suggestions=follow_up_suggestions,
1911
- contact_html=_contact_footer_html(question, "general_recent"),
1912
  )
1913
 
1914
  answer = await _web_search_answer(question, history, model) + _source_badge("web")
@@ -1921,7 +1780,6 @@ async def agent_query(request: Request, req: AgentRequest):
1921
  response_time=time.time() - start_time,
1922
  follow_up_question=follow_up_question,
1923
  follow_up_suggestions=follow_up_suggestions,
1924
- contact_html=_contact_footer_html(question, "general"),
1925
  )
1926
 
1927
 
 
112
  ai_tool_query: Optional[str] = None
113
  primo_boolean_query: Optional[str] = None
114
  primo_search_url: Optional[str] = None
 
 
 
115
 
116
 
117
  # ── Constants ─────────────────────────────────────────────────────────────────
 
200
  r"|news|update|updated|prime minister|ceo|minister|king|ruler|who won)\b",
201
  re.IGNORECASE,
202
  )
 
 
 
 
203
  SUMMARY_RE = re.compile(
204
  r"\b(summar(y|ize|ise|izing|ising)|overview|explain|what is|what are|describe"
205
  r"|tell me about|give me a summary|give me an overview|give me a brief"
 
1031
  }
1032
 
1033
 
1034
+ def _ai_source_links_html(context: SearchContextPayload) -> str:
1035
+ urls = _tool_urls(context)
1036
+ return (
1037
+ f'<a href="{urls["leapspace"]}" target="_blank" style="color:#9a3412;font-weight:700;text-decoration:none">LeapSpace</a> Β· '
1038
+ f'<a href="{urls["scopus_ai"]}" target="_blank" style="color:#1e40af;font-weight:700;text-decoration:none">Scopus AI</a> Β· '
1039
+ f'<a href="{urls["consensus"]}" target="_blank" style="color:#86198f;font-weight:700;text-decoration:none">Consensus</a> Β· '
1040
+ f'<a href="{urls["primo_discovery"]}" target="_blank" style="color:#5b21b6;font-weight:700;text-decoration:none">PRIMO Library Discovery</a>'
1041
+ )
1042
+
1043
+
1044
  def _database_guidance_for_topic(context: SearchContextPayload) -> str:
1045
  topic = _escape(context.display_topic or context.topic)
1046
+ links = _ai_source_links_html(context)
1047
  if context.intent == "search_medical":
1048
  return (
1049
+ f"For a stronger evidence trail on <strong>{topic}</strong>, start with <strong>PubMed</strong> or <strong>Embase</strong> for the core medical literature, "
1050
+ f"then continue with {links}."
 
1051
  )
1052
  return (
1053
+ f"For deeper work on <strong>{topic}</strong>, continue with {links}. "
1054
+ "Use Scopus AI for broader scholarly mapping, LeapSpace for guided evidence exploration, "
1055
+ "Consensus for quick paper-grounded answers, and PRIMO Library Discovery for KU-accessible material."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1056
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1057
 
1058
 
1059
  # ── Research Snapshot ─────────────────────────────────────────────────────────
 
1276
  if not isinstance(questions, list):
1277
  return []
1278
  return [
1279
+ {"label": q.strip("?") + "?", "question": q.strip("?") + " β€” find articles", "action": "new_search"}
1280
  for q in questions[:3]
1281
  if isinstance(q, str) and q.strip()
1282
  ]
 
1516
  response_time=time.time() - start_time,
1517
  follow_up_question=follow_up_question,
1518
  follow_up_suggestions=follow_up_suggestions,
 
1519
  )
1520
 
1521
  staff_role_match = match_staff_role(question) if should_attempt_staff_lookup(question) else None
 
1530
  response_time=time.time() - start_time,
1531
  follow_up_question=follow_up_question,
1532
  follow_up_suggestions=follow_up_suggestions,
 
1533
  )
1534
 
1535
  if _is_greeting_menu_followup(question, history):
 
1555
  response_time=time.time() - start_time,
1556
  follow_up_question=follow_up_question,
1557
  follow_up_suggestions=follow_up_suggestions,
 
1558
  )
1559
 
1560
  if _looks_campus_question(question):
 
1568
  response_time=time.time() - start_time,
1569
  follow_up_question=follow_up_question,
1570
  follow_up_suggestions=follow_up_suggestions,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1571
  )
1572
 
1573
  is_follow_up, follow_up_action, base_context = _detect_follow_up(question, client_state)
 
1608
  follow_up_suggestions=follow_up_suggestions,
1609
  search_context=_context_to_dict(context),
1610
  is_follow_up=True,
 
 
 
1611
  )
1612
 
1613
  answer, search_results, sources, source_url = await _run_search_mode(question, context, model, True)
 
1630
  follow_up_suggestions=follow_up_suggestions,
1631
  search_context=_context_to_dict(context),
1632
  is_follow_up=True,
 
 
 
1633
  )
1634
 
1635
  cls_result = await _llm_classify(question, history, model)
 
1685
  response_time=time.time() - start_time,
1686
  follow_up_question=follow_up_question,
1687
  follow_up_suggestions=follow_up_suggestions,
 
1688
  )
1689
 
1690
  if intent in ("search_academic", "search_medical"):
 
1734
  follow_up_question=follow_up_question,
1735
  follow_up_suggestions=follow_up_suggestions,
1736
  search_context=_context_to_dict(context),
 
 
 
1737
  )
1738
 
1739
  answer, search_results, sources, source_url = await _run_search_mode(question, context, model, False)
 
1755
  follow_up_question=follow_up_question,
1756
  follow_up_suggestions=follow_up_suggestions,
1757
  search_context=_context_to_dict(context),
 
 
 
1758
  )
1759
 
1760
  if intent == "general_recent":
 
1768
  response_time=time.time() - start_time,
1769
  follow_up_question=follow_up_question,
1770
  follow_up_suggestions=follow_up_suggestions,
 
1771
  )
1772
 
1773
  answer = await _web_search_answer(question, history, model) + _source_badge("web")
 
1780
  response_time=time.time() - start_time,
1781
  follow_up_question=follow_up_question,
1782
  follow_up_suggestions=follow_up_suggestions,
 
1783
  )
1784
 
1785
 
src/api/search.py CHANGED
@@ -1,6 +1,14 @@
 
 
 
 
 
 
 
 
 
1
  import logging
2
- import re
3
- from typing import Optional, List, Dict
4
  from urllib.parse import quote
5
 
6
  import httpx
@@ -12,68 +20,6 @@ from src.config import get_settings
12
  logger = logging.getLogger(__name__)
13
  router = APIRouter()
14
 
15
- PRIMO_BASE_URL = "https://khalifa.primo.exlibrisgroup.com/discovery/search?vid=971KUOSTAR_INST:KU"
16
- PRIMO_AI_URL = "https://khalifa.primo.exlibrisgroup.com/discovery/researchAssistant?vid=971KUOSTAR_INST:KU&query="
17
-
18
- _RESOURCE_NOISE_RE = re.compile(
19
- r"\b(articles?|papers?|books?|ebooks?|journals?|publications?|monographs?"
20
- r"|peer[- ]reviewed|open[- ]access|studies|study|research|latest|recent|current"
21
- r"|summary|summarize|summarise|overview|brief|analysis|review|deep|dive)\b",
22
- re.IGNORECASE,
23
- )
24
-
25
- _MEDICAL_HINT_RE = re.compile(
26
- r"\b(pubmed|embase|cinahl|clinicalkey|cochrane|uptodate|medline|medicine|medical|health|clinical|nursing|pharmacy|biomedical|disease|therapy|treatment|diagnosis|patient|cancer|diabetes|epidemiology|oncology|cardiology)\b",
27
- re.IGNORECASE,
28
- )
29
-
30
- _PREFIXES = [
31
- "summarize the latest studies in ", "summarize latest studies in ",
32
- "summarize the latest research on ", "summarize latest research on ",
33
- "summarize recent studies in ", "summarize recent research on ",
34
- "latest studies in ", "latest research on ", "recent studies in ", "recent research on ",
35
- "give me a summary of ", "give me an overview of ", "give me a brief on ",
36
- "give me articles on ", "peer reviewed papers on ", "peer-reviewed papers on ", "peer reviewed articles on ", "peer-reviewed articles on ",
37
- "find peer reviewed articles on ", "find peer-reviewed articles on ",
38
- "find articles on ", "find papers on ", "find books on ",
39
- "search for articles on ", "search for papers on ", "look for articles on ",
40
- "i need articles on ", "i need books on ", "research on ", "literature on ",
41
- "tell me about ", "explain ", "what is ", "what are ",
42
- ]
43
-
44
- PHRASE_SYNONYMS: List[tuple[str, List[str]]] = [
45
- ("global warming", ["climate change"]),
46
- ("climate change", ["global warming"]),
47
- ("machine learning", ["artificial intelligence", "AI"]),
48
- ("artificial intelligence", ["AI", "machine learning"]),
49
- ("higher education", ["universities", "tertiary education"]),
50
- ("university libraries", ["academic libraries", "academic library"]),
51
- ("university library", ["academic library"]),
52
- ("renewable energy", ["sustainable energy", "clean energy"]),
53
- ("solar energy", ["solar power", "photovoltaics"]),
54
- ("cyber security", ["cybersecurity", "information security"]),
55
- ("internet of things", ["IoT"]),
56
- ("big data", ["data analytics"]),
57
- ("public health", ["population health"]),
58
- ("gene editing", ["CRISPR", "genome editing"]),
59
- ("mental health", ["psychological wellbeing", "wellbeing"]),
60
- ("supply chain", ["logistics"]),
61
- ("academic writing", ["scholarly writing"]),
62
- ]
63
-
64
- TOKEN_SYNONYMS: Dict[str, List[str]] = {
65
- "healthcare": ["medicine", "clinical"],
66
- "agriculture": ["farming"],
67
- "education": ["teaching", "learning"],
68
- "student": ["students"],
69
- "students": ["student"],
70
- "library": ["libraries"],
71
- "libraries": ["library"],
72
- "energy": ["power"],
73
- "sustainability": ["sustainable development"],
74
- "older": ["aging"],
75
- }
76
-
77
 
78
  class SearchRequest(BaseModel):
79
  query: str
@@ -83,8 +29,8 @@ class SearchRequest(BaseModel):
83
  open_access: bool = False
84
  year_from: Optional[str] = None
85
  year_to: Optional[str] = None
86
- resource_type: str = "articles" # articles | books | both
87
- boolean_query: Optional[str] = None
88
 
89
  @field_validator("query")
90
  @classmethod
@@ -114,21 +60,12 @@ class SearchRequest(BaseModel):
114
  @field_validator("resource_type")
115
  @classmethod
116
  def valid_resource_type(cls, v: str) -> str:
117
- if v not in {"articles", "books", "both"}:
 
118
  return "articles"
119
  return v
120
 
121
 
122
- class SearchPlanRequest(BaseModel):
123
- question: str
124
- peer_reviewed: bool = False
125
- open_access: bool = False
126
- year_from: Optional[str] = None
127
- year_to: Optional[str] = None
128
- resource_type: Optional[str] = None
129
-
130
-
131
-
132
  def _safe_metrics_increment(key: str) -> None:
133
  try:
134
  from app import metrics_service
@@ -145,196 +82,6 @@ def _safe_metrics_bucket(bucket: str, key: str) -> None:
145
  return
146
 
147
 
148
- def _normalize(text: str) -> str:
149
- return re.sub(r"\s+", " ", (text or "").strip())
150
-
151
-
152
- def strip_resource_noise(query: str) -> str:
153
- cleaned = _RESOURCE_NOISE_RE.sub(" ", query or "")
154
- cleaned = re.sub(r"\b(last|past)\s+\d+\s+years?\b", " ", cleaned, flags=re.IGNORECASE)
155
- cleaned = re.sub(r"\b(19\d{2}|20\d{2})\b", " ", cleaned)
156
- cleaned = re.sub(r"\s+(AND|OR)\s+(AND|OR)\s+", " AND ", cleaned, flags=re.IGNORECASE)
157
- cleaned = re.sub(r"^\s*(AND|OR)\s+", "", cleaned, flags=re.IGNORECASE)
158
- cleaned = re.sub(r"\s+(AND|OR)\s*$", "", cleaned, flags=re.IGNORECASE)
159
- return _normalize(cleaned).strip(".?,")
160
-
161
-
162
- def _light_correct_question(question: str) -> str:
163
- q = _normalize(question)
164
- q = re.sub(r"\bpeer reviewd\b", "peer reviewed", q, flags=re.IGNORECASE)
165
- q = re.sub(r"\bpubmedd\b", "PubMed", q, flags=re.IGNORECASE)
166
- q = q.rstrip(".?")
167
- if q and not q.endswith("."):
168
- q += "."
169
- return q
170
-
171
-
172
- def extract_topic_from_question(question: str) -> str:
173
- q = _normalize(question)
174
- lower = q.lower()
175
- for prefix in _PREFIXES:
176
- if lower.startswith(prefix):
177
- q = q[len(prefix):]
178
- break
179
- q = strip_resource_noise(q)
180
- q = re.sub(r"^(on|about|for)\s+", "", q, flags=re.IGNORECASE)
181
- return _normalize(q).strip(".?") or _normalize(question).strip(".?")
182
-
183
-
184
- def derive_resource_type(question: str, explicit: Optional[str] = None) -> str:
185
- if explicit in {"articles", "books", "both"}:
186
- return explicit
187
- q = (question or "").lower()
188
- if any(t in q for t in ["book", "books", "ebook", "ebooks"]):
189
- return "books"
190
- if any(t in q for t in ["article", "articles", "paper", "papers", "journal", "journals", "study", "studies"]):
191
- return "articles"
192
- return "articles"
193
-
194
-
195
- def is_medical_query(question: str, topic: str = "") -> bool:
196
- return bool(_MEDICAL_HINT_RE.search(f"{question} {topic}"))
197
-
198
-
199
- def _quote_term(term: str) -> str:
200
- term = _normalize(term)
201
- if not term:
202
- return ""
203
- if re.fullmatch(r"[A-Z]{2,6}", term):
204
- return term
205
- if re.fullmatch(r"[A-Za-z0-9_-]+", term):
206
- return f'"{term}"'
207
- return f'"{term}"'
208
-
209
-
210
- def _dedupe_keep_order(items: List[str]) -> List[str]:
211
- seen = set()
212
- out = []
213
- for item in items:
214
- norm = item.lower().strip()
215
- if not norm or norm in seen:
216
- continue
217
- seen.add(norm)
218
- out.append(item)
219
- return out
220
-
221
-
222
- def _build_concept_groups(topic: str) -> List[List[str]]:
223
- work = f" {_normalize(topic).lower()} "
224
- groups: List[List[str]] = []
225
- phrase_keys = sorted(PHRASE_SYNONYMS, key=lambda x: len(x[0]), reverse=True)
226
- for phrase, syns in phrase_keys:
227
- needle = f" {phrase} "
228
- if needle in work:
229
- groups.append(_dedupe_keep_order([phrase] + syns))
230
- work = work.replace(needle, " ")
231
-
232
- leftovers = [w for w in re.findall(r"[a-z0-9]+", work) if len(w) > 2 and w not in {
233
- "and", "or", "for", "with", "from", "into", "onto", "about", "using", "based", "among",
234
- "between", "effects", "effect", "impact", "role", "use", "uses", "application", "applications",
235
- }]
236
- for token in leftovers:
237
- groups.append(_dedupe_keep_order([token] + TOKEN_SYNONYMS.get(token, [])))
238
-
239
- if not groups:
240
- cleaned = strip_resource_noise(topic)
241
- if cleaned:
242
- groups = [[cleaned]]
243
- return groups
244
-
245
-
246
- def build_boolean_query(topic: str) -> str:
247
- groups = _build_concept_groups(topic)
248
- parts = []
249
- for group in groups[:4]:
250
- tokens = [_quote_term(term) for term in group if term]
251
- tokens = [t for t in tokens if t]
252
- if not tokens:
253
- continue
254
- if len(tokens) == 1:
255
- parts.append(f"({tokens[0]})")
256
- else:
257
- parts.append("(" + " OR ".join(tokens) + ")")
258
- return " AND ".join(parts) if parts else f'({_quote_term(topic)})'
259
-
260
-
261
- def build_pubmed_url(query: str, year_from: Optional[str] = None, year_to: Optional[str] = None) -> str:
262
- url = f"https://pubmed.ncbi.nlm.nih.gov/?term={quote(strip_resource_noise(query) or query)}"
263
- if year_from or year_to:
264
- yf = year_from or "1900"
265
- yt = year_to or "3000"
266
- url += f"&filter=datesearch.y_custom&dates.from={yf}&dates.to={yt}"
267
- return url
268
-
269
-
270
- def build_primo_discovery_url(boolean_query: str, resource_type: str = "both", peer_reviewed: bool = False,
271
- open_access: bool = False, year_from: Optional[str] = None, year_to: Optional[str] = None) -> str:
272
- facets = []
273
- if resource_type == "articles":
274
- facets.append("rtype,include,articles")
275
- tab = "CentralIndex"
276
- scope = "CentralIndex"
277
- elif resource_type == "books":
278
- facets.append("rtype,include,books")
279
- tab = "Everything"
280
- scope = "MyInst_and_CI"
281
- else:
282
- tab = "Everything"
283
- scope = "MyInst_and_CI"
284
- if peer_reviewed:
285
- facets.append("tlevel,include,peer_reviewed")
286
- if open_access:
287
- facets.append("tlevel,include,online_resources")
288
- if year_from or year_to:
289
- yf = year_from or "0001"
290
- yt = year_to or "9999"
291
- facets.append(f"searchcreationdate,include,{yf}|,|{yt}")
292
- url = f"{PRIMO_BASE_URL}&query=any,contains,{quote(boolean_query)}&lang=en&mode=advanced&offset=0&tab={tab}&search_scope={scope}"
293
- for facet in facets:
294
- url += f"&facet={quote(facet)}"
295
- return url
296
-
297
-
298
- def build_search_plan(question: str, peer_reviewed: bool = False, open_access: bool = False,
299
- year_from: Optional[str] = None, year_to: Optional[str] = None,
300
- resource_type: Optional[str] = None) -> dict:
301
- corrected_question = _light_correct_question(question)
302
- topic = extract_topic_from_question(corrected_question)
303
- resource = derive_resource_type(corrected_question, resource_type)
304
- medical = is_medical_query(corrected_question, topic)
305
- bool_query = build_boolean_query(topic)
306
- all_url = build_primo_discovery_url(bool_query, "both", peer_reviewed, open_access, year_from, year_to)
307
- art_url = build_primo_discovery_url(bool_query, "articles", peer_reviewed, open_access, year_from, year_to)
308
- book_url = build_primo_discovery_url(bool_query, "books", peer_reviewed, open_access, year_from, year_to)
309
- return {
310
- "question": corrected_question,
311
- "topic": topic,
312
- "display_topic": topic,
313
- "resource_type": resource,
314
- "is_medical": medical,
315
- "ai_tool_query": corrected_question,
316
- "primo_boolean_query": bool_query,
317
- "primo_all_url": all_url,
318
- "primo_articles_url": art_url,
319
- "primo_books_url": book_url,
320
- "primo_ai_url": f"{PRIMO_AI_URL}{quote(corrected_question)}",
321
- "pubmed_url": build_pubmed_url(topic, year_from, year_to) if medical else "",
322
- "openalex_url": f"https://openalex.org/works?page=1&sort=relevance_score:desc&search.semantic={quote(corrected_question.strip().replace(' ', '+'))}",
323
- }
324
-
325
-
326
- @router.post("/plan")
327
- async def search_plan_endpoint(req: SearchPlanRequest):
328
- return build_search_plan(
329
- req.question,
330
- peer_reviewed=req.peer_reviewed,
331
- open_access=req.open_access,
332
- year_from=req.year_from,
333
- year_to=req.year_to,
334
- resource_type=req.resource_type,
335
- )
336
-
337
-
338
  @router.post("")
339
  async def search_endpoint(req: SearchRequest):
340
  _safe_metrics_increment("search_requests")
@@ -347,65 +94,80 @@ async def search_endpoint(req: SearchRequest):
347
  return await search_scholar(req)
348
 
349
 
 
 
350
  async def search_primo(req: SearchRequest, api_key: str):
 
 
 
 
 
 
 
 
 
 
 
351
  if not api_key:
352
  logger.warning("PRIMO_API_KEY not configured")
353
  _safe_metrics_bucket("errors", "primo_missing_api_key")
354
  return {"error": "PRIMO_API_KEY not configured", "results": [], "source": "PRIMO"}
355
 
356
- clean_query = strip_resource_noise(req.query) or req.query
357
- use_boolean = bool(req.boolean_query and req.boolean_query.strip())
358
- actual_query = req.boolean_query.strip() if use_boolean else clean_query
359
-
360
- def make_url(q: str) -> str:
361
- query_encoded = quote(q)
362
- url = (
363
- f"https://api-eu.hosted.exlibrisgroup.com/primo/v1/search"
364
- f"?vid=971KUOSTAR_INST:KU&tab=Everything&scope=MyInst_and_CI"
365
- f"&q=any,contains,{query_encoded}&lang=en&sort=rank&limit={req.limit}"
366
- f"&offset=0&mode=advanced&apikey={api_key}"
367
- )
368
- facets = []
369
- if req.resource_type == "articles":
370
- facets.append("facet_rtype,include,articles")
371
- elif req.resource_type == "books":
372
- facets.append("facet_rtype,include,books")
373
- if req.peer_reviewed:
374
- facets.append("facet_tlevel,include,peer_reviewed")
375
- if req.open_access:
376
- facets.append("facet_tlevel,include,online_resources")
377
- if req.year_from or req.year_to:
378
- yf = req.year_from or "0001"
379
- yt = req.year_to or "9999"
380
- facets.append(f"facet_searchcreationdate,include,{yf}|,|{yt}")
381
- for facet in facets:
382
- url += f"&multiFacets={quote(facet)}"
383
- return url
384
-
385
- logger.info("PRIMO actual_query=%r clean_query=%r boolean=%s", actual_query, clean_query, use_boolean)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
 
387
  try:
388
  async with httpx.AsyncClient(timeout=20) as client:
389
- r = await client.get(make_url(actual_query), headers={"Accept": "application/json"})
390
- if r.status_code == 400 and use_boolean:
391
- logger.warning("PRIMO rejected boolean query; falling back to clean keywords")
392
- r = await client.get(make_url(clean_query), headers={"Accept": "application/json"})
393
- actual_query = clean_query
394
  logger.info("PRIMO status=%s", r.status_code)
395
  if r.status_code != 200:
396
  _safe_metrics_bucket("errors", f"primo_{r.status_code}")
397
  return {"error": f"PRIMO API error: {r.status_code}", "results": [], "source": "PRIMO"}
 
398
  data = r.json()
399
  docs = data.get("docs", [])
400
- display_query = req.boolean_query.strip() if req.boolean_query else clean_query
401
- discovery_url = build_primo_discovery_url(
402
- display_query,
403
- req.resource_type,
404
- req.peer_reviewed,
405
- req.open_access,
406
- req.year_from,
407
- req.year_to,
408
- )
409
  results = []
410
  for doc in docs[: req.limit]:
411
  pnx = doc.get("pnx", {})
@@ -416,10 +178,18 @@ async def search_primo(req: SearchRequest, api_key: str):
416
  "date": (d.get("date") or [""])[0],
417
  "type": (d.get("type") or [""])[0],
418
  "source": (d.get("source") or [""])[0],
419
- "primo_url": discovery_url,
 
 
 
420
  "_source": "PRIMO",
421
  })
422
- return {"total": data.get("info", {}).get("total", 0), "results": results, "source": "PRIMO"}
 
 
 
 
 
423
  except httpx.TimeoutException:
424
  _safe_metrics_bucket("errors", "primo_timeout")
425
  return {"error": "PRIMO API timed out β€” please try again", "results": [], "source": "PRIMO"}
@@ -429,18 +199,58 @@ async def search_primo(req: SearchRequest, api_key: str):
429
  return {"error": str(exc), "results": [], "source": "PRIMO"}
430
 
431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  async def search_pubmed(req: SearchRequest):
433
- clean_query = strip_resource_noise(req.query) or req.query
 
 
 
 
 
 
 
 
 
434
  term = clean_query
435
  if req.peer_reviewed:
436
  term = f"({term}) AND Journal Article[pt]"
 
437
  query_encoded = quote(term)
 
 
438
  date_params = ""
439
  if req.year_from or req.year_to:
440
  yf = req.year_from or "1900"
441
  yt = req.year_to or "3000"
442
  date_params = f"&mindate={yf}&maxdate={yt}&datetype=pdat"
443
- logger.info("PubMed term=%r peer_reviewed=%s year=%s-%s", term, req.peer_reviewed, req.year_from, req.year_to)
 
 
 
 
 
444
  try:
445
  async with httpx.AsyncClient(timeout=15) as client:
446
  search_url = (
@@ -451,9 +261,11 @@ async def search_pubmed(req: SearchRequest):
451
  if r.status_code != 200:
452
  _safe_metrics_bucket("errors", "pubmed_search_failed")
453
  return {"error": "PubMed search failed", "results": [], "source": "PubMed"}
 
454
  ids = r.json().get("esearchresult", {}).get("idlist", [])
455
  if not ids:
456
  return {"total": 0, "results": [], "source": "PubMed"}
 
457
  fetch_url = (
458
  f"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
459
  f"?db=pubmed&id={','.join(ids)}&retmode=json"
@@ -462,6 +274,7 @@ async def search_pubmed(req: SearchRequest):
462
  if r2.status_code != 200:
463
  _safe_metrics_bucket("errors", "pubmed_fetch_failed")
464
  return {"error": "PubMed fetch failed", "results": [], "source": "PubMed"}
 
465
  data = r2.json().get("result", {})
466
  results = []
467
  for pid in ids:
@@ -483,12 +296,15 @@ async def search_pubmed(req: SearchRequest):
483
  "_source": "PubMed",
484
  })
485
  return {"total": len(ids), "results": results, "source": "PubMed"}
 
486
  except Exception as exc:
487
  logger.error("PubMed search error: %s", exc)
488
  _safe_metrics_bucket("errors", "pubmed_exception")
489
  return {"error": str(exc), "results": [], "source": "PubMed"}
490
 
491
 
 
 
492
  async def search_scholar(req: SearchRequest):
493
  try:
494
  params = {
@@ -497,10 +313,14 @@ async def search_scholar(req: SearchRequest):
497
  "fields": "title,authors,year,venue,externalIds,citationCount",
498
  }
499
  async with httpx.AsyncClient(timeout=15) as client:
500
- r = await client.get("https://api.semanticscholar.org/graph/v1/paper/search", params=params)
 
 
 
501
  if r.status_code != 200:
502
  _safe_metrics_bucket("errors", f"semantic_{r.status_code}")
503
  return {"error": f"Semantic Scholar API: {r.status_code}", "results": [], "source": "Semantic Scholar"}
 
504
  data = r.json()
505
  results = []
506
  for paper in data.get("data", []):
@@ -520,6 +340,7 @@ async def search_scholar(req: SearchRequest):
520
  "_source": "Semantic Scholar",
521
  })
522
  return {"total": data.get("total", 0), "results": results, "source": "Semantic Scholar"}
 
523
  except Exception as exc:
524
  logger.error("Semantic Scholar search error: %s", exc)
525
  _safe_metrics_bucket("errors", "semantic_exception")
 
1
+ """
2
+ search.py β€” LibBee v3
3
+ Key fixes:
4
+ - PRIMO: resource type (articles/books) sent as rtype FACET, not q= keyword
5
+ - PRIMO: year filter sent as facet_searchcreationdate FACET, not keyword
6
+ - PubMed: year filter wired to mindate/maxdate params (was silently ignored before)
7
+ - PubMed: peer_reviewed mapped to free full text filter
8
+ - query sent to both APIs is clean topic keywords only β€” no rtype noise
9
+ """
10
  import logging
11
+ from typing import Optional
 
12
  from urllib.parse import quote
13
 
14
  import httpx
 
20
  logger = logging.getLogger(__name__)
21
  router = APIRouter()
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  class SearchRequest(BaseModel):
25
  query: str
 
29
  open_access: bool = False
30
  year_from: Optional[str] = None
31
  year_to: Optional[str] = None
32
+ # resource_type controls rtype FACET β€” NOT injected into q= keyword query
33
+ resource_type: str = "articles" # "articles" | "books" | "both"
34
 
35
  @field_validator("query")
36
  @classmethod
 
60
  @field_validator("resource_type")
61
  @classmethod
62
  def valid_resource_type(cls, v: str) -> str:
63
+ allowed = {"articles", "books", "both"}
64
+ if v not in allowed:
65
  return "articles"
66
  return v
67
 
68
 
 
 
 
 
 
 
 
 
 
 
69
  def _safe_metrics_increment(key: str) -> None:
70
  try:
71
  from app import metrics_service
 
82
  return
83
 
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  @router.post("")
86
  async def search_endpoint(req: SearchRequest):
87
  _safe_metrics_increment("search_requests")
 
94
  return await search_scholar(req)
95
 
96
 
97
+ # ── PRIMO ──────────────────────────────────────────────────────────────────────
98
+
99
  async def search_primo(req: SearchRequest, api_key: str):
100
+ """
101
+ Send clean topic keywords to q=any,contains.
102
+ Resource type, peer review, open access, and year are all FACETS β€” never keywords.
103
+
104
+ PRIMO facet reference:
105
+ facet_rtype,include,articles β†’ journal articles
106
+ facet_rtype,include,books β†’ books / ebooks
107
+ facet_tlevel,include,peer_reviewed
108
+ facet_tlevel,include,online_resources (open access proxy)
109
+ facet_searchcreationdate,include,YYYY|,|YYYY
110
+ """
111
  if not api_key:
112
  logger.warning("PRIMO_API_KEY not configured")
113
  _safe_metrics_bucket("errors", "primo_missing_api_key")
114
  return {"error": "PRIMO_API_KEY not configured", "results": [], "source": "PRIMO"}
115
 
116
+ # ── The query sent to PRIMO must be ONLY topic keywords ──────────────────
117
+ # Strip any residual resource-type words that may have slipped through
118
+ # topic extraction (belt-and-braces defence).
119
+ clean_query = _strip_resource_noise(req.query)
120
+ if not clean_query:
121
+ clean_query = req.query # fallback if stripping removes everything
122
+
123
+ query_encoded = quote(clean_query)
124
+
125
+ url = (
126
+ f"https://api-eu.hosted.exlibrisgroup.com/primo/v1/search"
127
+ f"?vid=971KUOSTAR_INST:KU&tab=Everything&scope=MyInst_and_CI"
128
+ f"&q=any,contains,{query_encoded}&lang=en&sort=rank&limit={req.limit}"
129
+ f"&offset=0&mode=advanced&apikey={api_key}"
130
+ )
131
+
132
+ # ── Build facets β€” resource type, quality, year ───────────────────────────
133
+ facets = []
134
+
135
+ # Resource type β†’ rtype facet (NOT a keyword)
136
+ if req.resource_type == "articles":
137
+ facets.append("facet_rtype,include,articles")
138
+ elif req.resource_type == "books":
139
+ facets.append("facet_rtype,include,books")
140
+ # "both" β†’ no rtype facet (search everything)
141
+
142
+ if req.peer_reviewed:
143
+ facets.append("facet_tlevel,include,peer_reviewed")
144
+
145
+ if req.open_access:
146
+ facets.append("facet_tlevel,include,online_resources")
147
+
148
+ if req.year_from or req.year_to:
149
+ yf = req.year_from or "0001"
150
+ yt = req.year_to or "9999"
151
+ facets.append(f"facet_searchcreationdate,include,{yf}|,|{yt}")
152
+
153
+ for facet in facets:
154
+ url += f"&multiFacets={quote(facet)}"
155
+
156
+ logger.info(
157
+ "PRIMO query=%r resource_type=%s peer_reviewed=%s year=%s-%s",
158
+ clean_query, req.resource_type, req.peer_reviewed, req.year_from, req.year_to,
159
+ )
160
 
161
  try:
162
  async with httpx.AsyncClient(timeout=20) as client:
163
+ r = await client.get(url, headers={"Accept": "application/json"})
 
 
 
 
164
  logger.info("PRIMO status=%s", r.status_code)
165
  if r.status_code != 200:
166
  _safe_metrics_bucket("errors", f"primo_{r.status_code}")
167
  return {"error": f"PRIMO API error: {r.status_code}", "results": [], "source": "PRIMO"}
168
+
169
  data = r.json()
170
  docs = data.get("docs", [])
 
 
 
 
 
 
 
 
 
171
  results = []
172
  for doc in docs[: req.limit]:
173
  pnx = doc.get("pnx", {})
 
178
  "date": (d.get("date") or [""])[0],
179
  "type": (d.get("type") or [""])[0],
180
  "source": (d.get("source") or [""])[0],
181
+ "primo_url": (
182
+ f"https://khalifa.primo.exlibrisgroup.com/discovery/search"
183
+ f"?query=any,contains,{query_encoded}&vid=971KUOSTAR_INST:KU"
184
+ ),
185
  "_source": "PRIMO",
186
  })
187
+ return {
188
+ "total": data.get("info", {}).get("total", 0),
189
+ "results": results,
190
+ "source": "PRIMO",
191
+ }
192
+
193
  except httpx.TimeoutException:
194
  _safe_metrics_bucket("errors", "primo_timeout")
195
  return {"error": "PRIMO API timed out β€” please try again", "results": [], "source": "PRIMO"}
 
199
  return {"error": str(exc), "results": [], "source": "PRIMO"}
200
 
201
 
202
+ def _strip_resource_noise(query: str) -> str:
203
+ """
204
+ Remove resource-type and filter words that should be facets, not q= keywords.
205
+ Called as a belt-and-braces guard before sending to PRIMO/PubMed.
206
+ """
207
+ noise = re.compile(
208
+ r"\b(articles?|papers?|books?|ebooks?|journals?|publications?|monographs?"
209
+ r"|peer[- ]reviewed|open[- ]access|studies|study|research)\b",
210
+ re.IGNORECASE,
211
+ )
212
+ cleaned = noise.sub(" ", query)
213
+ # Collapse leftover AND/OR operators with no operands
214
+ cleaned = re.sub(r"\s+(AND|OR)\s+(AND|OR)\s+", " AND ", cleaned, flags=re.IGNORECASE)
215
+ cleaned = re.sub(r"^\s*(AND|OR)\s+", "", cleaned, flags=re.IGNORECASE)
216
+ cleaned = re.sub(r"\s+(AND|OR)\s*$", "", cleaned, flags=re.IGNORECASE)
217
+ return re.sub(r"\s+", " ", cleaned).strip()
218
+
219
+
220
+ import re # noqa: E402 (needed for _strip_resource_noise above)
221
+
222
+
223
+ # ── PubMed ─────────────────────────────────────────────────────────────────────
224
+
225
  async def search_pubmed(req: SearchRequest):
226
+ """
227
+ Year filters use mindate/maxdate params β€” NOT injected into the search term.
228
+ Peer-reviewed filter uses [pt] (publication type) in the term.
229
+ """
230
+ # Build clean term β€” topic keywords only
231
+ clean_query = _strip_resource_noise(req.query)
232
+ if not clean_query:
233
+ clean_query = req.query
234
+
235
+ # Add peer-reviewed as a PubMed publication-type filter (appended to term)
236
  term = clean_query
237
  if req.peer_reviewed:
238
  term = f"({term}) AND Journal Article[pt]"
239
+
240
  query_encoded = quote(term)
241
+
242
+ # Date params (separate from search term β€” correct PubMed approach)
243
  date_params = ""
244
  if req.year_from or req.year_to:
245
  yf = req.year_from or "1900"
246
  yt = req.year_to or "3000"
247
  date_params = f"&mindate={yf}&maxdate={yt}&datetype=pdat"
248
+
249
+ logger.info(
250
+ "PubMed term=%r peer_reviewed=%s year=%s-%s",
251
+ term, req.peer_reviewed, req.year_from, req.year_to,
252
+ )
253
+
254
  try:
255
  async with httpx.AsyncClient(timeout=15) as client:
256
  search_url = (
 
261
  if r.status_code != 200:
262
  _safe_metrics_bucket("errors", "pubmed_search_failed")
263
  return {"error": "PubMed search failed", "results": [], "source": "PubMed"}
264
+
265
  ids = r.json().get("esearchresult", {}).get("idlist", [])
266
  if not ids:
267
  return {"total": 0, "results": [], "source": "PubMed"}
268
+
269
  fetch_url = (
270
  f"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
271
  f"?db=pubmed&id={','.join(ids)}&retmode=json"
 
274
  if r2.status_code != 200:
275
  _safe_metrics_bucket("errors", "pubmed_fetch_failed")
276
  return {"error": "PubMed fetch failed", "results": [], "source": "PubMed"}
277
+
278
  data = r2.json().get("result", {})
279
  results = []
280
  for pid in ids:
 
296
  "_source": "PubMed",
297
  })
298
  return {"total": len(ids), "results": results, "source": "PubMed"}
299
+
300
  except Exception as exc:
301
  logger.error("PubMed search error: %s", exc)
302
  _safe_metrics_bucket("errors", "pubmed_exception")
303
  return {"error": str(exc), "results": [], "source": "PubMed"}
304
 
305
 
306
+ # ── Semantic Scholar ───────────────────────────────────────────────────────────
307
+
308
  async def search_scholar(req: SearchRequest):
309
  try:
310
  params = {
 
313
  "fields": "title,authors,year,venue,externalIds,citationCount",
314
  }
315
  async with httpx.AsyncClient(timeout=15) as client:
316
+ r = await client.get(
317
+ "https://api.semanticscholar.org/graph/v1/paper/search",
318
+ params=params,
319
+ )
320
  if r.status_code != 200:
321
  _safe_metrics_bucket("errors", f"semantic_{r.status_code}")
322
  return {"error": f"Semantic Scholar API: {r.status_code}", "results": [], "source": "Semantic Scholar"}
323
+
324
  data = r.json()
325
  results = []
326
  for paper in data.get("data", []):
 
340
  "_source": "Semantic Scholar",
341
  })
342
  return {"total": data.get("total", 0), "results": results, "source": "Semantic Scholar"}
343
+
344
  except Exception as exc:
345
  logger.error("Semantic Scholar search error: %s", exc)
346
  _safe_metrics_bucket("errors", "semantic_exception")