Skip to content

Commit

Permalink
refactor: improve DX using image vector stores
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusschiesser committed Dec 18, 2023
1 parent e381f95 commit 76f7256
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
11 changes: 1 addition & 10 deletions examples/multimodal/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ServiceContext,
serviceContextFromDefaults,
SimpleDirectoryReader,
SimpleVectorStore,
storageContextFromDefaults,
VectorStoreIndex,
} from "llamaindex";
Expand All @@ -22,17 +21,9 @@ async function generateDatasource(serviceContext: ServiceContext) {
const documents = await new SimpleDirectoryReader().loadData({
directoryPath: path.join("multimodal", "data"),
});
// set up vector store index with two vector stores, one for text, the other for images
const vectorStore = await SimpleVectorStore.fromPersistDir(
path.join("storage", "text"),
);
const imageVectorStore = await SimpleVectorStore.fromPersistDir(
path.join("storage", "images"),
);
const storageContext = await storageContextFromDefaults({
persistDir: "storage",
vectorStore,
imageVectorStore,
storeImages: true,
});
await VectorStoreIndex.fromDocuments(documents, {
serviceContext,
Expand Down
10 changes: 1 addition & 9 deletions examples/multimodal/retrieve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
ImageNode,
serviceContextFromDefaults,
SimpleVectorStore,
storageContextFromDefaults,
TextNode,
VectorStoreIndex,
Expand All @@ -14,16 +13,9 @@ export async function createRetriever() {
chunkSize: 512,
chunkOverlap: 20,
});
const vectorStore = await SimpleVectorStore.fromPersistDir(
path.join("storage", "text"),
);
const imageVectorStore = await SimpleVectorStore.fromPersistDir(
path.join("storage", "images"),
);
const storageContext = await storageContextFromDefaults({
persistDir: "storage",
vectorStore,
imageVectorStore,
storeImages: true,
});
const index = await VectorStoreIndex.init({
nodes: [],
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/storage/StorageContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as path from "path";
import { GenericFileSystem } from "./FileSystem";
import { DEFAULT_FS, DEFAULT_NAMESPACE } from "./constants";
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";
Expand All @@ -19,6 +24,7 @@ type BuilderParams = {
indexStore?: BaseIndexStore;
vectorStore?: VectorStore;
imageVectorStore?: VectorStore;
storeImages?: boolean;
persistDir?: string;
fs?: GenericFileSystem;
};
Expand All @@ -28,13 +34,15 @@ export async function storageContextFromDefaults({
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 =
Expand All @@ -48,6 +56,12 @@ export async function storageContextFromDefaults({
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 {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/storage/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const DEFAULT_DOC_STORE_PERSIST_FILENAME = "doc_store.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 DEFAULT_IMAGE_VECTOR_NAMESPACE = "images";
export { DEFAULT_FS } from "./FileSystem";

0 comments on commit 76f7256

Please sign in to comment.