Update src/api/agent.py
Browse files- src/api/agent.py +31 -121
src/api/agent.py
CHANGED
|
@@ -2544,48 +2544,19 @@ def _highly_cited_note(topic_escaped: str) -> str:
|
|
| 2544 |
|
| 2545 |
async def _research_snapshot(context: SearchContextPayload, model: str) -> Tuple[str, List[dict], List[dict]]:
|
| 2546 |
"""
|
| 2547 |
-
Research snapshot
|
| 2548 |
-
|
| 2549 |
-
Falls back to Semantic Scholar if OpenAlex returns nothing.
|
| 2550 |
"""
|
| 2551 |
-
import asyncio as _asyncio
|
| 2552 |
topic = context.topic
|
| 2553 |
-
|
| 2554 |
-
|
| 2555 |
-
if not papers:
|
| 2556 |
-
logger.info(f"S2 returned nothing for '{topic}' β trying OpenAlex")
|
| 2557 |
-
papers = await _openalex_search(context.ai_tool_query or context.topic, limit=8)
|
| 2558 |
-
q_display = context.ai_tool_query or f"Research on {topic}"
|
| 2559 |
-
answer = _search_trace_block(q_display, context)
|
| 2560 |
-
if papers:
|
| 2561 |
-
summary = await _generate_cited_summary(topic, papers, model)
|
| 2562 |
-
if not summary:
|
| 2563 |
-
_safe_metrics_increment("summary_fallbacks")
|
| 2564 |
-
summary = _extractive_snapshot_from_papers(topic, papers)
|
| 2565 |
-
source_label = papers[0].get("_source", "OpenAlex")
|
| 2566 |
-
if source_label == "OpenAlex":
|
| 2567 |
-
oa_src_url, src_key = "https://openalex.org", "semantic_badge"
|
| 2568 |
-
elif source_label == "Consensus":
|
| 2569 |
-
oa_src_url, src_key = "https://consensus.app", "consensus_badge"
|
| 2570 |
-
else:
|
| 2571 |
-
oa_src_url, src_key = "https://www.semanticscholar.org/", "semantic"
|
| 2572 |
-
answer += f"<strong>π Research Snapshot: {_escape(topic)}</strong><br><br>{summary}"
|
| 2573 |
-
answer += "<br><br><strong>Selected references:</strong><br>"
|
| 2574 |
-
for i, paper in enumerate(papers[:6], 1):
|
| 2575 |
-
answer += (
|
| 2576 |
-
f'<br><strong>[{i}]</strong> '
|
| 2577 |
-
f'<a href="{paper["link"]}" target="_blank" style="color:#003366;font-weight:600">{_escape(paper["title"])}</a><br>'
|
| 2578 |
-
f'<span style="font-size:.8rem;color:#6b7280">'
|
| 2579 |
-
f'{_escape(paper["authors"])} Β· {paper["year"]} Β· {_escape(paper["venue"] or "")}'
|
| 2580 |
-
f"</span>"
|
| 2581 |
-
)
|
| 2582 |
-
answer += _source_badge(src_key, oa_src_url)
|
| 2583 |
-
answer += _ai_tools_footer(context)
|
| 2584 |
-
references = [{"title": p["title"], "url": p["link"]} for p in papers[:6]]
|
| 2585 |
-
return answer, [], references
|
| 2586 |
answer += (
|
| 2587 |
f"<strong>π Research starting points: {_escape(topic)}</strong><br><br>"
|
| 2588 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2589 |
)
|
| 2590 |
answer += _ai_tools_footer(context)
|
| 2591 |
return answer, [], []
|
|
@@ -2941,73 +2912,22 @@ def _search_answer_intro(context: SearchContextPayload, is_follow_up: bool) -> s
|
|
| 2941 |
async def _run_search_mode(
|
| 2942 |
question: str, context: SearchContextPayload, model: str, is_follow_up: bool
|
| 2943 |
) -> Tuple[str, List[dict], List[dict], str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2944 |
context = await _prepare_queries(question, context, model, is_follow_up)
|
| 2945 |
-
|
| 2946 |
-
|
| 2947 |
-
if context.source == "pubmed":
|
| 2948 |
-
if use_openalex:
|
| 2949 |
-
(result, pub_url), oa_papers = await _aio.gather(
|
| 2950 |
-
_search_pubmed_articles(context, limit=6),
|
| 2951 |
-
_semantic_scholar_search(context.ai_tool_query or context.topic, limit=5),
|
| 2952 |
-
)
|
| 2953 |
-
else:
|
| 2954 |
-
result, pub_url = await _search_pubmed_articles(context, limit=6)
|
| 2955 |
-
oa_papers = []
|
| 2956 |
-
source_name, source_url = "pubmed", pub_url
|
| 2957 |
-
search_results = result.get("results", []) or []
|
| 2958 |
-
total = result.get("total", len(search_results))
|
| 2959 |
-
for r in search_results:
|
| 2960 |
-
if "_source" not in r:
|
| 2961 |
-
r["_source"] = "PubMed"
|
| 2962 |
-
else:
|
| 2963 |
-
# PRIMO API removed β OpenAlex for articles, PRIMO link shown to user
|
| 2964 |
-
oa_papers = await _semantic_scholar_search(context.ai_tool_query or context.topic, limit=max(3, min(10, _get_runtime_config().get("max_results", 8)))) if use_openalex else []
|
| 2965 |
-
source_name, source_url = "openalex", _primo_clean_url(context)
|
| 2966 |
-
search_results, total = [], 0
|
| 2967 |
-
# Tag OpenAlex results
|
| 2968 |
-
for paper in oa_papers:
|
| 2969 |
-
paper["_source"] = "OpenAlex"
|
| 2970 |
-
paper["description"] = paper.get("abstract", "")[:300]
|
| 2971 |
-
paper["creator"] = paper.get("authors", "")
|
| 2972 |
-
paper["date"] = paper.get("year", "")
|
| 2973 |
-
paper["type"] = "Article"
|
| 2974 |
-
from urllib.parse import quote as _pq
|
| 2975 |
-
paper["primo_url"] = (
|
| 2976 |
-
"https://khalifa.primo.exlibrisgroup.com/discovery/search"
|
| 2977 |
-
f"?query=title,contains,{_pq(paper['title'][:120])},AND"
|
| 2978 |
-
"&tab=Everything&search_scope=MyInst_and_CI"
|
| 2979 |
-
"&vid=971KUOSTAR_INST:KU&lang=en&mode=advanced"
|
| 2980 |
-
)
|
| 2981 |
answer = _search_trace_block(question, context)
|
| 2982 |
answer += _search_answer_intro(context, is_follow_up)
|
| 2983 |
filters = _filters_summary(context)
|
| 2984 |
if filters:
|
| 2985 |
answer += f"<br><br><strong>Active filters:</strong> {_escape(filters)}."
|
| 2986 |
-
|
| 2987 |
-
if search_results:
|
| 2988 |
-
answer += f"<br><br>Found <strong>{total:,}</strong> result(s) β top {len(search_results)} shown below."
|
| 2989 |
-
else:
|
| 2990 |
-
answer += "<br><br>No PubMed results found β try the tools below."
|
| 2991 |
-
answer += _source_badge(source_name, source_url)
|
| 2992 |
-
if oa_papers:
|
| 2993 |
-
answer += _source_badge("openalex_badge", "https://www.semanticscholar.org")
|
| 2994 |
-
else:
|
| 2995 |
-
if oa_papers:
|
| 2996 |
-
answer += f"<br><br>Found <strong>{len(oa_papers)}</strong> open access result(s) via OpenAlex."
|
| 2997 |
-
else:
|
| 2998 |
-
answer += "<br><br>No results found β use the platforms and databases below."
|
| 2999 |
-
answer += _source_badge("openalex_badge", "https://www.semanticscholar.org")
|
| 3000 |
-
if context.intent == "search_medical" and context.source != "pubmed":
|
| 3001 |
-
answer += "<br><br>For medical evidence, also check <strong>PubMed</strong> or <strong>Embase</strong>."
|
| 3002 |
answer += _ai_tools_footer(context)
|
| 3003 |
-
|
| 3004 |
-
|
| 3005 |
-
for item in search_results[:6]:
|
| 3006 |
-
lnk = item.get("primo_url") or item.get("link")
|
| 3007 |
-
if lnk: sources.append({"title": item.get("title", "Result"), "url": lnk})
|
| 3008 |
-
for paper in oa_papers[:6]:
|
| 3009 |
-
if paper.get("link"): sources.append({"title": paper["title"], "url": paper["link"]})
|
| 3010 |
-
return answer, combined, sources, source_url
|
| 3011 |
|
| 3012 |
def _sort_and_trim_contexts(contexts: List[SearchContextPayload]) -> List[SearchContextPayload]:
|
| 3013 |
dedup: Dict[str, SearchContextPayload] = {}
|
|
@@ -3663,43 +3583,33 @@ async def _agent_query_inner(
|
|
| 3663 |
_prepare_queries(question, _ctx_articles, model, False),
|
| 3664 |
_prepare_queries(question, _ctx_books, model, False),
|
| 3665 |
)
|
| 3666 |
-
#
|
| 3667 |
_ctx_articles = await _prepare_queries(question, _ctx_articles, model, False)
|
| 3668 |
-
_oa_arts = await _semantic_scholar_search(_ctx_articles.ai_tool_query or topic, limit=6)
|
| 3669 |
-
for _p in _oa_arts:
|
| 3670 |
-
_p["_source"] = "OpenAlex"
|
| 3671 |
-
_p["description"] = _p.get("abstract", "")[:300]
|
| 3672 |
-
_p["creator"] = _p.get("authors", "")
|
| 3673 |
-
_p["date"] = _p.get("year", "")
|
| 3674 |
-
_p["type"] = "Article"
|
| 3675 |
-
_results_a = _oa_arts
|
| 3676 |
-
_results_b = []
|
| 3677 |
_src_url_a = _primo_clean_url(_ctx_articles)
|
| 3678 |
_src_url_b = _primo_clean_url(_ctx_books)
|
| 3679 |
|
| 3680 |
_dual_answer = _search_trace_block(question, _ctx_articles)
|
| 3681 |
-
_dual_answer +=
|
| 3682 |
-
|
| 3683 |
-
|
| 3684 |
-
|
| 3685 |
-
|
| 3686 |
-
_dual_answer += _source_badge("
|
| 3687 |
_dual_answer += f"<br><br><strong>π Books on {_escape(topic)}</strong><br>"
|
| 3688 |
_dual_answer += (
|
| 3689 |
-
"
|
| 3690 |
-
f'
|
| 3691 |
)
|
| 3692 |
_dual_answer += _source_badge("primo", _src_url_b)
|
| 3693 |
_dual_answer += _ai_tools_footer(_ctx_articles)
|
| 3694 |
-
|
| 3695 |
follow_up_question, follow_up_suggestions = await _search_follow_up(_ctx_articles, model)
|
| 3696 |
return AgentResponse(
|
| 3697 |
answer=_dual_answer,
|
| 3698 |
intent=intent,
|
| 3699 |
-
tools_used=["
|
| 3700 |
-
search_results=
|
| 3701 |
-
sources=[
|
| 3702 |
-
for r in _combined[:6] if r.get("link")],
|
| 3703 |
model_used=model,
|
| 3704 |
response_time=time.time() - start_time,
|
| 3705 |
corrected_query=topic,
|
|
|
|
| 2544 |
|
| 2545 |
async def _research_snapshot(context: SearchContextPayload, model: str) -> Tuple[str, List[dict], List[dict]]:
|
| 2546 |
"""
|
| 2547 |
+
Research snapshot β no API calls.
|
| 2548 |
+
Shows PRIMO browser link + AI tools footer only.
|
|
|
|
| 2549 |
"""
|
|
|
|
| 2550 |
topic = context.topic
|
| 2551 |
+
primo_url = _primo_clean_url(context)
|
| 2552 |
+
answer = _search_trace_block(f"Research on {topic}", context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2553 |
answer += (
|
| 2554 |
f"<strong>π Research starting points: {_escape(topic)}</strong><br><br>"
|
| 2555 |
+
"Your query has been prepared and pre-loaded across multiple platforms. "
|
| 2556 |
+
"Click any platform below to search instantly, or use the boolean search tip "
|
| 2557 |
+
"to search directly in any KU database."
|
| 2558 |
+
f'<br><br><a href="{primo_url}" target="_blank" style="color:#003366;font-weight:700">'
|
| 2559 |
+
f'Search PRIMO for: {_escape(topic)} β</a>'
|
| 2560 |
)
|
| 2561 |
answer += _ai_tools_footer(context)
|
| 2562 |
return answer, [], []
|
|
|
|
| 2912 |
async def _run_search_mode(
|
| 2913 |
question: str, context: SearchContextPayload, model: str, is_follow_up: bool
|
| 2914 |
) -> Tuple[str, List[dict], List[dict], str]:
|
| 2915 |
+
"""
|
| 2916 |
+
Search mode β no API calls.
|
| 2917 |
+
Builds PRIMO browser link + AI tools footer only.
|
| 2918 |
+
"""
|
| 2919 |
context = await _prepare_queries(question, context, model, is_follow_up)
|
| 2920 |
+
source_url = _primo_clean_url(context)
|
| 2921 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2922 |
answer = _search_trace_block(question, context)
|
| 2923 |
answer += _search_answer_intro(context, is_follow_up)
|
| 2924 |
filters = _filters_summary(context)
|
| 2925 |
if filters:
|
| 2926 |
answer += f"<br><br><strong>Active filters:</strong> {_escape(filters)}."
|
| 2927 |
+
answer += _source_badge("primo", source_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2928 |
answer += _ai_tools_footer(context)
|
| 2929 |
+
|
| 2930 |
+
return answer, [], [], source_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2931 |
|
| 2932 |
def _sort_and_trim_contexts(contexts: List[SearchContextPayload]) -> List[SearchContextPayload]:
|
| 2933 |
dedup: Dict[str, SearchContextPayload] = {}
|
|
|
|
| 3583 |
_prepare_queries(question, _ctx_articles, model, False),
|
| 3584 |
_prepare_queries(question, _ctx_books, model, False),
|
| 3585 |
)
|
| 3586 |
+
# No API β just build PRIMO links for articles and books
|
| 3587 |
_ctx_articles = await _prepare_queries(question, _ctx_articles, model, False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3588 |
_src_url_a = _primo_clean_url(_ctx_articles)
|
| 3589 |
_src_url_b = _primo_clean_url(_ctx_books)
|
| 3590 |
|
| 3591 |
_dual_answer = _search_trace_block(question, _ctx_articles)
|
| 3592 |
+
_dual_answer += (
|
| 3593 |
+
f"<strong>π Articles on {_escape(topic)}</strong><br>"
|
| 3594 |
+
f'<a href="{_src_url_a}" target="_blank" style="color:#003366;font-weight:700">'
|
| 3595 |
+
f'Search PRIMO for articles β</a>'
|
| 3596 |
+
)
|
| 3597 |
+
_dual_answer += _source_badge("primo", _src_url_a)
|
| 3598 |
_dual_answer += f"<br><br><strong>π Books on {_escape(topic)}</strong><br>"
|
| 3599 |
_dual_answer += (
|
| 3600 |
+
f'<a href="{_src_url_b}" target="_blank" style="color:#003366;font-weight:700">'
|
| 3601 |
+
f'Search PRIMO for books β</a>'
|
| 3602 |
)
|
| 3603 |
_dual_answer += _source_badge("primo", _src_url_b)
|
| 3604 |
_dual_answer += _ai_tools_footer(_ctx_articles)
|
| 3605 |
+
|
| 3606 |
follow_up_question, follow_up_suggestions = await _search_follow_up(_ctx_articles, model)
|
| 3607 |
return AgentResponse(
|
| 3608 |
answer=_dual_answer,
|
| 3609 |
intent=intent,
|
| 3610 |
+
tools_used=["primo_browser_link"],
|
| 3611 |
+
search_results=[],
|
| 3612 |
+
sources=[],
|
|
|
|
| 3613 |
model_used=model,
|
| 3614 |
response_time=time.time() - start_time,
|
| 3615 |
corrected_query=topic,
|