mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-21 14:55:36 -04:00
6d4d96f8fe
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { HuggingFaceEmbedding } from "@llamaindex/huggingface";
|
|
import {
|
|
Document,
|
|
MetadataReplacementPostProcessor,
|
|
SentenceWindowNodeParser,
|
|
Settings,
|
|
VectorStoreIndex,
|
|
} from "llamaindex";
|
|
|
|
import essay from "./essay";
|
|
|
|
// Update node parser and embed model
|
|
Settings.nodeParser = new SentenceWindowNodeParser({
|
|
windowSize: 3,
|
|
windowMetadataKey: "window",
|
|
originalTextMetadataKey: "original_text",
|
|
});
|
|
Settings.embedModel = new HuggingFaceEmbedding();
|
|
|
|
async function main() {
|
|
const document = new Document({ text: essay, id_: "essay" });
|
|
|
|
// Split text and create embeddings. Store them in a VectorStoreIndex
|
|
const index = await VectorStoreIndex.fromDocuments([document], {
|
|
logProgress: true,
|
|
});
|
|
|
|
// Query the index
|
|
const queryEngine = index.asQueryEngine({
|
|
nodePostprocessors: [new MetadataReplacementPostProcessor("window")],
|
|
});
|
|
|
|
const response = await queryEngine.query({
|
|
query: "What did the author do in college?",
|
|
});
|
|
|
|
// Output response
|
|
console.log(response.toString());
|
|
}
|
|
|
|
main().catch(console.error);
|