跪拜 Guibai
← All articles
LLM · JavaScript · Database

RAG in 162 Lines: The Cognitive Architecture Shift That Turns LLMs Into Scholars

By 小月土星 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

RAG is the dominant pattern for grounding LLM outputs in external truth, yet most production implementations obscure the core loop behind frameworks and infrastructure. Seeing the entire pipeline in a single, runnable file—with scores, metadata, and prompt construction all visible—collapses the perceived complexity and makes the retrieval-generation contract tangible for any developer with Node.js.

Summary

A complete RAG system fits in 162 lines of JavaScript, moving from document ingestion and embedding to semantic retrieval and augmented generation. The implementation deliberately uses in-memory vector storage and a story-based dataset to expose cross-document reasoning, temporal dependencies, and emotional arcs that FAQ-style test data hides. Each design choice—the Document abstraction's separation of pageContent from metadata, the retriever interface, the anti-hallucination prompt guardrail—is unpacked as a deliberate architectural decision. The jump from a 94-line incomplete prototype to the full 162-line demo required only 68 additional lines, each one closing a specific gap in the pipeline. The code runs against Alibaba Cloud's DashScope via an OpenAI-compatible endpoint, proving vendor portability is a one-line baseURL change.

Takeaways
162 lines of JavaScript and two API calls are enough to run a complete RAG pipeline: ingestion, embedding, retrieval, and generation.
Separating Document.pageContent (for semantic embedding) from Document.metadata (for structured filtering) keeps precision and fuzziness from polluting each other.
MemoryVectorStore is intentionally used for zero-config prototyping; swapping it for pgvector or Pinecone later requires no changes to the retriever interface.
Story-based test data with cross-document reasoning and emotional arcs exposes RAG weaknesses that isolated FAQ entries never reveal.
similaritySearchWithScore returns cosine distance; converting it via 1 - score yields intuitive cosine similarity, a diagnostic signal for knowledge base health.
An anti-hallucination prompt instruction ('If the story doesn't mention it, say so') acts as a lightweight guardrail that defines the LLM's knowledge boundary.
Temperature 0 is the correct default for RAG because factual consistency and reproducibility matter more than creative variation when answers are grounded in retrieved documents.
Using an OpenAI-compatible baseURL lets the same code call DashScope, OpenAI, Azure, or a local vLLM instance without changing a single line.
Conclusions

The 68-line gap between the incomplete prototype and the working demo is not about missing code volume but about missing concepts: vector store, retriever abstraction, similarity scoring, and prompt orchestration.

Metadata fields like chapter and mood encode temporal and emotional structure that pure vector search cannot see, hinting that RAG's next evolution is a genuine memory system, not just a fact-lookup tool.

The deliberate choice of narrative test data over FAQs is a testing philosophy argument: stress-test a system with data that demands cross-document reasoning, or you will not discover its real failure modes.

Embedding models and chat models are fundamentally different model types, but LangChain's unified OpenAI-compatible abstraction treats them as interchangeable components—this is powerful but also obscures their distinct failure characteristics.

Concepts & terms
Cosine Distance vs. Cosine Similarity
Cosine distance ranges [0,2] where 0 means identical direction. Cosine similarity = 1 - distance, ranging [-1,1] where 1 means identical. Modern embedding vectors typically fall in the positive quadrant, so similarity scores usually land between 0 and 1.
Document Abstraction (LangChain)
A data structure with two fields: pageContent (the text that gets embedded into a vector) and metadata (structured key-value pairs used for filtering and traceability, never embedded). This separation keeps semantic search and exact filtering orthogonal.
Retriever Interface
A standard abstraction that wraps a vector store and exposes a single method: input a query string, output a list of relevant Document objects. It hides the embedding and distance-calculation steps from the caller.
Top-K Retrieval
After computing similarity between a query vector and all document vectors, only the K documents with the highest similarity scores are returned. K is a tunable parameter that balances context richness against noise and prompt length.
Temperature in RAG
A parameter controlling output randomness. In RAG, temperature 0 is preferred because answers must be factually grounded in retrieved documents; creative variation risks introducing hallucinations that contradict the provided context.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗