Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/memos/graph_dbs/neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def _prepare_node_metadata(metadata: dict[str, Any]) -> dict[str, Any]:
if embedding and isinstance(embedding, list):
metadata["embedding"] = [float(x) for x in embedding]

# serialization
if metadata["sources"]:
for idx in range(len(metadata["sources"])):
metadata["sources"][idx] = json.dumps(metadata["sources"][idx])
return metadata


Expand Down
16 changes: 15 additions & 1 deletion src/memos/graph_dbs/neo4j_community.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Any

from memos.configs.graph_db import Neo4jGraphDBConfig
Expand Down Expand Up @@ -49,6 +50,10 @@ def add_node(self, id: str, memory: str, metadata: dict[str, Any]) -> None:
# Safely process metadata
metadata = _prepare_node_metadata(metadata)

# serialization
if metadata["sources"]:
for idx in range(len(metadata["sources"])):
metadata["sources"][idx] = json.dumps(metadata["sources"][idx])
# Extract required fields
embedding = metadata.pop("embedding", None)
if embedding is None:
Expand Down Expand Up @@ -298,7 +303,16 @@ def _parse_node(self, node_data: dict[str, Any]) -> dict[str, Any]:
if time_field in node and hasattr(node[time_field], "isoformat"):
node[time_field] = node[time_field].isoformat()
node.pop("user_name", None)

# serialization
if node["sources"]:
for idx in range(len(node["sources"])):
if not (
isinstance(node["sources"][idx], str)
and node["sources"][idx][0] == "{"
and node["sources"][idx][0] == "}"
):
break
node["sources"][idx] = json.loads(node["sources"][idx])
new_node = {"id": node.pop("id"), "memory": node.pop("memory", ""), "metadata": node}
try:
vec_item = self.vec_db.get_by_id(new_node["id"])
Expand Down
Loading