From 57608f23f2f196b5bd9a2d148f7e69b4894d6b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Tue, 18 Feb 2025 11:24:57 +0800 Subject: [PATCH] 'commit' --- AI/Neo4j/K1_KnowledgeGraph.py | 53 ++++++++++-------- AI/Neo4j/K2_Neo4jExecutor.py | 2 +- .../K2_Neo4jExecutor.cpython-310.pyc | Bin 2865 -> 2878 bytes 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/AI/Neo4j/K1_KnowledgeGraph.py b/AI/Neo4j/K1_KnowledgeGraph.py index 725f7d5c..d0a2f16a 100644 --- a/AI/Neo4j/K1_KnowledgeGraph.py +++ b/AI/Neo4j/K1_KnowledgeGraph.py @@ -45,28 +45,21 @@ class KnowledgeGraph: def _generate_stream(self) -> Iterator[ChatCompletionChunk]: """生成限制性提示词""" - system_prompt = f'''# 严格生成规则 -1. 仅允许使用以下预注册节点: - - 知识点列表(共{len(self.existing_knowledge)}个): -{self._format_node_list(self.existing_knowledge)} - - 能力点列表(共{len(self.existing_ability)}个): -{self._format_node_list(self.existing_ability)} - -2. 必须遵守的Cypher模式: -MERGE (q:Question {{id: "{self.question_id}"}}) -SET q.content = "题目内容摘要" - -WITH q -MATCH (kp:KnowledgePoint {{id: "KP_xxxxxx"}}) -MERGE (q)-[:TESTS_KNOWLEDGE {{weight: 0.8}}]->(kp) - -MATCH (ab:AbilityPoint {{id: "AB_xxxxxx"}}) -MERGE (q)-[:REQUIRES_ABILITY {{weight: 0.7}}]->(ab) - -3. 绝对禁止: -- 使用CREATE创建新节点 -- 修改已有节点属性 -- 使用未注册的ID''' + # 修改提示词中的Cypher示例部分 + system_prompt = f''' + // 修改后的正确示例 + MERGE (q:Question {{id: "{self.question_id}"}}) + SET q.content = "题目内容摘要" + + // 必须添加WITH子句 + WITH q + MATCH (kp:KnowledgePoint {{id: "KP_xxxxxx"}}) + MERGE (q)-[:TESTS_KNOWLEDGE {{weight: 0.8}}]->(kp) + + WITH q // 多个关系需要继续传递 + MATCH (ab:AbilityPoint {{id: "AB_xxxxxx"}}) + MERGE (q)-[:REQUIRES_ABILITY {{weight: 0.7}}]->(ab) + ''' return self.client.chat.completions.create( model=MODEL_NAME, @@ -145,6 +138,12 @@ MERGE (q)-[:REQUIRES_ABILITY {{weight: 0.7}}]->(ab) return False return True # 没有weight属性时视为合法 + def _validate_cypher_structure(self, cypher: str) -> bool: + """验证WITH子句存在性""" + has_merge = re.search(r'\bMERGE\s*\(q:Question\b', cypher, re.IGNORECASE) + has_with = re.search(r'\bWITH\s+q\b', cypher, re.IGNORECASE) + return not has_merge or (has_merge and has_with) + def run(self) -> Tuple[bool, str, str]: """执行安全生成流程""" if not self.existing_knowledge or not self.existing_ability: @@ -183,9 +182,15 @@ MERGE (q)-[:REQUIRES_ABILITY {{weight: 0.7}}]->(ab) print("\n⚠️ 生成完成但未获取到有效内容") return False, "空内容", "" + + # 修改run方法中的异常处理 except Exception as e: - print(f"\n\n❌ 生成失败:{str(e)}") - return False, str(e), "" + # 原错误代码 + # print(f"\n\n❌ 生成失败:{str(e)}") + # 修正后的代码 + error_msg = str(e) if not isinstance(e, dict) else json.dumps(e) + print(f"\n\n❌ 生成失败:{error_msg}") + return False, error_msg, "" if __name__ == '__main__': diff --git a/AI/Neo4j/K2_Neo4jExecutor.py b/AI/Neo4j/K2_Neo4jExecutor.py index a0a1f913..035757f6 100644 --- a/AI/Neo4j/K2_Neo4jExecutor.py +++ b/AI/Neo4j/K2_Neo4jExecutor.py @@ -28,7 +28,7 @@ class K2_Neo4jExecutor: return stats except Exception as e: - print(f"文本解析错误: {str(e)}") + print(f"执行失败: {stmt[:100]}... \n完整错误: {str(e)}") # 原为str(e)[:100] return stats def execute_cypher_file(self, file_path: str) -> dict: # 确保方法名称正确 diff --git a/AI/Neo4j/__pycache__/K2_Neo4jExecutor.cpython-310.pyc b/AI/Neo4j/__pycache__/K2_Neo4jExecutor.cpython-310.pyc index fa852b524b7d833862e326ab710f32d7c5089840..8222e275ec28eea9203da5e5413c12547e8c7d85 100644 GIT binary patch delta 98 zcmdlewoi;VpO=@50SJozZcd-Jk#{8%69?nu9Zbhnn;98uSs59sBudy)xN2BZxKsFA znIsuzGo B8+8Bx delta 86 zcmdldwo!~XpO=@50SL7JY)((x$h(q>=@-M~9Zbi?YZw`7Ss59s1WVXb_)FMpSeqGV qGt6b$yo5Q5Q9uN!