Skip to content
Snippets Groups Projects
Commit f85c042a authored by Marcus Schiesser's avatar Marcus Schiesser Committed by Marcus Schiesser
Browse files

refactor: encapsulate node serialization

parent 40e892f8
No related branches found
No related tags found
No related merge requests found
import _ from "lodash";
import { createHash } from "node:crypto";
import path from "path";
import { v4 as uuidv4 } from "uuid";
......@@ -141,12 +142,26 @@ export abstract class BaseNode<T extends Metadata = Metadata> {
}
/**
* Used with built in JSON.stringify
* @returns
* Called by built in JSON.stringify (see https://javascript.info/json)
* Properties are read-only as they are not deep-cloned (not necessary for stringification).
* @see toMutableJSON - use to return a mutable JSON instead
*/
toJSON(): Record<string, any> {
return { ...this, type: this.getType() };
}
clone(): BaseNode {
return jsonToNode(this.toMutableJSON()) as BaseNode;
}
/**
* Converts the object to a JSON representation.
* Properties can be safely modified as a deep clone of the properties are created.
* @return {Record<string, any>} - The JSON representation of the object.
*/
toMutableJSON(): Record<string, any> {
return _.cloneDeep(this.toJSON());
}
}
/**
......
......@@ -4,7 +4,6 @@ import {
ImageNode,
MetadataMode,
ObjectType,
jsonToNode,
splitNodesByType,
} from "../../Node";
import { BaseQueryEngine, RetrieverQueryEngine } from "../../QueryEngine";
......@@ -278,7 +277,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
type === ObjectType.INDEX ||
type === ObjectType.IMAGE
) {
const nodeWithoutEmbedding = jsonToNode(nodes[i].toJSON());
const nodeWithoutEmbedding = nodes[i].clone();
nodeWithoutEmbedding.embedding = undefined;
this.indexStruct.addNode(nodeWithoutEmbedding, newIds[i]);
this.docStore.addDocuments([nodeWithoutEmbedding], true);
......
import _ from "lodash";
import { BaseNode, jsonToNode, Metadata, ObjectType } from "../../Node";
const DEFAULT_TEXT_KEY = "text";
......@@ -17,7 +16,7 @@ export function nodeToMetadata(
textField: string = DEFAULT_TEXT_KEY,
flatMetadata: boolean = false,
): Metadata {
const { metadata, embedding, ...rest } = _.cloneDeep(node.toJSON());
const { metadata, embedding, ...rest } = node.toMutableJSON();
if (flatMetadata) {
validateIsFlat(metadata);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment