Repository navigation
Expand file tree
/
Copy pathmain_rag_system.js
More file actions
71 lines (56 loc) · 2.3 KB
/
Copy pathmain_rag_system.js
File metadata and controls
71 lines (56 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import readline from "readline";
import { MongoClient } from "mongodb";
import { GoogleGenAI } from "@google/genai";
import dotenv from "dotenv";
dotenv.config();
const ai = new GoogleGenAI({apiKey: process.env.Gemini_Key});
const mongoClient = new MongoClient(process.env.MongoDB);
const rl = readline.createInterface({input: process.stdin, output: process.stdout});
async function findRelevantChunks(collection, question){
const qsEmbedding = await ai.models.embedContent({model:"gemini-embedding-001", contents: question});
const qsVector = qsEmbedding.embeddings[0].values;
const pipeline = [
{
$vectorSearch: {index: "vector_index", path: "embedding", queryVector: qsVector, numCandidates: 10, limit: 3 }
},
{
$project: {_id: 0, chunkText: 1}
}
]
const results = await collection.aggregate(pipeline).toArray();
return results;
}
async function askQuestion(collection, question){
const retrievedChunks = await findRelevantChunks(collection, question);
// console.log(retrievedChunks);
const inString = retrievedChunks.map(chunk => chunk.chunkText).join("\n\n===\n\n");
const prompt = `You are a intelligent chatbot for NovaTech. Answer the question based on the context that given below. If answer cannot be found in the given
context, then say "I don't have the relevant information about the question"
Context: ${inString}, User Question: ${question}, Answer:`;
// gemini llm
const gemini_response = await ai.models.generateContent({ model: "gemini-3.6-flash", contents: prompt});
return gemini_response.text;
}
async function main(){
await mongoClient.connect();
const collection = mongoClient.db("rag-tutorial").collection("documents");
console.log("\nWelcome to Document QA Bot\n");
const promptUser = () =>{
rl.question("Ask question: ", async(input) => {
if(input.trim().toLowerCase() === "exit"){
rl.close();
return;
}
try{
const answer = await askQuestion(collection, input);
console.log("\nResponse: ", answer, "\n");
}
catch(error){
console.log(error.message);
}
promptUser();
});
};
promptUser();
}
main();