A fully local Retrieval-Augmented Generation chatbot that lets you upload any PDF and immediately have a conversation with its content — no cloud API keys, no external services. Built in Node.js with LangChain.js and served via Express, the system extracts text from uploaded PDFs, splits it into 512-token overlapping chunks, embeds them using Ollama's llama3.2 model running locally, and stores the vectors as JSON for instant reloading. At query time, the user's question is embedded, semantically matched against the stored vectors, and the top-3 most relevant chunks are injected into a grounded prompt sent to the local LLM — eliminating hallucination entirely. The drag-and-drop web UI shows live ingestion status, switches between upload and chat modes seamlessly, and allows hot-swapping documents without restarting the server.
What it does
- Drag-and-drop PDF upload directly in the browser — no CLI required
- Text extraction with pypdf, split into 512-token chunks with 128-token overlap
- Local embeddings via Ollama llama3.2 — no API keys or internet connection
- Pure-JS MemoryVectorStore with JSON persistence — no native C++ modules
- Top-3 semantic retrieval using cosine similarity at query time
- LLM constrained to document context — says "I don't know" if out of scope
- Hot-swap documents via "Upload new PDF" without restarting the server
- LangChain.js orchestration: createRetrievalChain + createStuffDocumentsChain
From the code
const { ChatOllama } = require('@langchain/ollama');
const { ChatPromptTemplate } = require('@langchain/core/prompts');
const { createStuffDocumentsChain } = require('langchain/chains/combine_documents');
const { createRetrievalChain } = require('langchain/chains/retrieval');
async function buildChain(retriever) {
// Local LLM — runs entirely on your machine via Ollama
const llm = new ChatOllama({
model: 'llama3.2',
baseUrl: 'http://127.0.0.1:11434',
});
// Inline prompt — no hub.pull(), no API key needed
// System message constrains LLM to only use retrieved context
const prompt = ChatPromptTemplate.fromMessages([
['system',
'You are a helpful assistant. Answer using ONLY the context below. ' +
'If the answer is not in the context, say ' +
'"I don\'t know based on the provided document."\n\nContext: {context}'],
['human', '{input}'],
]);
// Stuff chain: concatenates top-k chunks into {context}, sends to LLM
const combineDocsChain = await createStuffDocumentsChain({ llm, prompt });
// Retrieval chain: embed query → cosine search → stuff → LLM → { answer }
return createRetrievalChain({ retriever, combineDocsChain });
}


