, also referred to as RAG, is a strong methodology to seek out related paperwork in a corpus of data, which you then present to an LLM to provide solutions to consumer questions.
Historically, RAG first makes use of vector similarity to seek out related chunks of paperwork within the corpus after which feeds essentially the most related chunks into the LLM to offer a response.
This works rather well in lots of situations since semantic similarity is a strong strategy to discover essentially the most related chunks. Nonetheless, semantic similarity struggles in some situations, for instance, when a consumer inputs particular key phrases or IDs that should be explicitly positioned for use as a related chunk. In these cases, vector similarity isn’t that efficient, and also you want a greater strategy to seek out essentially the most related chunks.
That is the place key phrase search is available in, the place you discover related chunks whereas utilizing key phrase search and vector similarity, also referred to as hybrid search, which is the subject I’ll be discussing right this moment.
Why use hybrid search
Vector similarity could be very highly effective. It is ready to successfully discover related chunks from a corpus of paperwork, even when the enter immediate has typos or makes use of synonyms such because the phrase elevate as an alternative of the phrase elevator.
Nonetheless, vector similarity falls quick in different situations, particularly when trying to find particular key phrases or identification numbers. The explanation for that is that vector similarity doesn’t weigh particular person phrases or IDs particularly extremely in comparison with different phrases. Thus, key phrases or key identifiers are usually drowned in different related phrases, which makes it laborious for semantic similarity to seek out essentially the most related chunks.
Key phrase search, nevertheless, is extremely good at key phrases and particular identifiers, because the title suggests. With BM25, for instance, you probably have a phrase that solely exists in a single doc and no different paperwork, and that phrase is within the consumer question, that doc will likely be weighed very extremely and most certainly included within the search outcomes.
That is the principle motive you wish to use a hybrid search. You’re merely capable of finding extra related paperwork if the consumer is inputting key phrases into their question.
The way to implement hybrid search
There are quite a few methods to implement hybrid search. If you wish to implement it your self, you are able to do the next.
- Implement vector retrieval by way of semantic similarity as you’d have usually accomplished. I gained’t cowl the precise particulars on this article as a result of it’s out of scope, and the principle level of this text is to cowl the key phrase search a part of hybrid search.
- Implement BM25 or one other key phrase search algorithm that you just want. BM25 is a typical because it builds upon TF-IDF and has a greater method, making it the higher alternative. Nonetheless, the precise key phrase search algorithm you utilize doesn’t actually matter, although I like to recommend utilizing BM25 as the usual.
- Apply a weighting between the similarity discovered by way of semantic similarity and key phrase search similarity. You’ll be able to resolve this weighting your self relying on what you regard as most necessary. You probably have an agent performing a hybrid search, you can even have the agent resolve this weighting, as brokers will usually have a very good instinct for when to make use of or when to attend, left or similarity extra, and when to weigh key phrase search similarity extra
There are additionally packages you should utilize to realize this, equivalent to TurboPuffer vector storage, which has a Keyboard Search bundle applied. To find out how the system actually works, nevertheless, it’s additionally really useful that you just implement this your self to check out the system and see if it really works.
General, nevertheless, hybrid search isn’t actually that tough to implement and can provide lots of advantages. If you happen to’re wanting right into a hybrid search, you usually understand how vector search itself works and also you merely want so as to add the key phrase search factor to it. Key phrase search itself isn’t actually that difficult both, which makes hybrid search a comparatively easy factor to implement, which may yield lots of advantages.
Agentic hybrid search
Implementing hybrid search is nice, and it’ll in all probability enhance how properly your RAG system works proper off the bat. Nonetheless, I imagine that in case you actually wish to get essentially the most out of a hybrid search RAG system, you want to make it agentic.
By making it agentic, I imply the next. A typical RAG system first fetches related chunks, doc chunks, feeds these chunks into an LLM, and has it reply a consumer query
Nonetheless, an agentic RAG system does it a bit in a different way. As an alternative of doing the trunk retrieval earlier than utilizing an LLM to reply, you make the trunk retrieval perform a device that the LLM can entry. This, after all, makes the LLM agentic, so it has entry to a device and has a number of main benefits:
- The agent can itself resolve the immediate to make use of for the vector search. So as an alternative of utilizing solely the precise consumer immediate, it might rewrite the immediate to get even higher vector search outcomes. Question rewriting is a widely known method you should utilize to enhance RAG efficiency.
- The agent can iteratively fetch the knowledge, so it might first do one vector search name, test if it has sufficient data to reply a query, and if not, it might fetch much more data. This makes it so the agent can assessment the knowledge it fetched and, if wanted, fetch much more data, which can make it higher capable of reply questions.
- The agent can resolve the weighting between key phrase search and vector similarity itself. That is extremely highly effective as a result of the agent usually is aware of if it’s trying to find a key phrase or if it’s trying to find semantically comparable content material. For instance, if the consumer included a key phrase of their search question, the agent will seemingly weigh the key phrase search factor of hybrid search greater, and let’s get even higher outcomes. This works lots higher than having a static quantity for the weighting between key phrase search and vector similarity.
At this time’s Frontier LLMs are extremely highly effective and can be capable of make all of those judgments themselves. Just some months in the past, I’d doubt in case you ought to give the agent as a lot freedom as I described within the bullet factors above, having it choose immediate use, iteratively fetching data, and the weighting between key phrase search and semantic similarity. Nonetheless, right this moment I do know that the most recent Frontier LLMs have turn out to be so highly effective that that is very doable and even one thing I like to recommend implementing.
Thus, by each implementing HybridSearch and by making it agentic, you’ll be able to actually supercharge your RAG system and obtain much better outcomes than you’d have achieved with a static vector similarity-only RAG system.
Conclusion
On this article, I’ve mentioned the way to implement hybrid search into your RAG system. Moreover, I described the way to make your RAG system genuine to realize much better outcomes. Combining these two methods will result in an unimaginable efficiency improve in your data retrieval system, and it might, actually, be applied fairly simply utilizing coding brokers equivalent to Claude Code. I imagine Agentex Programs is the way forward for data retrieval, and I urge you to offer efficient data retrieval instruments, equivalent to a hybrid search, to your brokers and make them carry out the remainder of the work.
👉 My free eBook and Webinar:
🚀 10x Your Engineering with LLMs (Free 3-Day Email Course)
📚 Get my free Vision Language Models ebook
💻 My webinar on Vision Language Models
👉 Discover me on socials:
💌 Substack

