-
Notifications
You must be signed in to change notification settings - Fork 305
/
StorageContext.ts
73 lines (69 loc) · 2.01 KB
/
StorageContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as path from "path";
import { GenericFileSystem } from "./FileSystem";
import {
DEFAULT_FS,
DEFAULT_IMAGE_VECTOR_NAMESPACE,
DEFAULT_NAMESPACE,
} from "./constants";
import { SimpleDocumentStore } from "./docStore/SimpleDocumentStore";
import { BaseDocumentStore } from "./docStore/types";
import { SimpleIndexStore } from "./indexStore/SimpleIndexStore";
import { BaseIndexStore } from "./indexStore/types";
import { SimpleVectorStore } from "./vectorStore/SimpleVectorStore";
import { VectorStore } from "./vectorStore/types";
export interface StorageContext {
docStore: BaseDocumentStore;
indexStore: BaseIndexStore;
vectorStore: VectorStore;
imageVectorStore?: VectorStore;
}
type BuilderParams = {
docStore?: BaseDocumentStore;
indexStore?: BaseIndexStore;
vectorStore?: VectorStore;
imageVectorStore?: VectorStore;
storeImages?: boolean;
persistDir?: string;
fs?: GenericFileSystem;
};
export async function storageContextFromDefaults({
docStore,
indexStore,
vectorStore,
imageVectorStore,
storeImages,
persistDir,
fs,
}: BuilderParams): Promise<StorageContext> {
if (!persistDir) {
docStore = docStore || new SimpleDocumentStore();
indexStore = indexStore || new SimpleIndexStore();
vectorStore = vectorStore || new SimpleVectorStore();
imageVectorStore = storeImages ? new SimpleVectorStore() : imageVectorStore;
} else {
fs = fs || DEFAULT_FS;
docStore =
docStore ||
(await SimpleDocumentStore.fromPersistDir(
persistDir,
DEFAULT_NAMESPACE,
fs,
));
indexStore =
indexStore || (await SimpleIndexStore.fromPersistDir(persistDir, fs));
vectorStore =
vectorStore || (await SimpleVectorStore.fromPersistDir(persistDir, fs));
imageVectorStore = storeImages
? await SimpleVectorStore.fromPersistDir(
path.join(persistDir, DEFAULT_IMAGE_VECTOR_NAMESPACE),
fs,
)
: imageVectorStore;
}
return {
docStore,
indexStore,
vectorStore,
imageVectorStore,
};
}