-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from topoteretes/main-cognify-fix
fix: fixes cognify duplicated edges and resets the methods to an olde…
- Loading branch information
Showing
2 changed files
with
83 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 16 additions & 28 deletions
44
cognee/modules/graph/utils/get_model_instance_from_graph.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,29 @@ | ||
from typing import Callable | ||
|
||
from pydantic_core import PydanticUndefined | ||
|
||
from cognee.infrastructure.engine import DataPoint | ||
from cognee.modules.storage.utils import copy_model | ||
|
||
|
||
def get_model_instance_from_graph( | ||
nodes: list[DataPoint], | ||
edges: list[tuple[str, str, str, dict[str, str]]], | ||
entity_id: str, | ||
): | ||
node_map = {node.id: node for node in nodes} | ||
def get_model_instance_from_graph(nodes: list[DataPoint], edges: list, entity_id: str): | ||
node_map = {} | ||
|
||
for source_node_id, target_node_id, edge_label, edge_properties in edges: | ||
source_node = node_map[source_node_id] | ||
target_node = node_map[target_node_id] | ||
for node in nodes: | ||
node_map[node.id] = node | ||
|
||
for edge in edges: | ||
source_node = node_map[edge[0]] | ||
target_node = node_map[edge[1]] | ||
edge_label = edge[2] | ||
edge_properties = edge[3] if len(edge) == 4 else {} | ||
edge_metadata = edge_properties.get("metadata", {}) | ||
edge_type = edge_metadata.get("type", "default") | ||
edge_type = edge_metadata.get("type") | ||
|
||
if edge_type == "list": | ||
NewModel = copy_model( | ||
type(source_node), | ||
{edge_label: (list[type(target_node)], PydanticUndefined)}, | ||
) | ||
source_node_dict = source_node.model_dump() | ||
source_node_edge_label_values = source_node_dict.get(edge_label, []) | ||
source_node_dict[edge_label] = source_node_edge_label_values + [target_node] | ||
|
||
node_map[source_node_id] = NewModel(**source_node_dict) | ||
NewModel = copy_model(type(source_node), { edge_label: (list[type(target_node)], PydanticUndefined) }) | ||
|
||
node_map[edge[0]] = NewModel(**source_node.model_dump(), **{ edge_label: [target_node] }) | ||
else: | ||
NewModel = copy_model( | ||
type(source_node), {edge_label: (type(target_node), PydanticUndefined)} | ||
) | ||
NewModel = copy_model(type(source_node), { edge_label: (type(target_node), PydanticUndefined) }) | ||
|
||
node_map[target_node_id] = NewModel( | ||
**source_node.model_dump(), **{edge_label: target_node} | ||
) | ||
node_map[edge[0]] = NewModel(**source_node.model_dump(), **{ edge_label: target_node }) | ||
|
||
return node_map[entity_id] |