fix: 实体默认类型Concept而非NULL,星图回退时间戳,entity_types改为dict格式

This commit is contained in:
root
2026-04-30 09:08:20 +08:00
parent aa9b25c829
commit da02a729a3
4 changed files with 18 additions and 61 deletions

View File

@ -393,8 +393,9 @@ class EmbeddedGraphDB:
continue
# 创建或更新实体
for entity_name in [subject, obj]:
entity_type = entity_types.get(entity_name) if entity_types else None
for entity_name, entity_key in [(subject, 'subject_type'), (obj, 'object_type')]:
# 按优先级获取实体类型1) triplet中的_type字段 2) entity_types字典 3) 默认
entity_type = triplet.get(entity_key) or (entity_types.get(entity_name) if entity_types else None) or 'Concept'
cursor.execute("""
INSERT INTO entities (name, type)

View File

@ -121,7 +121,7 @@ class Neo4jGraph:
return {"entities": list(entities.values()), "relations": relations[:20]}
def commit(self, triplets: list, entity_types: list = None, temporal_tag: str = None) -> dict:
def commit(self, triplets: list, entity_types: dict = None, temporal_tag: str = None) -> dict:
"""写入记忆"""
global CURRENT_TURN
with self.driver.session() as session:
@ -130,7 +130,6 @@ class Neo4jGraph:
if not valid_triplets:
return {"committed_count": 0, "details": []}
etype = entity_types[0] if entity_types else "unknown"
date_bucket = temporal_tag or datetime.now().strftime("%Y-%m-%d")
results = []
@ -140,13 +139,17 @@ class Neo4jGraph:
obj = triplet.get("object", "").strip()
confidence = triplet.get("confidence", 0.9)
# 按优先级获取实体类型1) triplet中的_type字段 2) entity_types字典 3) 默认
s_type = triplet.get("subject_type") or (entity_types.get(subject) if entity_types else None) or "Concept"
o_type = triplet.get("object_type") or (entity_types.get(obj) if entity_types else None) or "Concept"
session.run("""
MERGE (s:Entity {name: $subject})
ON CREATE SET s.type = $type, s.created_at = datetime(), s.mention_count = 1, s.updated_at = datetime()
ON CREATE SET s.type = $s_type, s.created_at = datetime(), s.mention_count = 1, s.updated_at = datetime()
ON MATCH SET s.mention_count = coalesce(s.mention_count, 0) + 1, s.updated_at = datetime()
MERGE (t:Entity {name: $object})
ON CREATE SET t.type = $type, t.created_at = datetime(), t.mention_count = 1, t.updated_at = datetime()
ON CREATE SET t.type = $o_type, t.created_at = datetime(), t.mention_count = 1, t.updated_at = datetime()
ON MATCH SET t.mention_count = coalesce(t.mention_count, 0) + 1, t.updated_at = datetime()
CREATE (s)-[r:RELATES {
@ -159,7 +162,8 @@ class Neo4jGraph:
confidence: $confidence,
date_bucket: $date_bucket
}]->(t)
""", subject=subject, object=obj, relation=relation, type=etype,
""", subject=subject, object=obj, relation=relation,
s_type=s_type, o_type=o_type,
session_id=CURRENT_SESSION_ID, turn_id=CURRENT_TURN, confidence=confidence,
date_bucket=date_bucket)

View File

@ -103,16 +103,18 @@ MEMORY_TOOLS = [
"subject": {"type": "string"},
"relation": {"type": "string"},
"object": {"type": "string"},
"confidence": {"type": "number"}
"confidence": {"type": "number"},
"subject_type": {"type": "string", "description": "主体的实体类型,如 Person、Project"},
"object_type": {"type": "string", "description": "客体的实体类型,如 Language、Technology"}
},
"required": ["subject", "relation", "object"]
},
"description": "三元组列表"
},
"entity_types": {
"type": "array",
"items": {"type": "string"},
"description": "实体类型(可选)"
"type": "object",
"additionalProperties": {"type": "string"},
"description": "实体类型字典,如 {\"用户\": \"Person\", \"项目A\": \"Project\"}(可选)"
},
"temporal_tag": {
"type": "string",