Update src/api/agent.py
Browse files- src/api/agent.py +35 -0
src/api/agent.py
CHANGED
|
@@ -1556,6 +1556,39 @@ def _light_strip_retrieval_boilerplate(text: str) -> str:
|
|
| 1556 |
return re.sub(r"\s+", " ", cleaned).strip()
|
| 1557 |
|
| 1558 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1559 |
def _sanitize_boolean_for_primo(boolean: str) -> str:
|
| 1560 |
"""Clean LLM-generated boolean so it works in PRIMO advanced search URLs."""
|
| 1561 |
if not boolean:
|
|
@@ -3496,6 +3529,8 @@ async def agent_query(request: Request, req: AgentRequest):
|
|
| 3496 |
client_state = req.client_state
|
| 3497 |
_safe_metrics_increment("agent_requests")
|
| 3498 |
response = await _agent_query_inner(question, history, model, client_state, start_time)
|
|
|
|
|
|
|
| 3499 |
_log_agent_query(question, response, start_time)
|
| 3500 |
# Append announcement banner if set in admin config
|
| 3501 |
_rcfg = _get_runtime_config()
|
|
|
|
| 1556 |
return re.sub(r"\s+", " ", cleaned).strip()
|
| 1557 |
|
| 1558 |
|
| 1559 |
+
_LEGACY_SYSTEM_PATTERNS = [
|
| 1560 |
+
# Summon — KU's old discovery system, replaced by PRIMO
|
| 1561 |
+
(
|
| 1562 |
+
re.compile(
|
| 1563 |
+
r'(you can use )?summon[™™]?\s*(to find [^.]+\.)?\s*'
|
| 1564 |
+
r'(search summon\s*)?'
|
| 1565 |
+
r'(what is summon[™™]?.*?(?:search screen|search results|find articles)[^<]*)?',
|
| 1566 |
+
re.IGNORECASE | re.DOTALL,
|
| 1567 |
+
),
|
| 1568 |
+
"PRIMO Library Discovery (https://khalifa.primo.exlibrisgroup.com/discovery/search?vid=971KUOSTAR_INST:KU)",
|
| 1569 |
+
),
|
| 1570 |
+
# Any bare Summon reference
|
| 1571 |
+
(re.compile(r'\bSummon[™™]?\b', re.IGNORECASE), "PRIMO Library Discovery"),
|
| 1572 |
+
# EDS / EBSCO Discovery Service (not the same as EBSCO databases)
|
| 1573 |
+
(re.compile(r'\bEBSCO Discovery Service\b', re.IGNORECASE), "PRIMO Library Discovery"),
|
| 1574 |
+
# Encore (old III discovery)
|
| 1575 |
+
(re.compile(r'\bEncore\b(?!\s+(?:learning|education))', re.IGNORECASE), "PRIMO Library Discovery"),
|
| 1576 |
+
]
|
| 1577 |
+
|
| 1578 |
+
|
| 1579 |
+
def _sanitize_llm_response(text: str) -> str:
|
| 1580 |
+
"""
|
| 1581 |
+
Strip legacy discovery system references from LLM output.
|
| 1582 |
+
KU migrated from Summon → PRIMO. Old KB chunks may still mention Summon
|
| 1583 |
+
and the LLM may echo them. This runs as a final pass on all agent answers.
|
| 1584 |
+
"""
|
| 1585 |
+
if not text:
|
| 1586 |
+
return text
|
| 1587 |
+
for pattern, replacement in _LEGACY_SYSTEM_PATTERNS:
|
| 1588 |
+
text = pattern.sub(replacement, text)
|
| 1589 |
+
return text
|
| 1590 |
+
|
| 1591 |
+
|
| 1592 |
def _sanitize_boolean_for_primo(boolean: str) -> str:
|
| 1593 |
"""Clean LLM-generated boolean so it works in PRIMO advanced search URLs."""
|
| 1594 |
if not boolean:
|
|
|
|
| 3529 |
client_state = req.client_state
|
| 3530 |
_safe_metrics_increment("agent_requests")
|
| 3531 |
response = await _agent_query_inner(question, history, model, client_state, start_time)
|
| 3532 |
+
# Strip legacy discovery system references (Summon etc.) from all responses
|
| 3533 |
+
response.answer = _sanitize_llm_response(response.answer)
|
| 3534 |
_log_agent_query(question, response, start_time)
|
| 3535 |
# Append announcement banner if set in admin config
|
| 3536 |
_rcfg = _get_runtime_config()
|