From 34b27d793e896f067184fb831fab48191dfd5579 Mon Sep 17 00:00:00 2001 From: jimi <1847930177@qq.com> Date: Sun, 1 Mar 2026 15:49:45 +0800 Subject: [PATCH] refactor: extract text risk detectors from pydantic agent --- core/pydantic_ai_agent.py | 34 ++----------------- core/risk_text_helpers.py | 71 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 core/risk_text_helpers.py diff --git a/core/pydantic_ai_agent.py b/core/pydantic_ai_agent.py index 66399dc..9592459 100755 --- a/core/pydantic_ai_agent.py +++ b/core/pydantic_ai_agent.py @@ -58,6 +58,7 @@ from core.agent_prompts import ( build_similar_prompt, build_system_prompt, ) +from core.risk_text_helpers import is_map_inquiry, is_political_inquiry load_dotenv() @@ -1376,37 +1377,8 @@ class CustomerServiceAgent: return "售后" return "售前" - @staticmethod - 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)) - - @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) + _is_political_inquiry = staticmethod(is_political_inquiry) + _is_map_inquiry = staticmethod(is_map_inquiry) def _get_customer_profile_context(self, customer_id: str) -> str: """从数据库读取客户画像,注入给 AI。含个性化语气、报价策略、主动预测、近期对话。""" diff --git a/core/risk_text_helpers.py b/core/risk_text_helpers.py new file mode 100644 index 0000000..47d1746 --- /dev/null +++ b/core/risk_text_helpers.py @@ -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)