Files
LlamaIndexTS/examples/mongo.ts
T
Huu Le 1e59695cef Restructure reader packages (#1877)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2025-04-22 17:20:08 +07:00

65 lines
1.7 KiB
TypeScript

import { SimpleMongoReader } from "@llamaindex/mongodb";
import { Document, VectorStoreIndex } from "llamaindex";
import { MongoClient } from "mongodb";
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline/promises";
async function main() {
//Dummy test code
const filterQuery = {};
const limit: number = Infinity;
const uri: string = process.env.MONGODB_URI ?? "mongodb://localhost:27017";
const client: MongoClient = new MongoClient(uri);
//Where the real code starts
const MR = new SimpleMongoReader(client);
const documents: Document[] = await MR.loadData(
"db",
"collection",
["text"],
"",
filterQuery,
limit,
);
//
//If you need to look at low-level details of
// a queryEngine (for example, needing to check each individual node)
//
// Split text and create embeddings. Store them in a VectorStoreIndex
// var storageContext = await storageContextFromDefaults({});
// const docStore = storageContext.docStore;
// for (const doc of documents) {
// docStore.setDocumentHash(doc.id_, doc.hash);
// }
// const nodes = Settings.nodeParser.getNodesFromDocuments(documents);
// console.log(nodes);
//
// Making Vector Store from documents
//
const index = await VectorStoreIndex.fromDocuments(documents);
// Create query engine
const queryEngine = index.asQueryEngine();
const rl = readline.createInterface({ input, output });
while (true) {
const query = await rl.question("Query: ");
if (!query) {
break;
}
const response = await queryEngine.query({ query });
// Output response
console.log(response.toString());
}
}
void main();