49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
"""Customer-service intent recognition contract."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from enum import StrEnum
|
|
|
|
|
|
logger = logging.getLogger("rag.intent")
|
|
|
|
|
|
class Intent(StrEnum):
|
|
GUIDE_PURCHASE = "guide_purchase"
|
|
WANT_ADVISOR = "want_advisor"
|
|
NL2SQL_REQUEST = "nl2sql_request"
|
|
COMPLAIN = "complain"
|
|
KNOWLEDGE_QA = "knowledge_qa"
|
|
NO_MATCH = "no_match"
|
|
|
|
|
|
INTENT_VALUES = frozenset(item.value for item in Intent)
|
|
|
|
|
|
async def intent_recognize(query: str, *, llm_client) -> Intent:
|
|
if not query or not query.strip():
|
|
return Intent.NO_MATCH
|
|
messages = [
|
|
{
|
|
"role": "system",
|
|
"content": (
|
|
"你是一个意图分类器。请根据用户输入,从以下选项中选择最匹配的意图,"
|
|
"并仅输出对应的英文标签(不输出任何其他内容):\n"
|
|
"- guide_purchase: 用户询问如何购买基金、开户、注册等引导类问题\n"
|
|
"- want_advisor: 用户希望获得个性化基金推荐或投资顾问服务\n"
|
|
"- knowledge_qa: 用户询问基金相关的知识性问题,如净值、费率、风险等\n"
|
|
"- nl2sql_request: 用户要求查询具体数据或账户信息\n"
|
|
"- complain: 用户表达不满或投诉\n"
|
|
"- no_match: 以上都不匹配\n"
|
|
"仅输出一个小写英文标签,不要输出解释、标点或换行。"
|
|
),
|
|
},
|
|
{"role": "user", "content": query},
|
|
]
|
|
try:
|
|
raw = await llm_client.chat(messages)
|
|
except Exception:
|
|
logger.exception("intent recognition failed")
|
|
return Intent.NO_MATCH
|
|
value = raw.strip().strip('`').lower()
|
|
return Intent(value) if value in INTENT_VALUES else Intent.NO_MATCH |