Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Overview

This project turns a PDF into a question-answering chatbot. It walks through the complete RAG pipeline, end to end:

  1. Read text from a PDF document
  2. Split the text into overlapping chunks
  3. Generate a Gemini embedding for every chunk
  4. Store the chunks and embeddings in MongoDB
  5. Embed a user's question
  6. Retrieve the most relevant chunks with MongoDB Atlas Vector Search
  7. 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

Technologies

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

Project Structure

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

Prerequisites

  • 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

Setup

1. Install dependencies

npm install

2. Add your PDF

Place a PDF named sample.pdf in the project root, next to knowledge_base.js.

3. Configure environment variables

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=majority

Do 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.

4. Create the MongoDB vector index

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.


Running the Tutorial

Run the loader first — it creates a fresh set of document records in the documents collection:

node knowledge_base.js

Then start the question-answering chatbot:

node main_rag_system.js

Ask 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?

How It Works

Ingestion

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]
}

Retrieval and Generation

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-flash to answer from that context
  • Returns a fallback response when the answer is not present in the retrieved context

Learning Goals

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

Course Resource

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


License

This project is provided for learning and tutorial purposes.

About

It has the mini RAG project that demonstrates PDF based QA chat bot using Node.js and MongoDB vector search.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages