youtu-graphrag
youtu-graphrag copied to clipboard
Graph中存在同一实体重复多次(entity_node_id不一样)的问题
Hi,
在看代码的时候,发现有点问题:
def process_level1_level2(self, chunk: str, id: int):
# Process attributes and triples
attr_nodes, attr_edges = self._process_attributes(extracted_attr, id, entity_types)
triple_nodes, triple_edges = self._process_triples(extracted_triples, id, entity_types)
上面self._process_attributes和self._process_triples都会调用self._find_or_create_entity函数,
def _find_or_create_entity(self, entity_name: str, chunk_id: int, nodes_to_add: list, entity_type: str = None) -> str:
"""Find existing entity or create a new one, returning the entity node ID."""
with self.lock:
entity_node_id = next(
(
n
for n, d in self.graph.nodes(data=True)
if d.get("label") == "entity" and d["properties"]["name"] == entity_name
),
None,
)
问题是:
-
对于self._process_attributes(extracted_attr, id, entity_types),如果1个entity有多个属性,在两层for循环的时候,内循环会调用
entity_node_id = self._find_or_create_entity(entity, chunk_id, nodes_to_add, entity_type)对于同一个entity,如果刚好这个entity没有在graph添加过,就会导致出现多个entity_node_id不一样(self.node_counter在自增)的同一entity,这些重复实体会被加入到nodes_to_add,后面就会被重复加入到graph中(但是entity_node_id不一样)。 -
对于self._process_triples函数里的节点处理:
subj_node_id = self._find_or_create_entity(subj, chunk_id, nodes_to_add, subj_type)
obj_node_id = self._find_or_create_entity(obj, chunk_id, nodes_to_add, obj_type)
如果subj和obj和self._process_attributes中的entity重复(大概率), 并且这些entity没有在graph中添加过,就会导致attr_nodes和triple_nodes存在entity_node_id不一样(self.node_counter在自增)的同一entity
- 代码中有triple的去重,但是好像没有实体节点的去重,不确定有没有影响