跪拜 Guibai
← All articles
JavaScript · LangChain

Stop LLM Hallucinations by Building a RAG Pipeline from Scratch

By YIAN ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Hallucination is the single biggest obstacle to deploying LLMs on proprietary data. RAG is the cheapest, fastest way to ground a model in facts without fine-tuning, and understanding its internals — vector search, chunking, prompt assembly — is now table stakes for any developer building AI features on company documents.

Summary

Large language models hallucinate because they cannot access information outside their training data. RAG fixes this by retrieving relevant documents from a private knowledge base and injecting them into the prompt, giving the model factual grounding before it generates a response. The technique mirrors an open-book exam: look up the source material first, then answer.

The core mechanics rely on embedding models that convert text into vectors for semantic search, not just keyword matching. This lets a query about "sport" find a document about "playing football" even when the exact word never appears. A full manual implementation in Node.js walks through vectorizing documents, computing cosine similarity, and splicing retrieved context into a prompt, while a higher-level LangChain.js version shows how to collapse the same pipeline into a few lines with MemoryVectorStore and RetrievalQAChain.

Production RAG demands more than a working demo. Document chunking strategy, hybrid retrieval that combines vector and keyword search, and re-ranking models all determine whether the system returns precise answers or noisy context. The article maps out these optimization paths alongside the foundational code.

Takeaways
LLMs hallucinate on anything outside their training data because they are optimized to produce plausible text, not to signal ignorance.
RAG works in three steps: retrieve relevant snippets from a private knowledge base, augment the prompt with those snippets, and generate an answer grounded in them.
Vector embeddings enable semantic search, so a query for "sport" can match a document about "playing football" even without keyword overlap.
Document chunking is critical: chunks that are too large introduce noise, while chunks that are too small lose necessary context.
A manual RAG implementation requires only an embedding model, a cosine similarity function, and a prompt template that instructs the model to say "I don't know" when context is missing.
LangChain.js collapses the full pipeline into MemoryVectorStore and RetrievalQAChain, reducing boilerplate to a few lines.
Production RAG systems add hybrid retrieval (vector + BM25 keyword search) and re-ranking models to improve recall precision.
Updating a RAG knowledge base means changing documents; no model retraining is required, which keeps iteration fast and cheap.
Conclusions

The manual implementation reveals that RAG's core is embarrassingly simple — vectorize, compare, splice — and the real complexity only emerges at scale with chunking strategies and hybrid retrieval.

Explicitly instructing the model to say "I don't know" when context is absent is a low-tech but essential guardrail; without it, the model will still confabulate from its parametric knowledge.

The jump from a manual cosine-similarity loop to a vector database is purely about scale: the algorithm is identical, but brute-force comparison becomes untenable beyond a few thousand documents.

Concepts & terms
RAG (Retrieval-Augmented Generation)
A pattern that retrieves relevant documents from a knowledge base and inserts them into an LLM's prompt before generation, grounding the response in provided facts rather than parametric knowledge alone.
Embedding Model
A model that converts text, images, or other data into high-dimensional numerical vectors, where semantically similar inputs produce vectors that are close together in vector space.
Cosine Similarity
A measure of similarity between two vectors calculated as the cosine of the angle between them. Values near 1 indicate high semantic similarity; values near 0 indicate unrelated content.
Document Chunking
The process of splitting long documents into smaller, semantically coherent fragments before embedding, balancing context completeness against retrieval precision.
Vector Database
A specialized database designed to store and query high-dimensional vectors with millisecond-latency similarity search, essential for RAG systems at scale. Examples include Chroma, Pinecone, and Milvus.
Hybrid Retrieval
A retrieval strategy that combines vector-based semantic search with traditional keyword search (e.g., BM25) and re-ranks the merged results, improving recall for both conceptual queries and exact-term matches.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗