This project turns a PDF into a question-answering chatbot. It walks through the complete RAG pipeline, end to end:
- Read text from a PDF document
- Split the text into overlapping chunks
- Generate a Gemini embedding for every chunk
- Store the chunks and embeddings in MongoDB
- Embed a user's question
- Retrieve the most relevant chunks with MongoDB Atlas Vector Search
- Ask Gemini to answer using the retrieved context
sample.pdf
|
v
Chunk text -> Gemini embeddings -> MongoDB collection
^
|
User question -> Gemini embedding -> Vector Search -> Gemini answer
| Tool | Purpose |
|---|---|
| Google Gemini API | Embeddings and answer generation |
| MongoDB Atlas Vector Search | Vector storage and retrieval |
@google/genai |
Gemini SDK |
pdf-parse |
PDF text extraction |
dotenv |
Environment configuration |
| File | Purpose |
|---|---|
knowledge_base.js |
Reads sample.pdf, chunks its text, creates embeddings, and loads documents into MongoDB |
main_rag_system.js |
Runs the interactive terminal chatbot and answers questions using vector retrieval |
package.json |
Project metadata and runtime dependencies |
.env |
Local API and database configuration — keep this file private |
sample.pdf |
The source document to index — add your own PDF to the project root |
- A Google AI Studio API key with access to Gemini models and embeddings
- A MongoDB Atlas cluster with Vector Search enabled
- A PDF document to use as the knowledge base
npm installPlace a PDF named sample.pdf in the project root, next to knowledge_base.js.
Create a .env file in the project root:
Gemini_Key=your_google_ai_studio_api_key
MongoDB=mongodb+srv://<username>:<password>@<cluster-url>/?retryWrites=true&w=majorityDo not commit .env or expose your API key and MongoDB connection string. The variable names are case-sensitive and must match the names used by the application.
The application uses database rag-tutorial, collection documents, and vector index name vector_index.
In MongoDB Atlas, open Search & Vector Search for the documents collection and create a vector search index with this definition:
{
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 3072,
"similarity": "cosine"
}
]
}Name the index vector_index and wait until its status is Ready. The numDimensions value matches the gemini-embedding-001 embeddings used by this project. If you change the embedding model, update the index dimensions to match the new model.
Run the loader first — it creates a fresh set of document records in the documents collection:
node knowledge_base.jsThen start the question-answering chatbot:
node main_rag_system.jsAsk questions about the content of your PDF. Type exit to close the chatbot.
Welcome to Document QA Bot
Ask question: What is this document about?
knowledge_base.js normalizes the extracted PDF text and splits it into chunks of 600 characters with a 100-character overlap. Each chunk is embedded with gemini-embedding-001 and saved as:
{
"chunkText": "...",
"embedding": [0.0123, -0.0456]
}When a question is submitted, main_rag_system.js:
- Creates an embedding for the question
- Uses MongoDB Vector Search to retrieve the three closest chunks
- Places those chunks into the prompt context
- Asks
gemini-3.6-flashto answer from that context - Returns a fallback response when the answer is not present in the retrieved context
By following this tutorial, you will understand the core RAG pipeline and how its main stages connect:
- Document loading and text extraction
- Chunking and overlap
- Vector embeddings
- Semantic retrieval
- Context-grounded generation
- Building a simple terminal-based AI application
This repository supports the RAG TUTORIAL content on MK CODE CLUB.
Subscribe and follow the channel for more programming and AI tutorials: MK CODE CLUB on YouTube
This project is provided for learning and tutorial purposes.