40 lines
1.0 KiB
Python
40 lines
1.0 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": "仅返回一个意图枚举值,不要输出解释。",
|
|
},
|
|
{"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
|