Habr AI showed how to build your own RAG retriever in LangChain for names and terms

Habr AI published a practical walkthrough on a custom RAG retriever for cases where vector search makes mistakes with names, titles, and rare terms. The piece covers the path from text chunking and TF-IDF to LangChain integration and a fair comparison with standard solutions. Ollama is used for the benchmark to quickly prepare questions and check where a custom search setup actually performs better.

AI-processed from Habr AI; edited by Hamidun News
Habr AI showed how to build your own RAG retriever in LangChain for names and terms
Source: Habr AI. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHabr AI ↗
Editor-in-Chief
Key metric #1corpus is cleaned and brought into working form
Key metric #2documents are split into chunks for accurate context return
Key metric #3a TF-IDF model is built from the chunks

Habr AI published a practical guide for RAG engineers who don't get the required accuracy from standard vector search on names, titles, and rare terms. The article shows how to build a custom TF-IDF retriever, integrate it into LangChain, and test it against typical solutions on a benchmark.

Where embeddings break down

The main idea of the article is simple: not every search task needs to be solved with the same vector scheme. Embeddings work well on general questions, but often stumble on named entities. For RAG this is particularly painful, because the model can formulate an answer confidently while relying on the wrong context. The error occurs not at the generation stage, but earlier — when the system retrieves the wrong document fragment.

The weak point of standard search appears where literal differences matter. Names of people, product names, companies, internal systems, technical abbreviations, and rare terms can be too similar in semantic context but critically different in a practical task. If such entities are poorly separated in the embedding space, the quality of results drops even with a good LLM layer. So the idea of a custom retriever here looks not like an ornament for the stack, but as a way to close a specific class of errors.

"And for that I have my own retriever."

Custom retriever scheme

The practical part begins with the most understandable layer — data preparation. Documents need to be split into fragments, or chunks, so that the search returns not the entire text, but a specific relevant piece. After that, a TF-IDF representation is built for the set of chunks. It helps rank fragments by word importance and find matches faster where literal accuracy matters more than semantic similarity. Then, on top of the index, custom search logic is added and all of this is packaged into a LangChain interface. In the article, this pipeline looks maximally practical:

  • corpus is cleaned and brought into working form
  • documents are split into chunks for accurate context return
  • a TF-IDF model is built from the chunks
  • search results are wrapped in a custom retriever for LangChain
  • test questions are separately prepared for comparison with standard options

The strength of this approach is predictability. The engineer better understands why the system selected one fragment or another, and can debug the results without complex infrastructure around a vector database. Plus such a retriever is cheaper to operate and faster to set up for local experiments. This is not a universal replacement for modern solutions, but a good tool for domains where exact entity and formulation matches matter, not "similar meaning."

How results are validated

A separate emphasis is placed on comparison, not just assembly. After creating a custom retriever, the author proposes running it against two or three standard solutions and looking at result quality and speed. This step is important because a custom implementation can easily seem better on a few manual examples but lose on a broader set of queries. The benchmark here acts as a filter against self-deception and helps understand where exactly specialized search provides real gains.

For question preparation, the article uses Ollama. This is a convenient way to quickly assemble a test set for your corpus without tying yourself to an external API and without spending time on completely manual markup. As a result, the material demonstrates a mature engineering approach: first identify a typical error, then select a more suitable search mechanism for it, and only after that compare results on a controlled set of queries. For teams building internal RAG services, such discipline is usually more important than loud promises about a "magical" stack.

What this means

Habr AI's analysis shows a shift in RAG practice maturity: the market is moving away from belief in one universal retriever toward more narrow tuning of search to data and error types. For teams with knowledge bases, catalogs, legal texts, or internal directories this is a good signal: sometimes a noticeable quality boost comes not from a new model, but from a properly assembled search layer.

Story

Key Questions Answered

Where embeddings break down?

The main idea of the article is simple: not every search task needs to be solved with the same vector scheme. Embeddings work well on general questions, but often stumble on named entities. For RAG this is particularly painful, because the model can formulate an answer confidently while relying on the wrong context. The error occurs not at the generation stage, but earlier — when the system retrieves the wrong document fragment. The weak point of standard search appears where literal differences matter. Names of people, product names, companies, internal systems, technical

What are the key details on Custom retriever scheme?

The practical part begins with the most understandable layer — data preparation. Documents need to be split into fragments, or chunks, so that the search returns not the entire text, but a specific relevant piece. After that, a TF-IDF representation is built for the set of chunks. It helps rank fragments by word importance and find matches faster where literal accuracy matters more than semantic similarity. Then, on top of the index, custom search logic is added and all of this is packaged into a LangChain interface. In the article, this pipeline looks maximally practical:

How results are validated?

A separate emphasis is placed on comparison, not just assembly. After creating a custom retriever, the author proposes running it against two or three standard solutions and looking at result quality and speed. This step is important because a custom implementation can easily seem better on a few manual examples but lose on a broader set of queries. The benchmark here acts as a filter against self-deception and helps understand where exactly specialized search provides real gains. For question preparation, the article uses Ollama. This is a convenient way to quickly assembl

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Private AI Builders Community · Vibe Club

Ship AI Faster with Founders, Engineers & Weekly Architecture Reviews

Inside Vibe Club (hamidun.com/club) — weekly live masterminds with Zhemal Khamidun, Tech Radar, full LMS access, and 370+ production Claude Code skills.

Join Vibe Club →Explore Hamidun Academy
What do you think?
Loading comments…