main
黄海 5 months ago
parent ecbb673175
commit 57608f23f2

@ -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__':

@ -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: # 确保方法名称正确

Loading…
Cancel
Save