Skip to content
Snippets Groups Projects
Commit 66d59c86 authored by Sourabh Desai's avatar Sourabh Desai
Browse files

update node types and doc serialization/deserialization

parent 3632c90e
No related branches found
No related tags found
No related merge requests found
import { v4 as uuidv4 } from "uuid";
export enum NodeType {
DOCUMENT,
TEXT,
IMAGE,
INDEX,
}
export abstract class BaseDocument {
text: string;
docId?: string;
......@@ -45,14 +53,20 @@ export abstract class BaseDocument {
getDocHash() {
return this.docHash;
}
abstract getType(): NodeType;
}
export class Document extends BaseDocument {
static getType() {
return "Document";
getType() {
return NodeType.DOCUMENT;
}
}
export class ImageDocument extends Document {
image?: string;
getType() {
return NodeType.IMAGE;
}
}
\ No newline at end of file
import { BaseDocument } from "./Document";
import { BaseDocument, NodeType } from "./Document";
export enum DocumentRelationship {
SOURCE = "source",
......@@ -8,12 +8,6 @@ export enum DocumentRelationship {
CHILD = "child",
}
export enum NodeType {
TEXT,
IMAGE,
INDEX,
}
export class Node extends BaseDocument {
relationships: { [key in DocumentRelationship]: string | string[] | null };
......@@ -61,6 +55,10 @@ export class Node extends BaseDocument {
childNodeIds(): string[] {
return [];
}
getType() {
return NodeType.TEXT;
}
}
export interface NodeWithEmbedding {
......
......@@ -5,6 +5,4 @@ export const DEFAULT_DOC_STORE_PERSIST_FILENAME = "docstore.json";
export const DEFAULT_VECTOR_STORE_PERSIST_FILENAME = "vector_store.json";
export const DEFAULT_GRAPH_STORE_PERSIST_FILENAME = "graph_store.json";
export const DEFAULT_NAMESPACE = "docstore";
export const TYPE_KEY = "__type__";
export const DATA_KEY = "__data__";
export { DEFAULT_FS } from "./FileSystem";
\ No newline at end of file
export { DEFAULT_FS } from "./FileSystem";
import { Node } from "../../Node";
import { BaseDocument, NodeType, Document } from '../../Document';
import { DATA_KEY, TYPE_KEY } from '../constants';
import { BaseDocument, Document, NodeType } from '../../Document';
const TYPE_KEY = "__type__";
const DATA_KEY = "__data__";
export function docToJson(doc: BaseDocument): Record<string, any> {
......@@ -16,9 +18,11 @@ export function jsonToDoc(docDict: Record<string, any>): BaseDocument {
let doc: BaseDocument;
if (docType === NodeType.DOCUMENT) {
doc = new Document(dataDict.docId, dataDict.text);
doc = new Document(dataDict.text, dataDict.docId, dataDict.embedding, dataDict.docHash);
} else if (docType === NodeType.TEXT) {
doc = new Node(dataDict.relationships);
const reslationships = dataDict.relationships;
doc = new Node(reslationships.text, reslationships.docId,
reslationships.embedding, reslationships.docHash);
} else {
throw new Error(`Unknown doc type: ${docType}`);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment