A Two-Step RAG Walkthrough: From a Handwritten Story to Crawling Juejin Articles
The pipeline shows how few moving parts a working RAG system actually needs — a loader, a splitter, an embedder, a vector store, and a prompt template — and that the same code runs against any OpenAI-compatible API, including Chinese models. For a developer prototyping a doc-Q&A feature, the whole thing fits in a single script.
RAG stops LLMs from hallucinating by retrieving relevant text before generation. The pipeline starts with a Cheerio-based web loader that scrapes only the article body via a CSS selector, then hands the raw text to RecursiveCharacterTextSplitter. That splitter first breaks on sentence-ending punctuation and greedily merges sentences until a chunk nears 400 characters, with a 50-character overlap to keep context from being severed at boundaries.
Vectors land in MemoryVectorStore, which runs cosine similarity between the question embedding and every stored chunk, returning the top K matches. Those chunks get spliced into a prompt template, and the LLM answers from the provided context rather than from stale training data. The same code works with Qwen, DeepSeek, or any domestic model that exposes an OpenAI-compatible endpoint — swap the base URL and key, nothing else changes.
A comparison table lays out five LangChain splitters side by side. RecursiveCharacterTextSplitter is the only one that cuts on semantic boundaries first and then merges; the others use fixed windows that can slice through mid-sentence, making the recursive variant the default choice for most RAG setups.
The article treats RecursiveCharacterTextSplitter's 'cut-then-merge' strategy as the key differentiator from other splitters, which is a useful mental model for debugging chunk quality.
Running the entire vector store in memory with MemoryVectorStore makes the example instantly reproducible without infrastructure, but the article never flags the obvious scaling limit — it works for one article, not a corpus.
The OpenAI-compatible API note is a practical bridge: Western developers often assume LangChain.js ties them to OpenAI, but the ecosystem has converged on that interface.