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

add initial types related to storage contexts

parent a56c4247
Branches
Tags
No related merge requests found
interface StorageContext {
docStore?: any;
indexStore?: any;
vectorStore?: any;
graphStore?: any;
}
/**
* A filesystem interface that is meant to be compatible with
* the 'fs' module from Node.js.
* Allows for the use of similar inteface implementation on
* browsers.
*/
export interface GenericFileSystem {
writeFile(path: string, options: any): Promise<void>;
readFile(path: string, options: any): Promise<string>;
}
import { BaseDocumentStore } from "./docStore/types";
import { BaseIndexStore } from "./indexStore/types";
import { VectorStore } from "./vectorStore/types";
export interface StorageContext {
docStore?: BaseDocumentStore;
indexStore?: BaseIndexStore;
vectorStore?: VectorStore;
graphStore?: any;
}
export const DEFAULT_PERSIST_DIR = "./storage";
export const DEFAULT_INDEX_STORE_PERSIST_FILENAME = "index_store.json";
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";
import { Node } from "../../Node";
import { BaseDocument } from "../../Document";
import { GenericFileSystem } from "../FileSystem";
import { DEFAULT_PERSIST_DIR, DEFAULT_DOC_STORE_PERSIST_FILENAME } from "../constants";
const defaultPersistPath = `${DEFAULT_PERSIST_DIR}/${DEFAULT_DOC_STORE_PERSIST_FILENAME}`;
export interface RefDocInfo {
docIds: string[];
extraInfo: {[key: string]: any};
}
export abstract class BaseDocumentStore {
// Save/load
persist(persistPath: string = defaultPersistPath, fs?: GenericFileSystem): void {
// Persist the docstore to a file.
}
// Main interface
abstract get docs(): {[key: string]: BaseDocument};
abstract addDocuments(docs: BaseDocument[], allowUpdate: boolean): void;
abstract getDocument(docId: string, raiseError: boolean): BaseDocument | null;
abstract deleteDocument(docId: string, raiseError: boolean): void;
abstract documentExists(docId: string): boolean;
// Hash
abstract setDocumentHash(docId: string, docHash: string): void;
abstract getDocumentHash(docId: string): string | null;
// Ref Docs
abstract getAllRefDocInfo(): {[key: string]: RefDocInfo} | null;
abstract getRefDocInfo(refDocId: string): RefDocInfo | null;
abstract deleteRefDoc(refDocId: string, raiseError: boolean): void;
// Nodes
getNodes(nodeIds: string[], raiseError: boolean = true): Node[] {
return nodeIds.map(nodeId => this.getNode(nodeId, raiseError));
}
getNode(nodeId: string, raiseError: boolean = true): Node {
let doc = this.getDocument(nodeId, raiseError);
if (!(doc instanceof Node)) {
throw new Error(`Document ${nodeId} is not a Node.`);
}
return doc;
}
getNodeDict(nodeIdDict: {[key: number]: string}): {[key: number]: Node} {
let result: {[key: number]: Node} = {};
for (let index in nodeIdDict) {
result[index] = this.getNode(nodeIdDict[index]);
}
return result;
}
}
import { GenericFileSystem } from "../FileSystem";
interface GraphStore {
client: any; // Replace with actual type depending on your usage
get(subj: string): string[][];
getRelMap(subjs?: string[], depth?: number): {[key: string]: string[][]};
upsertTriplet(subj: string, rel: string, obj: string): void;
delete(subj: string, rel: string, obj: string): void;
persist(persistPath: string, fs?: GenericFileSystem): void;
}
import { IndexStruct } from "llama_index/data_structs/data_structs";
import { GenericFileSystem } from "../FileSystem";
import { DEFAULT_PERSIST_DIR, DEFAULT_INDEX_STORE_PERSIST_FILENAME } from "../constants";
const defaultPersistPath = `${DEFAULT_PERSIST_DIR}/${DEFAULT_INDEX_STORE_PERSIST_FILENAME}`;
export abstract class BaseIndexStore {
abstract getIndexStructs(): IndexStruct[];
abstract addIndexStruct(indexStruct: IndexStruct): void;
abstract deleteIndexStruct(key: string): void;
abstract getIndexStruct(structId?: string): IndexStruct | null;
persist(persistPath: string = defaultPersistPath, fs?: GenericFileSystem): void {
// Persist the index store to disk.
}
}
import { GenericFileSystem } from "../FileSystem";
const defaultCollection = "data";
export abstract class BaseKVStore {
abstract put(key: string, val: {[key: string]: any}, collection?: string): void;
abstract get(key: string, collection?: string): {[key: string]: any} | null;
abstract getAll(collection?: string): {[key: string]: {[key: string]: any}};
abstract delete(key: string, collection?: string): boolean;
}
export abstract class BaseInMemoryKVStore extends BaseKVStore {
abstract persist(persistPath: string, fs?: GenericFileSystem): void;
static fromPersistPath(persistPath: string): BaseInMemoryKVStore {
throw new Error("Method not implemented.");
}
}
import { Node } from "../../Node";
import { GenericFileSystem } from "../FileSystem";
export interface NodeWithEmbedding {
node: Node;
embedding: number[];
id(): string;
refDocId(): string;
}
export interface VectorStoreQueryResult {
nodes?: Node[];
similarities?: number[];
ids?: string[];
}
export enum VectorStoreQueryMode {
DEFAULT = "default",
SPARSE = "sparse",
HYBRID = "hybrid",
// fit learners
SVM = "svm",
LOGISTIC_REGRESSION = "logistic_regression",
LINEAR_REGRESSION = "linear_regression",
// maximum marginal relevance
MMR = "mmr"
}
export interface ExactMatchFilter {
key: string;
value: string | number;
}
export interface MetadataFilters {
filters: ExactMatchFilter[];
}
export interface VectorStoreQuerySpec {
query: string;
filters: ExactMatchFilter[];
topK?: number;
}
export interface MetadataInfo {
name: string;
type: string;
description: string;
}
export interface VectorStoreInfo {
metadataInfo: MetadataInfo[];
contentInfo: string;
}
export interface VectorStoreQuery {
queryEmbedding?: number[];
similarityTopK: number;
docIds?: string[];
queryStr?: string;
mode: VectorStoreQueryMode;
alpha?: number;
filters?: MetadataFilters;
mmrThreshold?: number;
}
export interface VectorStore {
storesText: boolean;
isEmbeddingQuery?: boolean;
client(): any;
add(embeddingResults: NodeWithEmbedding[]): string[];
delete(refDocId: string, deleteKwargs?: any): void;
query(query: VectorStoreQuery, kwargs?: any): VectorStoreQueryResult;
persist(persistPath: string, fs?: GenericFileSystem): void;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment