55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
"""投顾聊天入口的轻量意图识别。"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
from common.common_const import AGENT_INTENT_DATA_QUERY
|
|
|
|
|
|
_QUERY_ACTIONS = (
|
|
"查询",
|
|
"查一下",
|
|
"查看",
|
|
"统计",
|
|
"列出",
|
|
"显示",
|
|
"多少",
|
|
"有哪些",
|
|
"明细",
|
|
)
|
|
_DATA_TERMS = (
|
|
"持仓",
|
|
"资产",
|
|
"收益",
|
|
"交易记录",
|
|
"申购",
|
|
"赎回",
|
|
"余额",
|
|
"市值",
|
|
"份额",
|
|
"客户数据",
|
|
"账户",
|
|
)
|
|
_NON_QUERY_INTENTS = ("推荐", "调仓", "再平衡", "话术", "沟通")
|
|
|
|
|
|
def recognize_advisor_intent(query: str | None, explicit_intent: str | None = None) -> str | None:
|
|
"""返回当前投顾聊天应使用的意图;无法判断时返回 ``None``。
|
|
|
|
数据查询采用保守规则:必须命中查询动作或客户数据表达,且不能明显是
|
|
推荐、调仓或话术请求,避免把生成类请求送入 NL2SQL。
|
|
"""
|
|
if explicit_intent:
|
|
return explicit_intent
|
|
text = re.sub(r"\s+", "", query or "")
|
|
if not text or any(term in text for term in _NON_QUERY_INTENTS):
|
|
return None
|
|
has_action = any(term in text for term in _QUERY_ACTIONS)
|
|
has_data = any(term in text for term in _DATA_TERMS)
|
|
if has_data and (has_action or "客户" in text or "近一年" in text or "本月" in text):
|
|
return AGENT_INTENT_DATA_QUERY
|
|
return None
|
|
|
|
|
|
__all__ = ["recognize_advisor_intent"]
|