fix(chat-fixed-assets): 修复固资连续问答与宽泛查询空字段问题
This commit is contained in:
1 parent
599475d4e2
commit
1164ae2e3c
7 files changed
+417
-46
No files matched your search
@@ -151,6 +151,51 @@ class Text2SQLService:
|
||||
"当前登录用户",
|
||||
"登录用户",
|
||||
)
|
||||
_ASSET_CONTEXT_DEPENDENT_MARKERS = (
|
||||
"这个",
|
||||
"这个资产",
|
||||
"该资产",
|
||||
"这台",
|
||||
"这个设备",
|
||||
"这个人",
|
||||
"这个部门",
|
||||
"这些",
|
||||
"上述",
|
||||
"上面",
|
||||
"前面",
|
||||
"刚才",
|
||||
"上一条",
|
||||
"上一个",
|
||||
"同一个",
|
||||
"它",
|
||||
"它的",
|
||||
"其",
|
||||
"他的",
|
||||
"她的",
|
||||
)
|
||||
_ASSET_RESET_SAFE_INTENT_MARKERS = (
|
||||
"明细",
|
||||
"列表",
|
||||
"统计",
|
||||
"查询",
|
||||
"查下",
|
||||
"看下",
|
||||
"看看",
|
||||
"有多少",
|
||||
"多少",
|
||||
"多少台",
|
||||
"状态",
|
||||
"规格",
|
||||
"型号",
|
||||
"账套",
|
||||
"保管人",
|
||||
"保管部门",
|
||||
"存放位置",
|
||||
"位置",
|
||||
"供应商",
|
||||
"取得日期",
|
||||
"入账日期",
|
||||
)
|
||||
_ASSET_CODE_HINT_PATTERN = re.compile(
|
||||
r"(?:资产编号|财产编号|资产编码|设备编号|编号|设备|资产|固资|固定资产)\s*(?:为|是|=|:|:)?\s*([A-Za-z][A-Za-z0-9_-]{4,30})",
|
||||
re.IGNORECASE,
|
||||
@@ -547,6 +592,68 @@ class Text2SQLService:
|
||||
or any(marker.lower() in sql_text.lower() for marker in cls._ASSET_TABLE_MARKERS)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _is_context_dependent_asset_query(cls, query: str) -> bool:
|
||||
"""判断固资问句是否依赖上一轮上下文。
|
||||
|
||||
AI 触发时机:当用户说“这个资产”“上面那条”“它的状态”等省略表达时,
|
||||
不能贸然重置 provider conversation,否则会把真正依赖上下文的追问切断。
|
||||
"""
|
||||
text = cls._strip_asset_query_prefix(query)
|
||||
if not text:
|
||||
return False
|
||||
if cls._extract_asset_code(text):
|
||||
return False
|
||||
if cls._has_explicit_asset_scope(text) or cls._is_self_asset_query(text):
|
||||
return False
|
||||
if cls._extract_advanced_asset_conditions(text):
|
||||
return False
|
||||
if cls._extract_explicit_asset_name(text):
|
||||
return False
|
||||
if cls._extract_asset_name_combo_keyword(text):
|
||||
return False
|
||||
if cls._extract_asset_name_keyword(text):
|
||||
return False
|
||||
return any(marker in text for marker in cls._ASSET_CONTEXT_DEPENDENT_MARKERS)
|
||||
|
||||
@classmethod
|
||||
def _should_reset_provider_conversation(cls, query: str, agent=None) -> bool:
|
||||
"""判断固资查询是否应主动切断上游会话上下文。
|
||||
|
||||
AI 触发时机:固资管理中的多数查询本质上都是“本轮文本可独立完成”的单轮检索。
|
||||
当上一轮返回超大结果时,继续复用 Dify 会话容易把 token 撑爆;此时应保留本地历史会话,
|
||||
但把上游 provider conversation 重置为空,让当前这轮按独立查询执行。
|
||||
|
||||
Args:
|
||||
query: 用户当前输入的问题文本。
|
||||
agent: 当前会话 Agent,仅固资管理 Agent 会进入该策略。
|
||||
|
||||
Returns:
|
||||
bool: True 表示当前问题可脱离旧上下文独立执行,应切断上游会话;
|
||||
False 表示当前问题明显依赖上一轮上下文,或根本不是固资管理问题。
|
||||
"""
|
||||
text = cls._strip_asset_query_prefix(query)
|
||||
if not text or not cls._is_asset_management_agent(agent):
|
||||
return False
|
||||
if not cls._looks_like_asset_query(text):
|
||||
return False
|
||||
if cls._is_context_dependent_asset_query(text):
|
||||
return False
|
||||
|
||||
if cls._extract_asset_code(text):
|
||||
return True
|
||||
if cls._has_explicit_asset_scope(text) or cls._is_self_asset_query(text):
|
||||
return True
|
||||
if cls._extract_advanced_asset_conditions(text):
|
||||
return True
|
||||
if cls._extract_explicit_asset_name(text):
|
||||
return True
|
||||
if cls._extract_asset_name_combo_keyword(text):
|
||||
return True
|
||||
if cls._extract_asset_name_keyword(text):
|
||||
return True
|
||||
return any(marker in text for marker in cls._ASSET_RESET_SAFE_INTENT_MARKERS)
|
||||
|
||||
@staticmethod
|
||||
def _normalize_user_context(user) -> dict:
|
||||
if user is None:
|
||||
|
||||
Reference in new issue
Block a user