<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Santosh Pasala on Medium]]></title>
        <description><![CDATA[Stories by Santosh Pasala on Medium]]></description>
        <link>https://medium.com/@santoshp12122?source=rss-56f930d9b458------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Santosh Pasala on Medium</title>
            <link>https://medium.com/@santoshp12122?source=rss-56f930d9b458------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 10:33:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@santoshp12122/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building an “Agentic” RAG System: Connecting SQL and Documents with LlamaIndex and Gemini]]></title>
            <link>https://medium.com/@santoshp12122/building-an-agentic-rag-system-connecting-sql-and-documents-with-llamaindex-and-gemini-bed8812e6fef?source=rss-56f930d9b458------2</link>
            <guid isPermaLink="false">https://medium.com/p/bed8812e6fef</guid>
            <category><![CDATA[business-data]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[business-analysis]]></category>
            <category><![CDATA[gen-ai-for-business]]></category>
            <dc:creator><![CDATA[Santosh Pasala]]></dc:creator>
            <pubDate>Mon, 26 Jan 2026 20:11:07 GMT</pubDate>
            <atom:updated>2026-01-26T20:11:07.399Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Bridging the gap between your sales dashboard and product manuals using Generative AI.</blockquote><p><strong>Imagine this:</strong> You’re an analyst. Your boss asks, <em>“How many X-2000 units did we sell in the East region last month?”</em> You check the SQL database. Easy. Then they ask, <em>“And by the way, how do I reset the X-2000 if it overheats?”</em> You sigh, close your SQL client, open a PDF viewer, and search through 50 pages of manuals.</p><p>Data in the real world is messy. It lives in <strong>silos</strong>. Structured data sits in rows and columns, and unstructured knowledge sits in docs and wikis. Traditional RAG (Retrieval-Augmented Generation) is great for the docs, and Text-to-SQL is great for the rows. <strong>But what if you want both?</strong></p><p>In this post, I’ll walk you through how I built an <strong>Agentic RAG</strong> system using <strong>LlamaIndex</strong>, <strong>Google Gemini</strong>, and <strong>PostgreSQL</strong>/<strong>MySQL</strong>. This agent doesn’t just “search” text — it <em>thinks</em>, decides whether to query a database or read a manual, and gives you a single answer.</p><h3>Main Objective</h3><p>The primary objective is to build an <strong>Agentic</strong> system that acts as a unified reasoning engine. The system must:</p><ol><li>Understand the intent behind the question.</li><li>Automatically<strong> </strong>decide whether to query a database or search a document store.</li><li>Generate syntactic SQL for a relational database (PostgreSQL/MySQL).</li><li>Perform semantic vector retrieval for text documents.</li><li>Synthesize a coherent, human-readable answer merging data from both sources.</li></ol><h3>The Architecture: A “Reasoning” Router</h3><p>We didn’t just want a chatbot; we wanted an agent with <strong>tools</strong>.</p><ol><li><strong>The Brain</strong>: Google Gemini Pro (LLM).</li><li><strong>Tool A (The Analyst)</strong>: A component that understands SQL schemas and can query a PostgreSQL/MySQL database.</li><li><strong>Tool B (The Librarian)</strong>: A Vector Store Retriever that performs semantic search over product manuals.</li><li><strong>The Router</strong>: The one SQLAutoVectorQueryEngine from LlamaIndex that directs traffic.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*-slLtTSKcL-iJCDV3uUKgg.png" /></figure><h3>Step 1: The Stack</h3><p>We kept it powerful (and local) for development:</p><ul><li><strong>Python 3.9+</strong></li><li><strong>LlamaIndex</strong>: The orchestration framework.</li><li><strong>PostgreSQL</strong>/<strong>MySQL</strong>: Running locally (simulating enterprise data).</li><li><strong>Google Gemini</strong>: For reasoning and embeddings (it’s fast and effective).</li><li><strong>Streamlit</strong>: For a quick, interactive UI.</li></ul><h3>Step 2: Defining the Tools</h3><p>The magic of Agentic RAG lies in the <strong>descriptions</strong>. We treat our data sources as functions that the LLM can call.</p><h4><strong>The SQL Tool</strong></h4><p>We wrap our <strong>PostgreSQL</strong>/<strong>MySQL</strong> connection with NLSQLTableQueryEngine. We give it a description so the agent knows <em>when</em> to use it.</p><pre>def get_sql_tool(sql_database):<br>    return QueryEngineTool.from_defaults(<br>        query_engine=sql_query_engine,<br>        name=&quot;sql_tool&quot;,<br>        description=(<br>            &quot;Useful for translating a natural language query into a SQL query over a database containing: &quot;<br>            &quot;sales (amount, date, region) and inventory (stock_level). &quot;<br>            &quot;Use this for structured data questions requiring math or aggregation.&quot;<br>        )<br>    )</pre><h4>The Vector Tool</h4><p>For the manuals, we use a VectorIndexAutoRetriever. This isn&#39;t just a keyword search; it uses embeddings to find concepts (e.g., matching &quot;broken&quot; to &quot;troubleshooting&quot;).</p><pre>def get_vector_tool(vector_index):<br>    return QueryEngineTool.from_defaults(<br>        query_engine=vector_query_engine,<br>        name=&quot;vector_tool&quot;,<br>        description=(<br>            &quot;Useful for answering questions about product manuals, features, usage instructions, &quot;<br>            &quot;and warranty. Use this for unstructured text queries.&quot;<br>        )<br>    )</pre><h3>Step 3: The “Agent” Logic</h3><p>With our tools defined, initializing the agent is surprisingly simple. We use theSQLAutoVectorQueryEngine, a specialized class in LlamaIndex designed exactly for this &quot;SQL vs. Vector&quot; problem.</p><pre>query_engine = SQLAutoVectorQueryEngine(<br>    sql_query_tool=sql_tool,<br>    vector_query_tool=vector_tool,<br>    llm=Settings.llm<br>)</pre><p>When a user sends a query, this engine doesn’t guess. It looks at the query and the tool descriptions.</p><ul><li><strong>Query</strong>: <em>“Total revenue in Q3?”</em> -&gt; Matches “sales”, “math” -&gt; <strong>Calls SQL Tool</strong>.</li><li><strong>Query</strong>: <em>“Warranty for X-2000?”</em> -&gt; Matches “manuals”, “warranty” -&gt; <strong>Calls Vector Tool</strong>.</li></ul><h3>Step 4: The Interface</h3><p>I built a clean frontend using <strong>Streamlit</strong>. It allows users to chat naturally. The best part? The system is transparent. It can show you the generated SQL or the retrieved text chunks, building trust in the results.</p><figure><img alt="Image of the interface of Agentic SQl-RAG" src="https://cdn-images-1.medium.com/max/1024/1*x11jsOQB5FlLgeSCPXWf9w.png" /></figure><h3>Challenges &amp; Learnings</h3><p>Building this wasn’t without hurdles.</p><ol><li><strong>Schema Ambiguity</strong>: If you ask “How many widgets?”, the LLM needs to know that “widgets” maps to the product_name column. Good schema names are crucial.</li><li><strong>Latency</strong>: “Thinking” takes time. The agent has to plan, generate code, execute, and synthesize. It’s slower than a keyword search but infinitely smarter.</li><li><strong>Imports</strong>: Python relative imports can be tricky when mixing script execution and app execution. Absolute imports saved the day.</li></ol><h3>Conclusion</h3><p>The future of Business Intelligence isn’t just dashboards; it’s <strong>interactive agents</strong>. By combining the precision of Databases with the flexibility of Semantic Search, we can unlock data that has been trapped in silos for decades.</p><p>This project is open source! You can check out the full code on <a href="https://github.com/pssgowtham/Agentic-SQL-RAG-for-Business-Data.git"><strong>GitHub</strong></a>.</p><p><strong>What would you build with an Agentic RAG system? Let me know in the comments.</strong></p><p><em>Tags: #GenerativeAI, #LlamaIndex, #RAG, #Python, #DataEngineering, #SQL</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bed8812e6fef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>