From b31e2d42eb04be16662f94dd7f55da979ff22c9c Mon Sep 17 00:00:00 2001 From: yisding <yi.s.ding@gmail.com> Date: Tue, 2 Jan 2024 18:10:23 -0800 Subject: [PATCH] fixed recursive issue in metadataDictToNode --- .../core/src/storage/vectorStore/utils.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/core/src/storage/vectorStore/utils.ts b/packages/core/src/storage/vectorStore/utils.ts index 3ad168588..f7905fcae 100644 --- a/packages/core/src/storage/vectorStore/utils.ts +++ b/packages/core/src/storage/vectorStore/utils.ts @@ -17,7 +17,7 @@ export function nodeToMetadata( flatMetadata: boolean = false, ): Metadata { const nodeObj = node.toJSON(); - const metadata = node.metadata; + const { metadata, embedding, ...rest } = nodeObj; if (flatMetadata) { validateIsFlat(node.metadata); @@ -27,9 +27,7 @@ export function nodeToMetadata( nodeObj[textField] = ""; } - nodeObj["embedding"] = null; - - metadata["_node_content"] = JSON.stringify(nodeObj); + metadata["_node_content"] = JSON.stringify(rest); metadata["_node_type"] = node.constructor.name.replace("_", ""); // remove leading underscore to be compatible with Python metadata["document_id"] = node.sourceNode?.nodeId || "None"; @@ -40,17 +38,24 @@ export function nodeToMetadata( } export function metadataDictToNode(metadata: Metadata): BaseNode { - const nodeContent = metadata["_node_content"]; + const { + _node_content: nodeContent, + _node_type: nodeType, + document_id, + doc_id, + ref_doc_id, + ...rest + } = metadata; if (!nodeContent) { throw new Error("Node content not found in metadata."); } const nodeObj = JSON.parse(nodeContent); + nodeObj.metadata = rest; // Note: we're using the name of the class stored in `_node_type` // and not the type attribute to reconstruct // the node. This way we're compatible with LlamaIndex Python - const node_type = metadata["_node_type"]; - switch (node_type) { + switch (nodeType) { case "IndexNode": return jsonToNode(nodeObj, ObjectType.INDEX); default: -- GitLab