Skip to content
Snippets Groups Projects
Unverified Commit fc7f3514 authored by yisding's avatar yisding Committed by GitHub
Browse files

Merge pull request #21 from run-llama/logan/storage_demo

initial storage examples attempt
parents 629bd323 fc65e6b9
No related branches found
No related tags found
No related merge requests found
import fs from "fs/promises";
import { Document, VectorStoreIndex, storageContextFromDefaults } from "llamaindex";
async function main() {
// Load essay from abramov.txt in Node
const essay = await fs.readFile(
"node_modules/llamaindex/examples/abramov.txt",
"utf-8"
);
// Create Document object with essay
const document = new Document({ text: 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 loadedIndex = await VectorStoreIndex.fromDocuments([], storageContext);
const laodedQueryEngine = loadedIndex.asQueryEngine();
const loadedResponse = await laodedQueryEngine.query(
"What did the author do growing up?"
);
console.log(loadedResponse.toString());
}
main().catch(console.error);
import { existsSync, rmSync } from "fs";
import { storageContextFromDefaults, StorageContext } from "../storage/StorageContext";
import { Document } from "../Node";
import { VectorStoreIndex } from "../indices";
import { ListIndex } from "../indices";
describe("StorageContext", () => {
test("initializes", async () => {
const storageContext = await storageContextFromDefaults({ persistDir: "/tmp/test_dir" });
expect(existsSync("/tmp/test_dir")).toBe(true);
expect(storageContext).toBeDefined();
// cleanup
rmSync("/tmp/test_dir", { recursive: true });
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment