2026-09-13 23:46:15 +08:00
|
|
|
"""投顾聊天意图分类:LLM 优先,规则识别兜底。"""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import re
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
from common.common_const import (
|
|
|
|
|
AGENT_INTENT_CASUAL_CHAT,
|
|
|
|
|
AGENT_INTENT_DATA_QUERY,
|
|
|
|
|
AGENT_INTENT_DIALOGUE_SCRIPT,
|
|
|
|
|
AGENT_INTENT_FUND_ANALYSIS,
|
|
|
|
|
AGENT_INTENT_REBALANCE,
|
|
|
|
|
AGENT_INTENT_RECOMMEND,
|
|
|
|
|
)
|
|
|
|
|
from agent.advisor_agent.intent.recognizer import recognize_advisor_intent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VALID_INTENTS = frozenset({
|
|
|
|
|
AGENT_INTENT_RECOMMEND,
|
|
|
|
|
AGENT_INTENT_REBALANCE,
|
|
|
|
|
AGENT_INTENT_FUND_ANALYSIS,
|
|
|
|
|
AGENT_INTENT_DIALOGUE_SCRIPT,
|
|
|
|
|
AGENT_INTENT_DATA_QUERY,
|
|
|
|
|
AGENT_INTENT_CASUAL_CHAT,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class IntentClassification:
|
|
|
|
|
intent: str
|
|
|
|
|
confidence: float
|
|
|
|
|
source: str
|
|
|
|
|
reason: str = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_CLASSIFIER_PROMPT = """你是基金投顾工作台的意图分类器,只负责分类,不回答用户问题。
|
|
|
|
|
只能从以下分类中选择一个:
|
|
|
|
|
- recommend:基金推荐、组合配置或投资方案
|
2026-09-14 20:51:47 +08:00
|
|
|
例:“帮我推荐两只适合稳健型客户的基金”“给客户生成一份组合配置建议”“该买什么基金”
|
2026-09-13 23:46:15 +08:00
|
|
|
- rebalance:组合偏离、调仓、再平衡、仓位调整
|
2026-09-14 20:51:47 +08:00
|
|
|
例:“该客户组合偏离目标配置,请给出调仓方案”“组合需要再平衡,降低股票类仓位”
|
2026-09-13 23:46:15 +08:00
|
|
|
- fund_analysis:单只基金分析、净值、收益、回撤、波动率、夏普比率
|
2026-09-14 20:51:47 +08:00
|
|
|
例:“分析华夏回报近一年的净值走势和最大回撤”“这只基金的夏普比率和波动率如何”
|
2026-09-13 23:46:15 +08:00
|
|
|
- dialogue-script:给客户准备沟通话术、解释、安抚、投诉或风险提醒
|
2026-09-14 20:51:47 +08:00
|
|
|
例:“市场波动时怎么和客户解释”“帮我准备安抚客户的沟通话术”“客户投诉了,话术怎么准备”
|
|
|
|
|
- data_query:查询客户持仓、资产、余额、收益、交易、账户明细、客户名册
|
|
|
|
|
例:“查询客户48当前持仓和账户余额”“我名下有哪些客户”“统计名下客户数量”
|
2026-09-13 23:46:15 +08:00
|
|
|
- casual_chat:问候、闲聊、感谢、身份询问或无法归入业务分类的内容
|
2026-09-14 20:51:47 +08:00
|
|
|
例:“你好”“谢谢”“你是谁”
|
2026-09-13 23:46:15 +08:00
|
|
|
|
|
|
|
|
只输出 JSON,不要 Markdown,不要额外文字:
|
|
|
|
|
{"intent":"分类值","confidence":0到1之间的数字,"reason":"不超过30字的原因"}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_RECOMMENDATION_TERMS = ("推荐", "组合建议", "配置建议", "买什么", "适合配置", "筛选基金", "投资方案")
|
2026-09-14 13:00:15 +08:00
|
|
|
_SCOPE_ERROR_MESSAGE = "投顾范围查询仅支持客户数据查询"
|
2026-09-13 23:46:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _rule_fallback(query: str | None) -> IntentClassification:
|
|
|
|
|
intent = recognize_advisor_intent(query)
|
|
|
|
|
if intent:
|
|
|
|
|
return IntentClassification(intent=intent, confidence=0.72, source="rule", reason="关键词规则匹配")
|
|
|
|
|
return IntentClassification(intent=AGENT_INTENT_CASUAL_CHAT, confidence=0.0, source="fallback", reason="无法匹配业务意图")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_model_result(raw: str) -> IntentClassification | None:
|
|
|
|
|
text = raw.strip()
|
|
|
|
|
fenced = re.search(r"\{.*\}", text, re.DOTALL)
|
|
|
|
|
if fenced:
|
|
|
|
|
text = fenced.group(0)
|
|
|
|
|
try:
|
|
|
|
|
payload = json.loads(text)
|
|
|
|
|
except (TypeError, json.JSONDecodeError):
|
|
|
|
|
return None
|
|
|
|
|
intent = payload.get("intent")
|
|
|
|
|
if intent not in VALID_INTENTS:
|
|
|
|
|
return None
|
|
|
|
|
try:
|
|
|
|
|
confidence = max(0.0, min(1.0, float(payload.get("confidence", 0.0))))
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
confidence = 0.0
|
|
|
|
|
return IntentClassification(
|
|
|
|
|
intent=intent,
|
|
|
|
|
confidence=confidence,
|
|
|
|
|
source="llm",
|
|
|
|
|
reason=str(payload.get("reason") or "模型分类"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def classify_advisor_intent(
|
|
|
|
|
query: str | None,
|
|
|
|
|
llm_client=None,
|
|
|
|
|
*,
|
|
|
|
|
explicit_intent: str | None = None,
|
|
|
|
|
timeout: float = 2.0,
|
|
|
|
|
) -> IntentClassification:
|
|
|
|
|
"""分类用户意图;显式意图兼容旧客户端,模型失败时安全回退规则。"""
|
|
|
|
|
if explicit_intent in VALID_INTENTS:
|
|
|
|
|
return IntentClassification(explicit_intent, 1.0, "explicit", "客户端显式指定")
|
|
|
|
|
if not query or not query.strip():
|
|
|
|
|
return IntentClassification("", 0.0, "fallback", "空输入")
|
2026-09-14 13:00:15 +08:00
|
|
|
if re.sub(r"\s+", "", query) == _SCOPE_ERROR_MESSAGE:
|
|
|
|
|
return IntentClassification(
|
|
|
|
|
AGENT_INTENT_CASUAL_CHAT,
|
|
|
|
|
1.0,
|
|
|
|
|
"rule",
|
|
|
|
|
"识别为系统提示文本而非业务查询",
|
|
|
|
|
)
|
2026-09-14 21:41:40 +08:00
|
|
|
fast_intent = recognize_advisor_intent(query)
|
|
|
|
|
if (
|
|
|
|
|
fast_intent == AGENT_INTENT_DATA_QUERY
|
|
|
|
|
and not any(term in query for term in _RECOMMENDATION_TERMS)
|
|
|
|
|
):
|
|
|
|
|
return IntentClassification(
|
|
|
|
|
fast_intent,
|
|
|
|
|
0.95,
|
|
|
|
|
"rule_fast",
|
|
|
|
|
"明显查询类问题",
|
|
|
|
|
)
|
2026-09-13 23:46:15 +08:00
|
|
|
if llm_client is not None:
|
|
|
|
|
try:
|
|
|
|
|
raw = await asyncio.wait_for(
|
|
|
|
|
llm_client.chat(
|
|
|
|
|
[
|
|
|
|
|
{"role": "system", "content": _CLASSIFIER_PROMPT},
|
|
|
|
|
{"role": "user", "content": query.strip()},
|
|
|
|
|
],
|
|
|
|
|
temperature=0,
|
|
|
|
|
max_tokens=120,
|
|
|
|
|
),
|
|
|
|
|
timeout=timeout,
|
|
|
|
|
)
|
|
|
|
|
parsed = _parse_model_result(raw)
|
|
|
|
|
if parsed is not None:
|
|
|
|
|
# LLM 偶尔会把“查询持仓/资产”等只读请求误判为推荐;
|
|
|
|
|
# 对明确的查询动作以规则结果为准,避免误进入草稿生成分支。
|
|
|
|
|
rule_intent = recognize_advisor_intent(query)
|
|
|
|
|
if (
|
|
|
|
|
rule_intent == AGENT_INTENT_DATA_QUERY
|
2026-09-14 13:00:15 +08:00
|
|
|
and parsed.intent != AGENT_INTENT_DATA_QUERY
|
2026-09-13 23:46:15 +08:00
|
|
|
and not any(term in (query or "") for term in _RECOMMENDATION_TERMS)
|
|
|
|
|
):
|
|
|
|
|
return IntentClassification(
|
|
|
|
|
intent=rule_intent,
|
|
|
|
|
confidence=max(parsed.confidence, 0.9),
|
|
|
|
|
source="rule_override",
|
|
|
|
|
reason="明确查询类关键词覆盖模型误判",
|
|
|
|
|
)
|
|
|
|
|
return parsed
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
return _rule_fallback(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["IntentClassification", "classify_advisor_intent"]
|