feat: ai-first intent detection with keyword fallback
This commit is contained in:
@@ -396,7 +396,14 @@ class QingjianAPIClient:
|
||||
elif self._is_shop_card(data):
|
||||
# 进店卡片:有历史对话就不回复,没有才打招呼(Gemini 已在上面统一跳过)
|
||||
cid = data.get('from_id', '')
|
||||
if self._has_chat_history(cid):
|
||||
acc_id = data.get('acc_id', '')
|
||||
residual_text = self._extract_customer_text_from_shop_card_msg(data.get('msg', ''))
|
||||
if residual_text:
|
||||
logger.info(f"[{self.get_time()}] 进店卡片携带客户文本,转普通消息处理: {residual_text}")
|
||||
patched = dict(data)
|
||||
patched['msg'] = residual_text
|
||||
await self._debounce_agent_reply(patched)
|
||||
elif self._has_chat_history(cid, acc_id=acc_id):
|
||||
logger.info(f"[{self.get_time()}] 进店卡片(已有记录),跳过")
|
||||
else:
|
||||
logger.info(f"[{self.get_time()}] 进店卡片(新客户),发送问候")
|
||||
@@ -505,14 +512,23 @@ class QingjianAPIClient:
|
||||
if self._msg_has_image_url(text):
|
||||
return "image"
|
||||
try:
|
||||
from utils.intent_analyzer import detect_intent_keywords
|
||||
intent = detect_intent_keywords(text)
|
||||
from utils.intent_analyzer import detect_intent
|
||||
decision = detect_intent(text)
|
||||
intent = decision.intent
|
||||
if intent:
|
||||
self._activity_log(
|
||||
"debounce_intent_detected",
|
||||
intent=intent,
|
||||
source=decision.source,
|
||||
score=round(float(decision.score or 0.0), 4),
|
||||
msg=text[:120],
|
||||
)
|
||||
except Exception:
|
||||
intent = ""
|
||||
if intent:
|
||||
return intent
|
||||
lower = text.lower()
|
||||
if any(k in lower for k in ["报价", "多少钱", "价格", "贵", "优惠"]):
|
||||
if any(k in lower for k in ["报价", "多少钱", "价格", "贵", "优惠", "收费", "怎么收费", "咋收费"]):
|
||||
return "询价"
|
||||
if any(k in lower for k in ["做一下", "改一下", "需求", "门头", "上面的字", "处理"]):
|
||||
return "修改"
|
||||
@@ -1222,17 +1238,31 @@ class QingjianAPIClient:
|
||||
msg = self.to_chinese(data.get('msg', ''))
|
||||
return msg.startswith('[进店卡片]') or '我想咨询你们店的这个商品' in msg
|
||||
|
||||
def _has_chat_history(self, customer_id: str) -> bool:
|
||||
"""判断该客户是否已有聊天记录(内存历史或数据库均可)"""
|
||||
def _extract_customer_text_from_shop_card_msg(self, msg: str) -> str:
|
||||
"""从“进店卡片+文本”混合消息里提取客户真实文本。"""
|
||||
text = self.to_chinese(msg or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
parts = [p.strip() for p in text.split("#*#") if p and p.strip()]
|
||||
kept = []
|
||||
for part in parts:
|
||||
if part.startswith("[进店卡片]") or "我想咨询你们店的这个商品" in part:
|
||||
continue
|
||||
kept.append(part)
|
||||
if kept:
|
||||
return " ".join(kept).strip()
|
||||
stripped = re.sub(r"\[进店卡片\][^\n\r]*", "", text).strip()
|
||||
stripped = stripped.replace("我想咨询你们店的这个商品", "").strip(",。,#* ")
|
||||
return stripped
|
||||
|
||||
def _has_chat_history(self, customer_id: str, acc_id: str = "") -> bool:
|
||||
"""判断该客户在当前店铺是否已有聊天记录。"""
|
||||
if not customer_id:
|
||||
return False
|
||||
# 先查内存对话历史(最快)
|
||||
if customer_id in self.agent.message_histories and self.agent.message_histories[customer_id]:
|
||||
return True
|
||||
# 再查数据库(重启后仍有记录)
|
||||
# 按店铺+客户查数据库,避免跨店串历史导致错误跳过。
|
||||
try:
|
||||
from db.chat_log_db import get_conversation
|
||||
msgs = get_conversation(customer_id, limit=1)
|
||||
from db.chat_log_db import get_recent_conversation
|
||||
msgs = get_recent_conversation(customer_id, acc_id=acc_id, limit=1)
|
||||
return len(msgs) > 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user