-
Yi Ding authored
Allows our scripts to be run repeatedly with vector store backing and persistence without throwing an error. This is rudimentary support which doesn't work if the document has changed over time. Also added the insert and delete functions so that documents can be added manually.
Yi Ding authoredAllows our scripts to be run repeatedly with vector store backing and persistence without throwing an error. This is rudimentary support which doesn't work if the document has changed over time. Also added the insert and delete functions so that documents can be added manually.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
storageContext.ts 1.22 KiB
import {
Document,
storageContextFromDefaults,
VectorStoreIndex,
} from "llamaindex";
import essay from "./essay";
async function main() {
// Create Document object with essay
const document = new Document({ text: essay, id_: "essay" });
// Split text and create embeddings. Store them in a VectorStoreIndex
// persist the vector store automatically with the storage context
const storageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
const index = await VectorStoreIndex.fromDocuments([document], {
storageContext,
});
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What did the author do in college?",
);
// Output response
console.log(response.toString());
// load the index
const secondStorageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
const loadedIndex = await VectorStoreIndex.init({
storageContext: secondStorageContext,
});
const loadedQueryEngine = loadedIndex.asQueryEngine();
const loadedResponse = await loadedQueryEngine.query(
"What did the author do growing up?",
);
console.log(loadedResponse.toString());
}
main().catch(console.error);