<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shawon Majid</title>
    <description>The latest articles on DEV Community by Shawon Majid (@shawonmajid).</description>
    <link>https://dev.to/shawonmajid</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1256798%2Fd36191e0-f131-4e81-9d7c-cde0c6e60938.jpeg</url>
      <title>DEV Community: Shawon Majid</title>
      <link>https://dev.to/shawonmajid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shawonmajid"/>
    <language>en</language>
    <item>
      <title>RAG Techniques: Multi Query</title>
      <dc:creator>Shawon Majid</dc:creator>
      <pubDate>Wed, 19 Jun 2024 13:49:43 +0000</pubDate>
      <link>https://dev.to/shawonmajid/rag-techniques-multi-query-2p5h</link>
      <guid>https://dev.to/shawonmajid/rag-techniques-multi-query-2p5h</guid>
      <description>&lt;p&gt;In my last project, I used RAG (Retrieval Augmented Generation) for retrieving the relevant context for the user question. But the problem I faced is that, from the user query, the retrieval is not always very accurate. &lt;/p&gt;

&lt;p&gt;For example, In my &lt;a href="https://www.linkedin.com/posts/shawon-majid_langchain-openai-ai-activity-7205634483872530432-8rV3/?utm_source=combined_share_message&amp;amp;utm_medium=member_desktop" rel="noopener noreferrer"&gt;budget AI&lt;/a&gt; project, I embedded the user query directly to match the vector-store database for getting the relevant documents. But for questions like: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What did I buy last month?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The retrieval sometimes provided relevant documents and sometimes failed. Because, this question is too vague and does not provide enough context or specific details (like category or amount) to match effectively with the embedded vectors. The semantic search might struggle to identify which specific records are relevant without additional context.&lt;/p&gt;

&lt;p&gt;To solve this problem, I came across a technique called multi-query. Before matching with the vector-store, I break down the user question into multiple prompts for the semantic search. For example, The given question can be broke into multiple different related prompts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;"List all expenses from last month."&lt;/li&gt;
&lt;li&gt;"Show purchases and expenses made in the last month."&lt;/li&gt;
&lt;li&gt;"What items did I spend money on in the previous month?"&lt;/li&gt;
&lt;li&gt;"Provide details of all transactions from last month."&lt;/li&gt;
&lt;li&gt;"What were my expenses for each category last month?"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This can be easily done with the help of an LLM. Just ask it to break the question into multiple related questions to get relevant documents. The following diagram, taken from the &lt;a href="https://www.langchain.com/" rel="noopener noreferrer"&gt;LangChain&lt;/a&gt; GitHub repository, visualizes the process:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhh0fhlftufnjj5i4etp.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhh0fhlftufnjj5i4etp.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This process significantly increased the relevancy of my documents from the retrieval. LangChain has a built-in function for the same task, you may look at their official documentation for &lt;a href="https://js.langchain.com/v0.1/docs/modules/data_connection/retrievers/multi-query-retriever/" rel="noopener noreferrer"&gt;Multi-Query&lt;/a&gt; retriever.&lt;/p&gt;

&lt;p&gt;I found this technique in &lt;a href="https://github.com/langchain-ai/rag-from-scratch/blob/main/rag_from_scratch_5_to_9.ipynb" rel="noopener noreferrer"&gt;Rag-From-Scratch&lt;/a&gt; by LangChain. There are more sophisticated techniques for improving retrieval capabilities, and I will try to write more articles if I find anything interesting.&lt;/p&gt;

</description>
      <category>rag</category>
      <category>langchain</category>
      <category>llm</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
