refactor: extract text risk detectors from pydantic agent

This commit is contained in:
2026-03-01 15:49:45 +08:00
parent 62e5fed25c
commit 34b27d793e
2 changed files with 74 additions and 31 deletions

View File

@@ -58,6 +58,7 @@ from core.agent_prompts import (
build_similar_prompt, build_similar_prompt,
build_system_prompt, build_system_prompt,
) )
from core.risk_text_helpers import is_map_inquiry, is_political_inquiry
load_dotenv() load_dotenv()
@@ -1376,37 +1377,8 @@ class CustomerServiceAgent:
return "售后" return "售后"
return "售前" return "售前"
@staticmethod _is_political_inquiry = staticmethod(is_political_inquiry)
def _is_political_inquiry(text: str) -> bool: _is_map_inquiry = staticmethod(is_map_inquiry)
"""文本前置风控:政治人物/政治事件/政治图片相关询问一律拒绝。"""
s = (text or "").strip().lower()
if not s:
return False
kw = (
"政治", "涉政", "党政", "政治人物", "政治事件", "政治图片", "政治海报", "政治宣传",
"领导人", "伟人", "元帅", "将军", "红色人物", "党史",
"天安门", "人民大会堂", "中南海",
"习近平", "毛泽东", "邓小平", "江泽民", "胡锦涛", "李克强", "周恩来",
"特朗普", "拜登", "普京", "泽连斯基",
"trump", "biden", "putin", "zelensky", "xi jinping",
)
if any(k in s for k in kw):
return True
# 兜底:类似“有没有十大元帅的照片/图片”
return bool(re.search(r"(元帅|将军|领导人|政治人物|政治事件).*(照片|图片|头像|原图)?", s))
@staticmethod
def _is_map_inquiry(text: str) -> bool:
"""地图类需求一律拒绝(按业务规则)。"""
s = (text or "").strip().lower()
if not s:
return False
kw = (
"地图", "地形图", "行政区划图", "世界地图", "中国地图",
"卫星地图", "导航图", "航海图", "作战地图", "军事地图",
"map", "topographic map", "satellite map",
)
return any(k in s for k in kw)
def _get_customer_profile_context(self, customer_id: str) -> str: def _get_customer_profile_context(self, customer_id: str) -> str:
"""从数据库读取客户画像,注入给 AI。含个性化语气、报价策略、主动预测、近期对话。""" """从数据库读取客户画像,注入给 AI。含个性化语气、报价策略、主动预测、近期对话。"""

71
core/risk_text_helpers.py Normal file
View File

@@ -0,0 +1,71 @@
from __future__ import annotations
import re
def is_political_inquiry(text: str) -> bool:
"""文本前置风控:政治人物/政治事件/政治图片相关询问一律拒绝。"""
s = (text or "").strip().lower()
if not s:
return False
kw = (
"政治",
"涉政",
"党政",
"政治人物",
"政治事件",
"政治图片",
"政治海报",
"政治宣传",
"领导人",
"伟人",
"元帅",
"将军",
"红色人物",
"党史",
"天安门",
"人民大会堂",
"中南海",
"习近平",
"毛泽东",
"邓小平",
"江泽民",
"胡锦涛",
"李克强",
"周恩来",
"特朗普",
"拜登",
"普京",
"泽连斯基",
"trump",
"biden",
"putin",
"zelensky",
"xi jinping",
)
if any(k in s for k in kw):
return True
return bool(re.search(r"(元帅|将军|领导人|政治人物|政治事件).*(照片|图片|头像|原图)?", s))
def is_map_inquiry(text: str) -> bool:
"""地图类需求一律拒绝(按业务规则)。"""
s = (text or "").strip().lower()
if not s:
return False
kw = (
"地图",
"地形图",
"行政区划图",
"世界地图",
"中国地图",
"卫星地图",
"导航图",
"航海图",
"作战地图",
"军事地图",
"map",
"topographic map",
"satellite map",
)
return any(k in s for k in kw)