<?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 Adarishanmukh on Medium]]></title>
        <description><![CDATA[Stories by Adarishanmukh on Medium]]></description>
        <link>https://medium.com/@adarishanmukh15501?source=rss-fe524b420d71------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vTHeBouHoi4cmOV8</url>
            <title>Stories by Adarishanmukh on Medium</title>
            <link>https://medium.com/@adarishanmukh15501?source=rss-fe524b420d71------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 15:20:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adarishanmukh15501/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[Prompting Techniques with Gemini Models using the OpenAI SDK]]></title>
            <link>https://medium.com/@adarishanmukh15501/prompting-techniques-with-gemini-models-using-the-openai-sdk-a6006a9a86eb?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6006a9a86eb</guid>
            <category><![CDATA[prompting-technique]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[prompt]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Wed, 10 Sep 2025 02:28:23 GMT</pubDate>
            <atom:updated>2025-09-10T02:28:23.377Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everyone! 👋</p><p>In this blog, I’d like to walk you through how to use <strong>Gemini models</strong> via the <strong>OpenAI SDK</strong>, along with some practical <strong>prompting techniques</strong> that can help you get the most out of them.</p><p>Getting Started with OPENAI</p><pre>from openai import OpenAI<br>client = OpenAI()<br><br>response = client.responses.create(<br>    model=&quot;gpt-3.5-turbo&quot;,<br>    input=&quot;Write a one-sentence bedtime story about a unicorn.&quot;<br>)<br><br>print(response.output_text)</pre><p>Using Gemini</p><pre>from google import genai<br><br>client = genai.Client()<br><br>response = client.models.generate_content(<br>    model=&quot;gemini-2.0-flash&quot;,<br>    contents=&quot;Explain how AI works in a few words&quot;,<br>)<br><br>print(response.text)</pre><h4>Getting Started with Gemini via OpenAI SDK</h4><pre>from openai import OpenAI<br>from dotenv import load_dotenv<br>import os<br><br><br>client = OpenAI(<br>    api_key=os.getenv(&quot;GOOGLE_API_KEY&quot;),<br>    base_url=&quot;https://generativelanguage.googleapis.com/v1beta/openai/&quot;<br>)<br><br>response = client.chat.completions.create(<br>    model=&quot;gemini-2.0-flash&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;},<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;Explain to me how AI works&quot;<br>        }<br>    ]<br>)<br><br>print(response.choices[0].message.content)</pre><h4><strong>Zero Shot Prompting</strong></h4><p>Zero-shot prompting is a technique for large language models (LLMs) that involves providing a task or question in a prompt without any specific examples of the desired output. The model then relies on its extensive pre-trained knowledge and general understanding to infer the task and generate an appropriate response. This approach leverages the model’s ability to generalize to new, unseen problems, effectively showcasing its broad knowledge base without requiring task-specific fine-tuning or demonstration data.</p><pre>SYSTEM_PROMPT = &#39;&#39;&#39;You are a math expert. <br>                  Answer only math-related questions.<br>                  If the query is not related to mathematics,<br>                  respond with Sorry&#39;&#39;&#39;<br><br>response = client.chat.completions.create(<br>    model=&quot;gemini-2.5-flash&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: SYSTEM_PROMPT},<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;Explain to me how AI works&quot;<br>        }<br>    ]<br>)<br><br>print(response.choices[0].message.content)</pre><h4><strong>Few Shot Prompting</strong></h4><p>Few-shot prompting is a technique used to improve large language models (LLMs) performance by including a small number of examples within the prompt itself, demonstrating the desired task and format. This approach guides the model by providing context and examples for a specific task, helping it to generate more accurate and relevant outputs without requiring large amounts of labeled data or updating the model’s weights.</p><pre>SYSTEM_PROMPT_1 = &quot;&quot;&quot;<br>You are a math expert. Answer only math-related questions. If the query is not related to mathematics, respond with &#39;Sorry.&#39;<br><br>Examples:<br>Q: What is the derivative of x²?<br>A: The derivative of x² is 2x.<br><br>Q: Solve for x: 2x + 3 = 7<br>A: Subtract 3 from both sides: 2x = 4. Then divide by 2: x = 2.<br><br>Q: Who won the World Cup in 2022?<br>A: Sorry I am trained to answer only on Math-Related Questions<br><br>Q: What is the integral of sin(x)?<br>A: The integral of sin(x) is -cos(x) + C.<br><br>Q: Tell me a joke.<br>A: Sorry I am trained to answer only on Math-Related Questions<br><br>Now respond to the next query accordingly.<br>&quot;&quot;&quot;<br><br><br><br>response = client.chat.completions.create(<br>    model=&quot;gemini-2.5-flash&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: SYSTEM_PROMPT_1},<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;Explain to me how AI works&quot;<br>        }<br>    ]<br>)<br><br>print(response.choices[0].message.content)</pre><h4><strong>Chain of Thought</strong></h4><p>Chain of Thought (CoT) prompting is a technique in AI that guides models to reason step-by-step before giving an answer. Instead of jumping straight to a conclusion, the model is encouraged to “think aloud” — breaking down the problem into intermediate steps that lead to a final solution.</p><p>Why It Matters</p><p>- It improves accuracy, especially for complex tasks like math, logic, and commonsense reasoning.</p><p>- It makes the model’s reasoning transparent, so you can follow how it arrived at the answer.</p><p>- It mimics how humans solve problems — by working through them logically.</p><pre>SYSTEM_PROMPT_2 = &quot;&quot;&quot;<br>You are a math expert. Answer only math-related questions. If the query is not related to mathematics, respond with &#39;Sorry.&#39; For math questions, explain your reasoning step-by-step before giving the final answer.<br><br>Examples:<br>Q: What is the derivative of x²?<br>A: Let&#39;s think step-by-step.<br>The derivative of xⁿ with respect to x is n·xⁿ⁻¹.<br>Here, n = 2, so the derivative of x² is 2x.<br>Final answer: 2x.<br><br>Q: Solve for x: 2x + 3 = 7<br>A: Let&#39;s think step-by-step.<br>First, subtract 3 from both sides: 2x = 4.<br>Then divide both sides by 2: x = 2.<br>Final answer: x = 2.<br><br>Q: Who won the World Cup in 2022?<br>A: Sorry, I am trained to answer only math-related questions.<br><br>Q: What is the integral of sin(x)?<br>A: Let&#39;s think step-by-step.<br>The integral of sin(x) with respect to x is -cos(x), plus a constant of integration.<br>Final answer: -cos(x) + C.<br><br>Q: Tell me a joke.<br>A: Sorry, I am trained to answer only math-related questions.<br><br>Now respond to the next query accordingly.<br>&quot;&quot;&quot;<br><br>response = client.chat.completions.create(<br>    model=&quot;gemini-2.5-flash&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: SYSTEM_PROMPT_2},<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;Explain right angled traingle&quot;<br>        }<br>    ]<br>)<br><br>print(response.choices[0].message.content)</pre><h4><strong>Persona Based Prompting</strong></h4><p>Persona-Based Prompting is a technique where you assign a specific identity or role to an AI model to guide its behavior, tone, and expertise. This helps the model generate responses that are more relevant, consistent, and tailored to the task at hand.</p><pre>persona_prompt = &quot;&quot;&quot;<br>You are Dr. Maya Chen, a leading UX researcher with 15 years of experience in fintech applications.<br>Your expertise includes analyzing user behavior patterns and identifying usability issues in financial interfaces.<br>You communicate clearly using data-backed insights and avoid technical jargon.<br>Focus only on mobile banking application design challenges and do not provide general UX advice outside this domain.<br>&quot;&quot;&quot;<br>response = client.chat.completions.create(<br>    model=&quot;gemini-2.5-flash&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: persona_prompt},<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;Explain right angled traingle&quot;<br>        }<br>    ]<br>)<br><br>print(response.choices[0].message.content)</pre><h4><strong>ChatML Format</strong></h4><p>The ChatML Format refers to a structured way of formatting multi-turn conversations for training or interacting with language models. It’s commonly used in systems like OpenAI’s chat models and others that follow similar conventions.</p><pre>chat_ml_prompt = &quot;&quot;&quot;&lt;|im_start|&gt;system<br>You are a math expert. Answer only math-related questions. If the query is not related to mathematics, respond with &#39;Sorry.&#39; For math questions, explain your reasoning step-by-step before giving the final answer.<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;user<br>What is the derivative of x²?<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;assistant<br>Let&#39;s think step-by-step.<br>The derivative of xⁿ with respect to x is n·xⁿ⁻¹.<br>Here, n = 2, so the derivative of x² is 2x.<br>Final answer: 2x.<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;user<br>Solve for x: 2x + 3 = 7<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;assistant<br>Let&#39;s think step-by-step.<br>First, subtract 3 from both sides: 2x = 4.<br>Then divide both sides by 2: x = 2.<br>Final answer: x = 2.<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;user<br>Who won the World Cup in 2022?<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;assistant<br>Sorry, I am trained to answer only math-related questions.<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;user<br>What is the integral of sin(x)?<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;assistant<br>Let&#39;s think step-by-step.<br>The integral of sin(x) with respect to x is -cos(x), plus a constant of integration.<br>Final answer: -cos(x) + C.<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;user<br>Tell me a joke.<br>&lt;|im_end|&gt;<br>&lt;|im_start|&gt;assistant<br>Sorry, I am trained to answer only math-related questions.<br>&lt;|im_end|&gt;&quot;&quot;&quot;<br><br>response = client.chat.completions.create(<br>    model=&quot;gemini-2.5-flash&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: persona_prompt},<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;Explain right angled traingle&quot;<br>        }<br>    ]<br>)<br><br>print(response.choices[0].message.content)</pre><h4>Conclusion</h4><p>Gemini models are powerful, but <strong>the key to unlocking their potential lies in prompting</strong>. With the OpenAI SDK, experimenting is easier than ever. Try different techniques, mix them up, and see which one works best for your use case — whether that’s summarization, coding help, translation, or content creation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6006a9a86eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tools in LangGraph]]></title>
            <link>https://medium.com/@adarishanmukh15501/tools-in-langgraph-934db04694af?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/934db04694af</guid>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Sun, 31 Aug 2025 06:00:43 GMT</pubDate>
            <atom:updated>2025-08-31T06:00:43.192Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Tools</strong> are external functions or APIs that nodes can call.<br> They allow your graph to interact with the outside world (search engines, databases, calculators, APIs, etc.).</p><ul><li>Tools can be added to an LLM node so the LLM decides when to call them.</li></ul><p>So far, we’ve been able to <strong>create nodes, define edges, and build workflows</strong> in LangGraph. But there’s a limitation when we bind a node directly to an <strong>LLM</strong>:</p><ul><li>The LLM can only respond based on the <strong>knowledge it was trained on</strong>.</li><li>This means it <strong>cannot fetch real-time or domain-specific data</strong> by itself.</li></ul><p>👉 That’s where <strong>tools</strong> come in.</p><p>By <strong>binding tools to the LLM node</strong>, we extend its capabilities:</p><ul><li>The LLM can decide <strong>when</strong> and <strong>which tool</strong> to call.</li><li>Tools can connect to <strong>APIs, databases, vector stores, or calculators</strong>.</li><li>This allows the LLM to <strong>augment its static knowledge</strong> with <strong>live, up-to-date information</strong>.</li></ul><h3><strong>ToolNode</strong></h3><p>In <strong>LangGraph</strong>, a <strong>ToolNode</strong> is a special type of node that allows your LLM agent (or workflow) to <strong>call external tools</strong> during execution. its a collections of tools that binds with the llm.</p><h4>Problem Statement</h4><p>We will build a chatbot integrated with one predefined tool and two custom tools. The chatbot is powered by an LLM that dynamically decides which tool to invoke based on the user’s query. Once a tool provides its response, the LLM incorporates that information to generate a more accurate and contextually rich final answer.”</p><blockquote>IMPORTS</blockquote><pre>from typing import TypedDict, Optional<br>from langchain_openai import ChatOpenAI<br>from langgraph.prebuilt import ToolNode, tools_condition<br>from langchain_community.tools import DuckDuckGoSearchRun<br>from langchain.agents import tool<br>import requests<br>from IPython.display import Image, display<br>from langchain.schema import BaseMessage, HumanMessage<br>from langgraph.graph import StateGraph, START, END, add_messages<br>from typing import TypedDict, Annotated</pre><blockquote>SETUP STATE VARIABLES</blockquote><pre># 1. Define state using TypedDict<br><br>class GraphStateData(TypedDict):<br>    messages: Annotated[list[BaseMessage], add_messages]<br><br>llm = ChatOpenAI()</pre><blockquote>Pre-defined tool</blockquote><pre>search_tool = DuckDuckGoSearchRun()</pre><blockquote>Custom-Tools</blockquote><pre># Custom Tools<br><br>@tool<br>def get_weather_tool(city:str)-&gt;str:<br>    &#39;&#39;&#39;this tool is used for getting the weather information&#39;&#39;&#39;<br>    if &quot;delhi&quot; in city.lower():<br>        return &quot;Delhi is very hot  52c&quot;<br>    else:<br>        return f&quot;{city} is very cold 32 degree celcius&quot;<br><br># Example usage<br><br>print(type(get_weather_tool)) <br>print((get_weather_tool(&quot;what is the weather in delhi&quot;))) <br><br><br><br><br>@tool<br>def get_pokemon_info(pokemon: str) -&gt; str:<br>    &#39;&#39;&#39;This tool fetches information about a Pokémon from the PokéAPI.&#39;&#39;&#39;<br>    url = f&quot;https://pokeapi.co/api/v2/pokemon/{pokemon.lower()}&quot;<br>    response = requests.get(url)<br>    <br>    if response.status_code == 200:<br>        data = response.json()<br>        name = data[&#39;name&#39;]<br>        height = data[&#39;height&#39;]<br>        weight = data[&#39;weight&#39;]<br>        types = [t[&#39;type&#39;][&#39;name&#39;] for t in data[&#39;types&#39;]]<br>        return f&quot;Name: {name}, Height: {height}, Weight: {weight}, Types: {&#39;, &#39;.join(types)}&quot;<br>    else:<br>        return f&quot;Error: Pokémon &#39;{pokemon}&#39; not found.&quot;<br><br><br># Example usage<br><br>print(type(get_pokemon_info))<br>print(get_pokemon_info(&quot;ditto&quot;))<br><br>tools = [get_weather_tool,search_tool,get_pokemon_info]<br><br></pre><blockquote>Bind existing tools to LLM and bind all tools in toolbox</blockquote><pre>llm_with_tools =  llm.bind_tools(tools)<br><br>def chatbot(state: MessagesState) -&gt; dict:<br>    response = llm_with_tools.invoke(state[&quot;messages&quot;])<br>    return {<br>        &quot;messages&quot;: [response]<br>    }<br><br>tool_node = ToolNode(tools=tools)<br></pre><blockquote>Define the WorkFlow</blockquote><pre>workflow = StateGraph(GraphStateData)<br><br>workflow.add_edge(START, &quot;chatbot&quot;)<br><br>workflow.add_node(&quot;chatbot&quot;, chatbot)<br><br>workflow.set_entry_point(&quot;chatbot&quot;)<br><br>workflow.add_node(&quot;tools&quot;, tool_node)<br><br>workflow.add_conditional_edges(<br>    &quot;chatbot&quot;,<br>    tools_condition,<br>)<br><br>workflow.add_edge(&quot;tools&quot;, &quot;chatbot&quot;)<br><br>graph = workflow.compile()<br><br>result = graph.invoke({<br>    &quot;messages&quot;: [HumanMessage(content=&quot;Tell the weather of delhi&quot;)]<br>})<br><br>result[&#39;messages&#39;][-1].content</pre><blockquote>Output:- ‘The weather in Delhi is currently very hot at 52°C.’</blockquote><pre>display(Image(graph.get_graph().draw_mermaid_png()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*m0SW9dEoy-TlB2H8oGOWYA.png" /></figure><h4>Conclusion</h4><p>In LangGraph, StateGraph defines the workflow, MessagesState is ideal for chat-style memory, and END cleanly marks completion. Choosing the right state structure ensures smoother, more reliable LLM workflows.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=934db04694af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MULTIMODAL RAG]]></title>
            <link>https://medium.com/@adarishanmukh15501/multimodal-rag-389b8a829be7?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/389b8a829be7</guid>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[multimodal-ai]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Sat, 30 Aug 2025 03:45:43 GMT</pubDate>
            <atom:updated>2025-08-30T03:45:43.001Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Multimodal RAG (Retrieval-Augmented Generation)</strong> is an advanced AI architecture that combines:</p><ul><li><strong>Multimodal inputs</strong> (like text, images, audio, video)</li><li><strong>Retrieval mechanisms</strong> (searching external knowledge bases)</li><li><strong>Generation models</strong> (LLMs that produce responses)</li></ul><p>It enhances the LLM’s capabilities by giving it access to <strong>external, relevant, and often up-to-date multimodal information</strong> before generating a response.</p><p><strong>Three Approaches to Multimodal Retrieval-Augmented Generation (RAG)</strong></p><h3>Option 1: Use Multimodal Embeddings for Both Text and Images</h3><ul><li>Use a model like <strong>CLIP</strong> that understands both <strong>text</strong> and <strong>images</strong>.</li><li>It converts both into the <strong>same vector space</strong>.</li><li>You store the image and text <strong>embeddings</strong> in a <strong>vector database</strong>.</li><li>That means: an image of a “dog playing football” and the text “dog playing football” will map to <strong>close vectors</strong> in the DB.</li><li>You store all vectors (both image embeddings + text embeddings) in your vector DB.</li></ul><p>When a question comes in:</p><ul><li>You <strong>retrieve similar images or text</strong> based on the query.</li><li>You <strong>send both</strong> the raw images and text to a <strong>multimodal LLM</strong> like <strong>GPT-4V</strong> to generate the final answer.</li></ul><h3>Workflow:</h3><ol><li><strong>Ingestion time (building your DB):</strong></li></ol><ul><li>Take every document (text) → embed with text encoder of CLIP.</li><li>Take every image → embed with image encoder of CLIP.</li><li>Store them together in DB.</li></ul><p><strong>Query time:</strong></p><p>User asks: <em>“Show me sales charts for 2024”</em>.</p><ul><li>Convert this query into a <strong>vector</strong> using CLIP (text encoder).</li><li>Retrieve both <strong>images + text</strong> that are semantically close.</li><li>Send both to a <strong>multimodal LLM</strong> (like GPT-4V) → It can read both text and raw images → generate the answer.</li></ul><p>🧠 <strong>Example</strong>:</p><ul><li>Query: <em>“Find the graph that shows 2024 sales growth.”</em></li><li>Retrieval: CLIP finds both a chart image + text about sales growth 2024.</li><li>Answering: GPT-4V looks at the chart and writes:<br> <em>“This chart shows a 25% increase in Q1 and steady growth through 2024.”</em></li></ul><h3>Option 2: Convert Images to Text First, Then Use Text Embeddings</h3><ul><li>Use a <strong>multimodal LLM</strong> (like GPT-4V or LLaVA) to <strong>look at each image</strong> and create a <strong>text summary</strong> or description.</li><li>Store these text summaries (along with original text chunks) in a <strong>text-based vector DB</strong> using <strong>text embeddings</strong>.</li></ul><p>At question time:</p><ul><li>Here, instead of storing images as embeddings, you <strong>convert each image into text (captions, summaries, tables)</strong>.</li><li>Then you store <strong>only text</strong> in a text-based vector DB (like FAISS/Chroma with OpenAI text embeddings).</li><li>During retrieval, you never use the raw image again.</li></ul><h3>Workflow:</h3><ol><li><strong>Ingestion time:</strong></li></ol><ul><li>Each image → send to GPT-4V (or LLaVA, BLIP, etc.) → generate a <strong>text summary</strong>.<br> Example: <em>“Image shows sales growth chart for 2024 with upward trend.”</em></li><li>Store this summary in vector DB alongside your other text.</li></ul><p><strong>Query time:</strong></p><ul><li>User asks: <em>“What were the 2024 sales trends?”</em></li><li>Use <strong>text embeddings</strong> to retrieve summaries + documents</li><li>Only text is retrieved (no images).</li><li>Send this text to a <strong>normal LLM</strong> (no vision required) → generate the final answer.</li></ul><p>✅ <strong>Best when</strong>:</p><ul><li>You <strong>can’t afford multimodal LLM calls</strong> (too expensive).</li><li>Your system is <strong>text-only at inference</strong> (faster, cheaper).</li><li>The <strong>image content can be captured in text</strong> without much loss.</li></ul><p>🧠 <strong>Example</strong>:</p><ul><li>Query: <em>“What does the 2024 sales chart show?”</em></li><li>Retrieval: The summary text <em>“Image shows sales growth chart for 2024 with upward trend.”</em></li><li>Answer: LLM says:<br> <em>“Sales increased steadily throughout 2024, showing consistent growth.”</em></li></ul><h4>Option 3: Convert Images to Text for Retrieval, But Use Raw Images at Answer Time</h4><ul><li>Like Option 2, you <strong>summarize the image</strong> into text.</li><li>You store these summaries (text) in a vector DB.</li><li>But you also <strong>keep the original image</strong> and use it during answer generation.</li><li>You <strong>don’t use multimodal embeddings</strong> for search — just text summaries.</li></ul><h3>Workflow:</h3><ol><li><strong>Ingestion time:</strong></li></ol><ul><li>Convert each image into a <strong>summary</strong> (like in Option 2).</li><li>Store <strong>summaries + text</strong> in vector DB.</li><li>Keep original image files stored somewhere (not in DB, but linked).</li></ul><p><strong>Query time:</strong></p><ul><li>User asks: <em>“Show me 2024 sales charts.”</em></li><li>Retrieve text summaries → find relevant docs.</li><li>Then: fetch the actual <strong>image file(s)</strong> linked to those summaries.</li><li>Send <strong>both summary + original image + other text</strong> to a <strong>multimodal LLM</strong> like GPT-4V.</li><li>Answer is generated using both.</li></ul><p>✅ <strong>Best for</strong>:</p><ul><li>You <strong>don’t want multimodal embeddings</strong> (cheaper, simpler DB).</li><li>But you <strong>still want the LLM to see the raw image</strong> at the end (for accuracy).</li></ul><p>🧠 <strong>Example</strong>:</p><ul><li>Query: <em>“What does the 2024 sales chart say?”</em></li><li>Retrieval: Finds summary <em>“Image shows sales growth chart for 2024.”</em></li><li>Fetch actual chart image.</li><li>Answering: GPT-4V looks at the real chart and says:<br> <em>“This chart shows a 25% increase in sales from Q1 to Q2, then stable growth through Q4.”</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*qwsqqNfh81J58CNl-nvtuA.png" /></figure><h3>✅ Types of Data by Structure</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*TPNQS0acmeTRq0i4ApLVCQ.png" /></figure><h3>Conclusion</h3><p>Multimodal RAG can be designed in different ways depending on your goals, cost constraints, and infrastructure.</p><ul><li><strong>Option 1 (multimodal embeddings)</strong> gives you the most seamless search across both text and images, but it requires a multimodal model throughout the pipeline.</li><li><strong>Option 2 (images → text only)</strong> is the simplest and cheapest approach, but it sacrifices the ability to directly reason over the original visuals.</li><li><strong>Option 3 (images → text for retrieval + raw images at answer time)</strong> strikes a practical balance: you get the speed and efficiency of text-based retrieval while still allowing the LLM to inspect the real images during answer generation.</li></ul><p>For many real-world use cases — like enterprise reports, charts, scientific papers, and presentations — <strong>Option 3 offers the best trade-off</strong>. It keeps your vector database lightweight, reduces retrieval costs, and still ensures high-quality answers by letting the LLM validate details directly from the raw images.</p><p>In the end, the right approach depends on whether you prioritize <strong>efficiency, accuracy, or flexibility</strong>. But understanding these three patterns gives you a clear toolbox for building multimodal RAG systems that fit your needs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=389b8a829be7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LANGRAPH WORKFLOW — 3]]></title>
            <link>https://medium.com/@adarishanmukh15501/langraph-workflow-3-4d435c743179?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d435c743179</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[langraph]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Mon, 18 Aug 2025 04:00:07 GMT</pubDate>
            <atom:updated>2025-08-18T04:00:07.325Z</atom:updated>
            <content:encoded><![CDATA[<h3>LANGRAPH WORKFLOW — 3</h3><h3>What is Parallelism in LangGraph?</h3><p>In LangGraph, <strong>parallelism</strong> means running multiple nodes (steps/agents/tools) <strong>at the same time</strong> instead of one after another.<br> This is useful when different tasks are <strong>independent</strong> and don’t need to wait for each other.</p><pre>from typing import TypedDict<br>from langgraph.graph import StateGraph<br>from langchain.chat_models import ChatOpenAI<br><br># -----------------------------<br># Shared state<br># -----------------------------<br>class GraphState(TypedDict):<br>    query: str<br>    two_multiplier: int<br>    three_multiplier: int<br>    four_multiplier: int<br>    final_result: int<br><br># LLM setup<br>llm = ChatOpenAI(model=&quot;gpt-4o-mini&quot;, temperature=0)<br><br># -----------------------------<br># Entry node<br># -----------------------------<br>def query_input(state: GraphState):<br>    # Just pass the query along to the next nodes<br>    return {&quot;query&quot;: state[&quot;query&quot;]}<br><br># -----------------------------<br># Agent nodes<br># -----------------------------<br>def number2_multiplier(state: GraphState):<br>    resp = llm.predict(f&quot;Take the number {state[&#39;query&#39;]} and multiply it by 2. Only return the number.&quot;)<br>    return {&quot;two_multiplier&quot;: int(resp.strip())}<br><br>def number3_multiplier(state: GraphState):<br>    resp = llm.predict(f&quot;Take the number {state[&#39;query&#39;]} and multiply it by 3. Only return the number.&quot;)<br>    return {&quot;three_multiplier&quot;: int(resp.strip())}<br><br>def number4_multiplier(state: GraphState):<br>    resp = llm.predict(f&quot;Take the number {state[&#39;query&#39;]} and multiply it by 4. Only return the number.&quot;)<br>    return {&quot;four_multiplier&quot;: int(resp.strip())}<br><br>def final_total(state: GraphState):<br>    total = state[&quot;two_multiplier&quot;] + state[&quot;three_multiplier&quot;] + state[&quot;four_multiplier&quot;]<br>    return {&quot;final_result&quot;: total}<br><br># -----------------------------<br># Build graph<br># -----------------------------<br>graph = StateGraph(GraphState)<br><br>graph.add_node(&quot;query_input&quot;, query_input)<br>graph.add_node(&quot;number2_multiplier&quot;, number2_multiplier)<br>graph.add_node(&quot;number3_multiplier&quot;, number3_multiplier)<br>graph.add_node(&quot;number4_multiplier&quot;, number4_multiplier)<br>graph.add_node(&quot;final_total&quot;, final_total)<br><br># Fan out from query → multipliers<br>graph.add_edge(&quot;query_input&quot;, &quot;number2_multiplier&quot;)<br>graph.add_edge(&quot;query_input&quot;, &quot;number3_multiplier&quot;)<br>graph.add_edge(&quot;query_input&quot;, &quot;number4_multiplier&quot;)<br><br># Merge multipliers → final<br>graph.add_edge([&quot;number2_multiplier&quot;, &quot;number3_multiplier&quot;, &quot;number4_multiplier&quot;], &quot;final_total&quot;)<br><br># Define entry/finish<br>graph.set_entry_point(&quot;query_input&quot;)<br>graph.set_finish_point(&quot;final_total&quot;)<br><br># -----------------------------<br># Compile and Run<br># -----------------------------<br>workflow = graph.compile()<br><br>result = workflow.invoke({&quot;query&quot;: &quot;5&quot;})<br>print(&#39;result&#39;,result)<br><br>------------------------------output-----------------------------------------<br>result {&#39;query&#39;: &#39;5&#39;, &#39;two_multiplier&#39;: 10, &#39;three_multiplier&#39;: 15, &#39;four_multiplier&#39;: 20, &#39;final_result&#39;: 45}</pre><pre>from IPython.display import Image, display<br>app = graph.compile()<br>display(Image(app.get_graph().draw_mermaid_png()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*e6-Ugv55e94HASxdui3BwA.png" /></figure><h3>Conclusion</h3><p>In this workflow, we demonstrated how <strong>LangGraph enables parallel execution</strong> of independent tasks and then merges their results into a final output. By setting up a dedicated <strong>entry node</strong> to capture the query, we fanned it out to three multiplier nodes (×2, ×3, ×4) which executed <strong>concurrently</strong>. Their results were then collected in a final_total node that aggregated the outputs into a single value.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d435c743179" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LANGRAPH WorkFlow —2]]></title>
            <link>https://medium.com/@adarishanmukh15501/langraph-c61c8fcaac8f?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/c61c8fcaac8f</guid>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Tue, 12 Aug 2025 01:45:38 GMT</pubDate>
            <atom:updated>2025-08-18T05:00:45.053Z</atom:updated>
            <content:encoded><![CDATA[<h3>Langraph Conditional WorkFlow</h3><h3>LANGRAPH WORKFLOW — 2</h3><h3>Routing Workflow</h3><p>In Our previous post we have understood about prompt chaining workflow where the sequential flow is followed now, In this flow lets decide</p><p>This is a <strong>routing workflow</strong> — specifically an <strong>LLM-based conditional routing workflow</strong> in LangGraph, based on the query provided llm decides which node to call.</p><h3>Workflow Overview</h3><p>The goal of this workflow is to take a <strong>user query</strong>, let the <strong>LLM decide the category</strong>, and then send it to the <strong>appropriate agent node</strong>.</p><h3>Step-by-Step Flow</h3><ul><li><strong>User Input Arrives</strong><br> The workflow starts when a user sends a query like: “My internet bill has extra charges”</li><li>The query is sent to a <strong>router node</strong>.</li><li>The router uses a <strong>ChatOpenAI model</strong> with a short classification prompt:</li><li>Categories: billing, tech, general</li><li>LLM must return <strong>only</strong> the category name.</li><li>Example LLM output: &quot;billing&quot;</li></ul><ol><li><strong>State Update</strong></li></ol><ul><li>The category value is stored in the <strong>graph state</strong> (GraphState).</li><li>This state is shared across all nodes.</li></ul><ol><li><strong>Conditional Routing</strong></li></ol><ul><li>LangGraph’s add_conditional_edges looks at state[&quot;category&quot;].</li></ul><p>Based on the category:</p><ul><li>&quot;billing&quot; → goes to <strong>Billing Agent node</strong></li><li>&quot;tech&quot; → goes to <strong>Tech Agent node</strong></li><li>&quot;general&quot; → goes to <strong>General Agent node</strong></li></ul><pre>from typing import TypedDict, Literal<br>from langgraph.graph import StateGraph<br>from langchain.chat_models import ChatOpenAI<br>from langchain.prompts import ChatPromptTemplate<br><br># Shared state<br>class GraphState(TypedDict):<br>    query: str<br>    category: Literal[&quot;billing&quot;, &quot;tech&quot;, &quot;general&quot;]<br><br># LLM router setup<br>llm = ChatOpenAI(model=&quot;gpt-4o-mini&quot;, temperature=0)<br>router_prompt = ChatPromptTemplate.from_template(&quot;&quot;&quot;<br>Classify the customer query into one category:<br>- billing<br>- tech<br>- general<br>Return ONLY the category name.<br><br>Query: {query}<br>&quot;&quot;&quot;)<br><br>def route_with_llm(state: GraphState):<br>    response = llm.invoke(router_prompt.format_messages(query=state[&quot;query&quot;]))<br>    state[&quot;category&quot;] = response.content.strip().lower()<br>    return state<br><br># Agent nodes<br>def billing_agent(state: GraphState):<br>    print(&quot;💰 Billing issue:&quot;, state[&quot;query&quot;])<br>    return state<br><br>def tech_agent(state: GraphState):<br>    print(&quot;🛠 Tech issue:&quot;, state[&quot;query&quot;])<br>    return state<br><br>def general_agent(state: GraphState):<br>    print(&quot;📌 General query:&quot;, state[&quot;query&quot;])<br>    return state<br><br># Build graph<br>graph = StateGraph(GraphState)<br>graph.add_node(&quot;router&quot;, route_with_llm)<br>graph.add_node(&quot;billing&quot;, billing_agent)<br>graph.add_node(&quot;tech&quot;, tech_agent)<br>graph.add_node(&quot;general&quot;, general_agent)<br><br>graph.add_conditional_edges(<br>    &quot;router&quot;,<br>    lambda state: state[&quot;category&quot;],<br>    {<br>        &quot;billing&quot;: &quot;billing&quot;,<br>        &quot;tech&quot;: &quot;tech&quot;,<br>        &quot;general&quot;: &quot;general&quot;,<br>    }<br>)<br><br>graph.set_entry_point(&quot;router&quot;)<br>app = graph.compile()<br><br># Test<br>app.invoke({&quot;query&quot;: &quot;My internet bill has extra charges&quot;})<br><br>----------------------------------------------------------------------------<br>{&#39;query&#39;: &#39;My internet bill has extra charges&#39;, &#39;category&#39;: &#39;billing&#39;}</pre><pre>from IPython.display import Image, display<br><br>display(Image(app.get_graph().draw_mermaid_png()))<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*xJHHKEVw7B5ljqTTfjNE0w.png" /></figure><h3>Conclusion</h3><p>By implementing an <strong>LLM-powered routing workflow</strong> in LangGraph, we move beyond rigid keyword checks and into a more intelligent, context-aware system. The LLM acts as a decision-maker, understanding the user’s intent and directing the query to the right specialized node.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c61c8fcaac8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LANGRAPH WORKFLOW — 1]]></title>
            <link>https://medium.com/@adarishanmukh15501/langraph-part-2-708f6325ca75?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/708f6325ca75</guid>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[workflow]]></category>
            <category><![CDATA[prompt-engineering]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Wed, 06 Aug 2025 04:25:13 GMT</pubDate>
            <atom:updated>2025-08-12T01:48:08.392Z</atom:updated>
            <content:encoded><![CDATA[<h3>Prompt Chaining Workflow</h3><ul><li>In our previous post we have learned how to build a workflow with a simple graph with no state management.</li></ul><p>Problem Statement: Lets build a state graph where the data is shared across the nodes with the prompt chaining workflow pattern.</p><h3>📘 What is a State Graph?</h3><p>A <strong>state graph</strong> in LangGraph represents a <strong>directed graph</strong> where:</p><ul><li><strong>Nodes</strong> are computation steps (e.g., an LLM call, tool use, agent action).</li><li><strong>Edges</strong> (or transitions) define how you move from one node to the next based on outputs.</li><li>It maintains <strong>state</strong> across nodes, allowing memory and variables to evolve with the conversation</li></ul><p>🚦 What is Prompt Chaining?</p><p>Prompt chaining is:</p><blockquote><em>Breaking a complex task into a sequence of smaller prompts, each handled by different components (nodes) in a LangGraph , </em>where the <strong>output of one step</strong> becomes the <strong>input to the next</strong> — just like in traditional function chaining or pipeline stages.</blockquote><h3>A Simple Prompt Chain</h3><p>Suppose you want to:</p><ol><li><strong>Summarize a document</strong></li><li><strong>Translate the summary to Hindi</strong></li><li><strong>Generate a heading from the Hindi summary</strong></li></ol><p>Imports</p><pre>from typing import TypedDict, Optional<br>from langgraph.graph import StateGraph<br>from langchain.chat_models import ChatOpenAI</pre><p>Define State Variables</p><pre>class GraphStateData(TypedDict):<br>    input: str<br>    summary: Optional[str]<br>    hindi_summary: Optional[str]<br>    heading: Optional[str]</pre><p>Define LLM</p><pre>llm = ChatOpenAI()</pre><p>Define Nodes &amp; Build a State Graph</p><pre><br>def summarize(state: GraphStateData) -&gt; dict:<br>    summary = llm.predict(f&quot;Summarize this:\n\n{state[&#39;input&#39;]}&quot;)<br>    return {&quot;summary&quot;: summary}<br><br>def translate(state: GraphStateData) -&gt; dict:<br>    hindi = llm.predict(f&quot;Translate to Hindi:\n\n{state[&#39;summary&#39;]}&quot;)<br>    return {&quot;hindi_summary&quot;: hindi}<br><br>def generate_heading(state: GraphStateData) -&gt; dict:  <br>    heading = llm.predict(f&quot;Generate a heading for:\n\n{state[&#39;hindi_summary&#39;]}&quot;)<br>    return {&quot;heading&quot;: heading}<br><br># 3. Build graph<br>graph_builder = StateGraph(GraphStateData)<br>graph_builder.add_node(&quot;summarize&quot;, summarize)<br>graph_builder.add_node(&quot;translate&quot;, translate)<br>graph_builder.add_node(&quot;generate_heading&quot;, generate_heading)  <br>graph_builder.set_entry_point(&quot;summarize&quot;)<br>graph_builder.add_edge(&quot;summarize&quot;, &quot;translate&quot;)<br>graph_builder.add_edge(&quot;translate&quot;, &quot;generate_heading&quot;)  <br>graph_builder.set_finish_point(&quot;generate_heading&quot;)    <br><br>graph = graph_builder.compile()<br><br># 4. Run the graph<br>result = graph.invoke({&quot;input&quot;: &quot;Tell about Modi&quot;})<br>print(result[&quot;heading&quot;]) <br><br>----------------------------------output--------------------------------------<br>नरेंद्र मोदी: भारत का सक्रिय प्रधानमंत्री</pre><p>Display the graph</p><pre>from IPython.display import Image, display<br><br>display(Image(graph.get_graph().draw_mermaid_png()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*dYfpNA6QsGq8Pr3WJGPzdw.png" /></figure><p><strong>Conclusion</strong></p><p>Prompt chaining in LangGraph helps break down complex tasks into smaller steps that are easier to manage. Each step runs in its own node, and the output from one step becomes the input for the next. This makes workflows more flexible and easier to build, especially when using large language models. LangGraph is a powerful tool for creating smart, multi-step AI applications in a clean and organized way.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=708f6325ca75" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LANGRAPH NOTES]]></title>
            <link>https://medium.com/@adarishanmukh15501/langraph-part-1-86ec0154c16a?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/86ec0154c16a</guid>
            <category><![CDATA[begineers]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Sun, 20 Jul 2025 15:17:32 GMT</pubDate>
            <atom:updated>2025-08-12T01:47:16.287Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>LangGraph</strong> is a library developed by LangChain that is specifically designed to support <strong>agentic workflows</strong> and <strong>multi-agent systems</strong> using a <strong>graph-based architecture</strong>.</p><p>It allows you to define <strong>flexible, multi-step, branching workflows</strong></p><p><strong><em>“LangGraph is an orchestration tool that supports agentic workflows.”</em></strong></p><h3>🤖 What is an Agentic Workflow?</h3><p>An <strong>agentic workflow</strong> is a system where an <strong>AI agent</strong> (powered by an LLM) is:</p><ul><li><strong>Autonomous</strong>: It can decide what to do next.</li><li><strong>Tool-using</strong>: It can call functions/tools/APIs when needed.</li><li><strong>Stateful</strong>: It remembers and updates the state or context.</li><li><strong>Iterative</strong>: It can loop, retry, or branch based on conditions.</li></ul><p>In short:</p><blockquote><strong><em>An agentic workflow is a dynamic, decision-driven process where an AI agent takes action, learns from feedback/state, and controls its next step.</em></strong></blockquote><h3>📌 1. Graph</h3><p>A <strong>Graph</strong> is the overall structure representing your flow of operations.</p><ul><li>Think of it like a <strong>roadmap</strong> of your LLM or multi-agent pipeline.</li><li>It’s composed of <strong>nodes</strong> (tasks/steps) and <strong>edges</strong> (paths between tasks).</li></ul><p>LangGraph supports <strong>directed graphs</strong>, where each connection has a direction.</p><h3>🔄 Cyclic vs. Acyclic Graphs in LangGraph</h3><h3>1. ✅ Acyclic Graphs (DAG — Directed Acyclic Graph)</h3><ul><li><strong>No loops</strong>: Once a node is visited, it won’t be revisited.</li><li><strong>Linear or branching flow</strong>, but always moves forward.</li></ul><p>Common in:</p><ul><li>Data pipelines</li><li>Static multi-step LLM chains</li><li>RAG pipelines with fixed steps</li></ul><p><strong>Example:</strong></p><pre>start → fetch → generate → respond</pre><h3>🔁 Cyclic Graphs</h3><ul><li><strong>Contain loops</strong>: Nodes can revisit earlier nodes.</li><li>Allows <strong>iterations</strong>, <strong>feedback loops</strong>, <strong>retry logic</strong>, or <strong>re-planning</strong>.</li></ul><p>Crucial for:</p><ul><li><strong>Agentic workflows</strong></li><li><strong>Multi-agent communication</strong></li><li><strong>Retry on failure</strong></li><li><strong>“Think → Act → Observe → Think again”</strong> loops</li></ul><pre>→ planner<br> ↓ ↑<br> executor ← observer</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*nkmODoQQo8_fCcDFOwV5Rw.png" /><figcaption>cyclic vs acyclic</figcaption></figure><h3>📌 2. Node</h3><p>A <strong>Node</strong> is a <strong>unit of execution</strong> — a step in your workflow.</p><p>Each node can be:</p><ul><li>A prompt to an LLM</li><li>A call to a function/tool</li><li>An agent</li><li>A conditional handler, etc.</li></ul><h3>📌 3. Edge</h3><p>An <strong>Edge</strong> defines <strong>what node comes next</strong>.</p><ul><li>It connects two nodes.</li><li>You can make this <strong>conditional</strong> based on the state.</li></ul><h4>Example — 1</h4><h4>✅ 1. Define the Functions (Nodes)</h4><pre>def function1(input1):<br>    return input1 + &quot; from first function&quot;<br><br>def function2(input2):<br>    return input2 + &quot; Adari from second function&quot;</pre><h4>✅ 2. Create the Graph</h4><pre>from langgraph.graph import Graph<br>workflow = Graph()</pre><h4>✅ 3. Add Nodes</h4><pre>workflow.add_node(&quot;fun1&quot;, function1)<br>workflow.add_node(&quot;fun2&quot;, function2)</pre><ul><li>&quot;fun1&quot; maps to function1</li><li>&quot;fun2&quot; maps to function2</li></ul><h4>✅ 4. Define the Flow (Edge)</h4><pre>workflow.add_edge(&quot;fun1&quot;, &quot;fun2&quot;)</pre><p>This means:</p><ul><li>After &quot;fun1&quot; runs, LangGraph will automatically run &quot;fun2&quot; using the output of &quot;fun1&quot; as input to &quot;fun2&quot;.</li></ul><p>So the output flows like:<br> input → fun1 → fun2 → result</p><h4>✅ 5. Set Entry and Finish Points</h4><pre>workflow.set_entry_point(&quot;fun1&quot;)<br>workflow.set_finish_point(&quot;fun2&quot;)</pre><h4>✅ 6. Compile the Graph</h4><pre>app = workflow.compile()</pre><h4>✅ 7. Visualize the Graph (Optional)</h4><pre>from IPython.display import Image, display<br>display(Image(app.get_graph().draw_mermaid_png()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/187/1*Zh5294H-oDmZq9EJ2VJ0kw.png" /></figure><pre>app.invoke(&quot;hi this is shanmukh&quot;)<br><br>----------------------------------------output-------------------------------------------------<br>&#39;hi this is shanmukh from first function Adari from second function&#39;</pre><h4>Example — 2</h4><h3>Building a Simple LangGraph Workflow: LLM + Token Counter</h3><h3>✅ Step 1: Initialize the LLM</h3><pre>from langchain_openai import ChatOpenAI<br>import os<br>llm = ChatOpenAI(<br>    openai_api_key=os.getenv(&quot;OPENAI_API_KEY&quot;, &quot;&quot;),<br>    temperature=0,<br>    model=&quot;gpt-4&quot;,<br>    streaming=True,<br>    verbose=True,<br>)</pre><h3>🧠 Step 2: Define Functions</h3><pre>def call_llm(input):<br>    output=llm.invoke(input)<br>    return output.content<br><br>def token_counter(input):<br>    token=input.split()<br>    token_number=len(token)<br>    return f&quot;total token number in the generated answer is {token_number}&quot;</pre><h3>🧠 Step 3: Define Workflow</h3><pre>workflow2=Graph()</pre><h3>🧠 Step 4: Define Nodes</h3><pre>workflow2.add_node(&quot;My_LLM&quot;,call_llm)<br>workflow2.add_node(&quot;LLM_Output_Token_Counter&quot;,token_counter)</pre><h3>🔁 Step 5: Define the Workflow Graph</h3><pre>workflow2.add_edge(&quot;My_LLM&quot;,&quot;LLM_Output_Token_Counter&quot;)<br><br>workflow2.set_entry_point(&quot;My_LLM&quot;)<br>workflow2.set_finish_point(&quot;LLM_Output_Token_Counter&quot;)<br><br>app1 = workflow2.compile()<br><br>display(Image(app.get_graph().draw_mermaid_png()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*ojibpJcCR5rIffZ756Pjhw.png" /></figure><pre>result = app1.invoke(&quot;Tell me about Australia&quot;)<br>print(result)<br>-----------------------------------------------------------------------------<br>total token number in the generated answer is 256</pre><h3>Conclusion</h3><p>With just a few lines of code, we created a simple LangGraph workflow — a directed acyclic graph (DAG) that chains an LLM with a token counter. This structure makes it easy to build and workflows with full control over each step.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86ec0154c16a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tools in LangChain]]></title>
            <link>https://medium.com/@adarishanmukh15501/tools-in-langchain-cd77d00f126f?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd77d00f126f</guid>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[ai-tools]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Sun, 29 Jun 2025 09:45:11 GMT</pubDate>
            <atom:updated>2025-07-20T10:22:59.255Z</atom:updated>
            <content:encoded><![CDATA[<p>In <strong>LangChain</strong>, <strong>tools</strong> are like <strong>skills or abilities</strong> that an AI agent can use to perform specific tasks. Think of them as plugins or functions that the agent can call to get something done.</p><p>A <strong>tool in LangChain</strong> is essentially just a <strong>Python function</strong>, but it’s <strong>wrapped in a special way</strong> so that an LLM can:</p><h4>Why Do We Need Tools?</h4><p>Large Language Models (LLMs) like GPT can generate text, but they <strong>don’t have real-world access</strong> by default. Tools allow the model to:</p><ul><li>🔍 <strong>Search the web</strong></li><li>📦 <strong>Query a database</strong></li><li>📊 <strong>Call an API</strong></li><li>🧮 <strong>Do calculations</strong></li><li>📁 <strong>Look into documents or files</strong></li></ul><p><strong>Example -1</strong></p><p>🤔 <strong>How does ChatGPT answer 2025 questions if it was trained on 2023 data?</strong></p><p>The secret lies in the <strong>tools</strong> integrated into modern LLM platforms like ChatGPT.</p><p>While the model itself doesn’t “know” anything beyond its training cutoff, it can use real-time tools such as:</p><ul><li>🌐 <strong>Web browsing</strong> — to fetch up-to-date information from the internet.</li><li>📁 <strong>File reader</strong> — to process and understand documents or PDFs uploaded by the user.</li></ul><p>These tools act like <strong>live extensions</strong> to the model, allowing it to go beyond static memory and interact with the world dynamically.</p><p>✅ So when you ask about 2025 events, it’s not the model “remembering” — it’s <strong>actively looking it up</strong> using its tools (if enabled).</p><p>Simple — it’s not just a model, it’s a <strong>model with tools</strong>.</p><h4>Example-2</h4><p>Imagine you’re building a travel assistant. You can give it tools like:</p><ul><li>SearchFlights → to check flight availability</li><li>WeatherAPI → to get weather updates</li><li>Calculator → to estimate travel costs</li></ul><p>So when a user says, <em>“What’s the cheapest flight to Delhi tomorrow?”</em>, the LLM will:</p><ul><li>Understand the request.</li><li>Decide it needs to use the SearchFlights tool.</li><li>Call that tool with the right inputs.</li><li>Return the result to the user.</li></ul><h4>Types of Tools</h4><ul><li>We have build in tools and custom tools in langchain.</li></ul><h4>Built-in Tools</h4><ul><li>Ready-to-use for common tasks</li></ul><p>Examples:</p><ul><li>PythonREPLTool → run Python code</li><li>RequestsGetTool → make API calls</li><li>WikipediaAPIWrapper → fetch Wikipedia content</li><li>For <a href="https://python.langchain.com/docs/integrations/tools/">Build-In Tools</a></li></ul><pre># Usage of Build-in tool<br>from langchain_community.tools import DuckDuckGoSearchRun<br><br>search = DuckDuckGoSearchRun()<br><br>search.invoke(&quot;Obama&#39;s first name?&quot;)</pre><h4>🛠️ Custom Tools</h4><p>Wrap your own Python functions Use @tool decorator or Tool.from_function()</p><p>Examples:</p><ul><li>get_employee_info(name) → fetch from your database</li><li>summarize_document(path) → summarize your PDFs</li><li>🔧 Tailored for your business logic</li></ul><pre>from langchain.agents import Tool<br>from langchain.agents import initialize_agent<br>from langchain.agents.agent_types import AgentType<br>from langchain.chat_models import ChatOpenAI<br><br># Define your custom function<br>def count_words(text: str) -&gt; str:<br>    num_words = len(text.strip().split())<br>    return f&quot;The text contains {num_words} words.&quot;<br><br># Wrap it as a Tool<br>word_count_tool = Tool(<br>    name=&quot;WordCounter&quot;,<br>    func=count_words,<br>    description=&quot;Counts the number of words in a given text. Use this when you want to know how many words a user has written.&quot;<br>)<br><br># Create a language model<br>llm = ChatOpenAI(temperature=0)<br><br># Initialize the agent with the tool<br>agent = initialize_agent(<br>    tools=[word_count_tool],<br>    llm=llm,<br>    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,<br>    verbose=True,<br>)<br><br># Try running the agent with a prompt<br>response = agent.run(&quot;How many words are in: LangChain makes building LLM apps easy?&quot;)<br>print(response)</pre><h3>🧠 What are Agents in LangChain?</h3><p>An <strong>Agent</strong> is like a smart AI assistant that can:</p><ul><li>Think using a <strong>Large Language Model (LLM)</strong></li><li>Decide <strong>what action to take</strong></li><li>Use <strong>tools</strong> to get information or perform tasks</li><li>Return the final answer to the user</li></ul><h3>🧰 What is a Toolbox in LangChain?</h3><blockquote><em>A </em><strong><em>Toolbox</em></strong><em> is a collection of reusable tools that can be passed to an agent to extend its capabilities.</em></blockquote><h3>🧰 Tool Binding, Tool Calling, and Tool Execution</h3><p>What is Tool Binding :-</p><ul><li>Tool Binding is when you define a <strong>function</strong> and wrap it using the Tool class, giving it a <strong>name</strong>, <strong>function</strong>, and <strong>description</strong>.</li><li><strong>Purpose:</strong> Make the tool usable by agents or chains.</li></ul><pre>from langchain.agents import Tool<br><br>def add_numbers(text: str) -&gt; str:<br>    nums = list(map(int, text.split()))<br>    return str(sum(nums))<br><br>add_tool = Tool(<br>    name=&quot;AddTwoNumbers&quot;,<br>    func=add_numbers,<br>    description=&quot;Adds two numbers passed as a string like &#39;3 5&#39;.&quot;<br>)</pre><h4>What is Tool Calling :-</h4><ul><li>This happens <strong>during runtime</strong>, when an <strong>agent decides</strong> it needs to use a tool to complete a task. The LLM <strong>selects the tool name and input</strong> using reasoning.</li></ul><h4>What is Tool Execution :-</h4><ul><li>This is when the tool’s func() is actually <strong>executed</strong> with the given input (e.g., &quot;3 5&quot;), and the result is returned to the agent.</li></ul><h4>Example :-</h4><pre>from langchain.agents import Tool, initialize_agent, AgentType<br>from langchain.chat_models import ChatOpenAI<br><br># 1. Tool Binding<br>def multiply(text: str) -&gt; str:<br>    a, b = map(int, text.split())<br>    return str(a * b)<br><br>multiply_tool = Tool(<br>    name=&quot;Multiply&quot;,<br>    func=multiply,<br>    description=&quot;Multiplies two numbers provided as a space-separated string.&quot;<br>)<br><br># 2. Initialize agent with tool<br>llm = ChatOpenAI(temperature=0)<br>agent = initialize_agent(<br>    tools=[multiply_tool],<br>    llm=llm,<br>    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,<br>    verbose=True<br>)<br><br># 3. Tool Calling + Execution<br>agent.run(&quot;What is 7 times 6?&quot;)</pre><h4>Example 2:- Currency convertor using tools</h4><ul><li>Step -1 : - Define the Currency Converter Tool</li></ul><pre># Define the Currency Converter Tool<br>import requests<br>from langchain.agents import Tool<br>from langchain.chat_models import ChatOpenAI<br>from langchain.agents import initialize_agent, AgentType<br><br># Currency converter function<br>def convert_currency(input_text: str) -&gt; str:<br>    &quot;&quot;&quot;<br>    Input format: &#39;100 USD to INR&#39;<br>    &quot;&quot;&quot;<br>    try:<br>        parts = input_text.strip().upper().split()<br>        amount = float(parts[0])<br>        from_currency = parts[1]<br>        to_currency = parts[3]<br><br>        # Call exchange rate API<br>        url = f&quot;https://api.exchangerate-api.com/v4/latest/{from_currency}&quot;<br>        response = requests.get(url)<br>        data = response.json()<br><br>        rate = data[&quot;rates&quot;].get(to_currency)<br>        if rate is None:<br>            return f&quot;Conversion rate not found for {to_currency}.&quot;<br>        <br>        converted = amount * rate<br>        return f&quot;{amount} {from_currency} = {converted:.2f} {to_currency}&quot;<br>    <br>    except Exception as e:<br>        return f&quot;Error: {str(e)}. Format should be like &#39;100 USD to INR&#39;&quot;</pre><ul><li>Step-2 Bind It as a Tool</li></ul><pre>currency_tool = Tool(<br>    name=&quot;CurrencyConverter&quot;,<br>    func=convert_currency,<br>    description=&quot;Use this tool to convert currency like &#39;100 USD to INR&#39;.&quot;<br>)</pre><ul><li>Step-3 Initialize LLM Agent with the Tool</li></ul><pre>llm = ChatOpenAI(temperature=0)<br><br>agent = initialize_agent(<br>    tools=[currency_tool],<br>    llm=llm,<br>    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,<br>    verbose=True<br>)<br><br>response = agent.run(&quot;Convert 250 EUR to JPY&quot;)<br><br>print(response)<br><br>-------------------------------------------------------------------------------<br>&gt; Entering new AgentExecutor chain...<br>I should use the CurrencyConverter tool to convert EUR to JPY.<br>Action: CurrencyConverter<br>Action Input: 250 EUR to JPY<br>Observation: 250.0 EUR = 42347.50 JPY<br>Thought:I now know the final answer<br>Final Answer: 250 EUR is equal to 42347.50 JPY.<br><br>&gt; Finished chain.<br>250 EUR is equal to 42347.50 JPY.</pre><h4>Finally</h4><ul><li><strong>Tools = extra powers for the LLM.</strong></li><li>They let your AI <strong>interact with APIs, files, databases</strong>, etc.</li><li>Useful when you want the agent to go <strong>beyond just generating text</strong> and <strong>do real tasks</strong>.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd77d00f126f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Runnables in LangChain]]></title>
            <link>https://medium.com/@adarishanmukh15501/runnables-in-langchain-0c5630335685?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c5630335685</guid>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Sun, 29 Jun 2025 05:54:38 GMT</pubDate>
            <atom:updated>2025-06-29T05:54:38.981Z</atom:updated>
            <content:encoded><![CDATA[<p>In LangChain, a Runnable is a fundamental building block that represents a single task or operation, and it’s the core of the Lang Chain Expression Language (LCEL). Runnables are designed to be composable, meaning you can chain them together to create complex workflows or pipelines.</p><p>Runnable is an object that can take an input and produce an output. Many LangChain components, such as:</p><ul><li><strong>Language Models (LLMs):</strong> Like OpenAI’s GPT models or Google’s Gemini.</li><li><strong>Prompt Templates:</strong> For formatting user input into a suitable prompt for an LLM.</li><li><strong>Output Parsers:</strong> For extracting structured information from an LLM’s response.</li><li><strong>Retrievers:</strong> For fetching relevant documents from a knowledge base.</li><li><strong>Tools:</strong> Functions or APIs that an agent can use.</li></ul><p>They are at the heart of the <strong>LangChain Expression Language (LCEL)</strong>, which allows for declarative and optimized construction of these workflows.</p><h3>RunnableSequence in LangChain</h3><p>A <strong>RunnableSequence</strong> is a way to <strong>run multiple steps in order</strong>, one after another—like a pipeline.</p><p>Each step in the sequence takes the output from the previous step as input.</p><pre>llm = ChatOpenAI(temperature=0)<br><br>prompt1 = PromptTemplate.from_template(<br>  &#39;Explain in short about {country}&#39;<br>)<br>prompt2 = PromptTemplate.from_template(<br>  &#39;Tell me a joke about the {text}.&#39;<br>)<br><br># Output parser<br>parser = StrOutputParser()<br><br># Chain: prompt1 → llm → parser → prompt2 → llm → parser<br>chain = RunnableSequence(<br>        prompt1,<br>        llm,<br>        parser,<br>        prompt2,<br>        llm,<br>        parser<br>    <br>)<br><br># Invoke<br>result = chain.invoke({&quot;country&quot;: &quot;NewZealand&quot;})<br>print(result)<br></pre><h3>RunnableParallel in LangChain</h3><p><strong>RunnableParallel</strong> lets you <strong>run multiple tasks at the same time</strong> and get all their results together.<br> It’s useful when you want to <strong>ask the LLM or tools multiple things at once</strong>.</p><pre><br>llm_summary = ChatOpenAI(temperature=0)<br>llm_quiz = ChatOpenAI(temperature=0.9)<br><br>parser = StrOutputParser()<br><br>summary_prompt = PromptTemplate.from_template(<br>    &quot;Give a short and clear summary about {country}.&quot;<br>)<br>summary_chain = summary_prompt | llm_summary | parser<br><br>quiz_prompt = PromptTemplate.from_template(<br>    &quot;Generate 5 interesting quiz questions based on {country}.&quot;<br>)<br>quiz_chain = quiz_prompt | llm_quiz | parser<br><br>parallel_chain = RunnableParallel({<br>    &quot;summary&quot;: summary_chain,<br>    &quot;quiz&quot;: quiz_chain<br>})<br><br>result = parallel_chain.invoke({&quot;country&quot;: &quot;Australia&quot;})<br><br># Print the output nicely<br>print(&quot;📄 Summary:\n&quot;, result[&quot;summary&quot;])<br>print(&quot;\n🧠 Quiz Questions:\n&quot;, result[&quot;quiz&quot;])</pre><h3>RunnablePassthrough in LangChain</h3><p><strong>RunnablePassthrough</strong> is the <strong>simplest runnable</strong> that just returns the input <strong>as-is</strong>, without doing anything to it.</p><pre>from langchain_core.runnables import RunnablePassthrough<br><br>passthrough = RunnablePassthrough()<br><br>output = passthrough.invoke({&quot;message&quot;: &quot;Hello from LangChain!&quot;})<br>print(output)</pre><blockquote><em>In a </em><em>RunnableSequence, the input flows step-by-step, where the output of one component becomes the input to the next. For example, if we start with a </em><em>country input, the first prompt generates some text (e.g., a summary), and that output is then passed into the next prompt (e.g., to create a joke or quiz).</em></blockquote><blockquote><em>However, there are scenarios where you may want to </em><strong><em>preserve the original input</em></strong><em> or </em><strong><em>inspect both the original input and intermediate output</em></strong><em>. This is where </em><em>RunnablePassthrough becomes useful.</em></blockquote><blockquote><em>By using </em><em>RunnablePassthrough, you can </em><strong><em>fork or capture data at specific points</em></strong><em> in the pipeline without altering it. This allows you to run multiple chains in parallel or access both input and intermediate values—for example, comparing the input prompt with the final result, or evaluating different outputs side by side.</em></blockquote><pre><br>llm = ChatOpenAI(temperature=0)<br><br># Output parser<br>parser = StrOutputParser()<br><br># Summary Chain<br>summary_prompt = PromptTemplate.from_template(<br>    &quot;Give a short and clear summary about {country}.&quot;<br>)<br>summary_chain = summary_prompt | llm | parser<br><br># Quiz Chain<br>quiz_prompt = PromptTemplate.from_template(<br>    &quot;Generate 5 interesting quiz questions based on  the {text}.&quot;<br>)<br><br>passthrough = RunnablePassthrough()<br><br><br># Parallel Execution Chain<br>parallel_chain = RunnableParallel({<br>    &quot;summary&quot;: summary_chain,<br>    &quot;quiz&quot;: passthrough<br>})<br><br>combined_chain = RunnableSequence(summary_chain,parallel_chain)<br><br># Run the chain<br>result = combined_chain.invoke({&quot;country&quot;: &quot;Australia&quot;})<br><br># Print the output nicely<br>print(&quot;📄 Summary:\n&quot;, result[&quot;summary&quot;])<br>print(&quot;\n🧠 Quiz Questions:\n&quot;, result[&quot;quiz&quot;])</pre><h3>RunnableLambda in LangChain</h3><p><strong>RunnableLambda</strong> is a way to wrap a simple <strong>Python function</strong> as a <strong>runnable step</strong> in a LangChain pipeline.</p><p><strong>LangChain runnables</strong>: one chain generates text (like a summary or context), and the next computes something from that text (like word count), using a <strong>Python function wrapped with </strong><strong>RunnableLambda</strong>.</p><h3>Use Case: Two-Stage Runnable Chain</h3><blockquote><em>1️⃣ First chain: Generate a context/summary from input (e.g., country name)<br> 2️⃣ Second chain: Count how many words are in the generated response</em></blockquote><pre>Use Case: Two-Stage Runnable Chain<br>1️⃣ First chain: Generate a context/summary from input (e.g., country name)<br>2️⃣ Second chain: Count how many words are in the generated response<br><br>from langchain_core.runnables import RunnableLambda, RunnableSequence<br>from langchain.prompts import PromptTemplate<br>from langchain.chat_models import ChatOpenAI<br>from langchain_core.output_parsers import StrOutputParser<br><br># ---- 1. Word Count Function ----<br>def count_words(text: str) -&gt; int:<br>    words = text.strip().split()<br>    return len(words)<br><br># ---- 2. RunnableLambda for Word Count ----<br>word_counter = RunnableLambda(lambda x: {&quot;summary&quot;: x, &quot;word_count&quot;: count_words(x)})<br><br># ---- 3. LLM and Output Parser ----<br>llm = ChatOpenAI(temperature=0)<br>parser = StrOutputParser()<br><br># ---- 4. Prompt for Summary ----<br>summary_prompt = PromptTemplate.from_template(<br>    &quot;Give a short and clear summary about {country}.&quot;<br>)<br><br># ---- 5. Chain: Prompt → LLM → Parse → Word Count ----<br>full_chain = (<br>    summary_prompt<br>    | llm<br>    | parser<br>    | word_counter<br>)<br><br># ---- 6. Invoke the chain ----<br>result = full_chain.invoke({&quot;country&quot;: &quot;Brazil&quot;})<br><br># ---- 7. Output ----<br>print(&quot;📄 Summary:\n&quot;, result[&quot;summary&quot;])<br>print(&quot;🔢 Word Count:\n&quot;, result[&quot;word_count&quot;])</pre><h3>RunnableBranch in LangChain</h3><p><strong>RunnableBranch</strong> lets you <strong>conditionally choose between multiple runnable chains</strong> based on the input.</p><blockquote><em>RunnableBranch is like an </em><em>if-elif-else chain that picks which path (chain or function) to run based on the input.</em></blockquote><pre>from langchain_core.runnables import RunnableBranch, RunnableLambda<br>from langchain.prompts import PromptTemplate<br>from langchain.chat_models import ChatOpenAI<br>from langchain_core.output_parsers import StrOutputParser<br><br>llm = ChatOpenAI(temperature=0)<br>parser = StrOutputParser()<br><br># HR Chain<br>hr_prompt = PromptTemplate.from_template(&quot;Explain the onboarding process for HR.&quot;)<br>hr_chain = hr_prompt | llm | parser<br><br># Finance Chain<br>finance_prompt = PromptTemplate.from_template(&quot;What are the financial reporting duties?&quot;)<br>finance_chain = finance_prompt | llm | parser<br><br># Default Chain<br>default_chain = RunnableLambda(lambda _: &quot;Sorry, no info available for this role.&quot;)<br><br># Branch logic<br>branch_chain = RunnableBranch(<br>    (lambda x: x.get(&quot;role&quot;) == &quot;HR&quot;, hr_chain),<br>    (lambda x: x.get(&quot;role&quot;) == &quot;Finance&quot;, finance_chain),<br>    default_chain  # fallback if none match<br>)<br><br># Run examples<br>print(&quot;🧑‍💼 HR Output:\n&quot;, branch_chain.invoke({&quot;role&quot;: &quot;HR&quot;}))<br>print(&quot;\n💰 Finance Output:\n&quot;, branch_chain.invoke({&quot;role&quot;: &quot;Finance&quot;}))<br>print(&quot;\n🤷 Unknown Role Output:\n&quot;, branch_chain.invoke({&quot;role&quot;: &quot;Marketing&quot;}))</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c5630335685" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LANGCHAIN PART-3]]></title>
            <link>https://medium.com/@adarishanmukh15501/langchain-part-3-0d844d61146b?source=rss-fe524b420d71------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d844d61146b</guid>
            <dc:creator><![CDATA[Adarishanmukh]]></dc:creator>
            <pubDate>Sat, 14 Jun 2025 11:26:37 GMT</pubDate>
            <atom:updated>2025-06-15T05:22:50.846Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>Please refer this link for LANGCHAIN </em><a href="https://medium.com/@adarishanmukh15501/langchain-part-2-9436e7a10483"><em>PART-2</em></a><em> and </em><a href="https://medium.com/@adarishanmukh15501/langchain-part-1-4853eb4166f2"><em>PART-1</em></a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/0*tDS6ysO-1hZW2a0Z.png" /></figure><h3>📚 Table of Contents</h3><h4>8. Chunks</h4><ul><li>CharacterTextSplitter</li><li>RecursiveCharacterTextSplitter</li><li>HTMLHeaderTextSplitter</li><li>RecursiveJsonSplitter</li></ul><h4><strong>9. Embedding</strong></h4><ul><li>OpenAIEmbeddings</li><li>HuggingFaceEmbeddings</li></ul><h4><strong>10. Vector store vs Vector database</strong></h4><h4><strong>11. SQL/NOSQL vs Vector database</strong></h4><h4><strong>12. Indexing</strong></h4><h3>Why Do We Split Text into Chunks?</h3><ul><li><strong>LLMs have a limited context window</strong>, which restricts the amount of text they can process at once.</li><li>Feeding large documents directly into LLMs is not feasible.</li></ul><h4>Splitting allows:</h4><ul><li>Efficient <strong>vector embedding</strong>.</li><li><strong>Chunk-level retrieval</strong> based on semantic similarity.</li><li><strong>Improved performance and cost-efficiency</strong> in LLM usage.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/0*TkBdzkA53qaT1yJ7.png" /></figure><h4>CharacterTextSplitter</h4><ul><li><strong>Strategy</strong>: Splits text <strong>only using a specific separator</strong> you provide (e.g., &quot;\n&quot;).</li><li><strong>No recursive logic</strong>: It simply chunks based on the separator linearly.</li><li><strong>Simpler</strong> and more manual control, but <strong>less smart</strong> about keeping context.</li></ul><p><strong>RecursiveCharacterTextSplitter</strong></p><ul><li><strong>Strategy</strong>: Tries to split text at <strong>prioritized boundaries</strong>, starting from larger to smaller units (e.g., paragraphs, then sentences, then words).</li><li><strong>Goal</strong>: Preserve semantic integrity as much as possible.</li><li><strong>Fallback logic</strong>: If it can’t find a good place to split, it will recursively go to the next smaller boundary.</li><li><strong>Separator priority (default)</strong>: [&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;]<br> (but customizable)</li></ul><h4><strong>Example</strong></h4><ul><li>Lets use the sample text and do splitting in the same pattern and check the results.</li><li>We tested CharacterTextSplitter and RecursiveCharacterTextSplitter on a paragraph without newlines.</li><li>CharacterTextSplitter didn’t split the text because it relies strictly on the \n separator.</li><li>RecursiveCharacterTextSplitter split the text into meaningful chunks by finding natural breaks like spaces and punctuation.</li></ul><pre>text = (<br>    &quot;LangChain makes it easy to build LLM-powered applications. &quot;<br>    &quot;It provides components and interfaces to help with data ingestion, retrieval, and output formatting. &quot;<br>    &quot;RecursiveCharacterTextSplitter is ideal when the structure of the content varies. &quot;<br>    &quot;CharacterTextSplitter, on the other hand, is more rigid and splits only where instructed. &quot;<br>    &quot;Choosing the right splitter depends on the nature of the input text. &quot;<br>    &quot;When paragraphs, sentences, and words are mixed, recursive splitting is more natural. &quot;<br>    &quot;In structured data, character-based splitting works just fine. &quot;<br>    &quot;Context preservation is key in many NLP applications. &quot;<br>    &quot;Chunk overlap also helps maintain context across boundaries. &quot;<br>    &quot;Always test with different splitters to find what works best for your data.&quot;<br>)</pre><pre>#CharacterTextSplitter<br><br>from langchain_text_splitters import CharacterTextSplitter<br>from langchain_core.documents import Document<br><br>text_splitter = CharacterTextSplitter(<br>    separator=&quot;\n&quot;,  # but our text has no &quot;\n&quot;, it&#39;s all one paragraph<br>    chunk_size=200,<br>    chunk_overlap=50,<br>    is_separator_regex=False,<br>)<br><br>doc = Document(page_content=text)<br>docs = [doc]<br><br>chunks = text_splitter.split_documents(docs)<br><br>for i, chunk in enumerate(chunks):<br>    print(&quot;chunk&quot;,chunk)<br>    print(f&quot;\nChunk {i + 1}:\n{chunk.page_content}&quot;)</pre><pre>------------------------------------------output-------------------------------------<br>chunk page_content=&#39;LangChain makes it easy to build LLM-powered applications. It provides components and interfaces to help with data ingestion, retrieval, and output formatting. RecursiveCharacterTextSplitter is ideal when the structure of the content varies. CharacterTextSplitter, on the other hand, is more rigid and splits only where instructed. Choosing the right splitter depends on the nature of the input text. When paragraphs, sentences, and words are mixed, recursive splitting is more natural. In structured data, character-based splitting works just fine. Context preservation is key in many NLP applications. Chunk overlap also helps maintain context across boundaries. Always test with different splitters to find what works best for your data.&#39;<br><br>Chunk 1:<br>LangChain makes it easy to build LLM-powered applications. It provides components and interfaces to help with data ingestion, retrieval, and output formatting. RecursiveCharacterTextSplitter is ideal when the structure of the content varies. CharacterTextSplitter, on the other hand, is more rigid and splits only where instructed. Choosing the right splitter depends on the nature of the input text. When paragraphs, sentences, and words are mixed, recursive splitting is more natural. In structured data, character-based splitting works just fine. Context preservation is key in many NLP applications. Chunk overlap also helps maintain context across boundaries. Always test with different splitters to find what works best for your data.</pre><pre># RecursiveCharacterTextSplitter<br><br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>from langchain_core.documents import Document<br><br>text_splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=200,<br>    chunk_overlap=50,<br>    is_separator_regex=False,<br>)<br><br>doc = Document(page_content=text)<br>docs = [doc]<br><br>chunks = text_splitter.split_documents(docs)<br><br>for i, chunk in enumerate(chunks):<br>    print(&quot;chunk&quot;,chunk)<br>    print(f&quot;\nChunk {i + 1}:\n{chunk.page_content}&quot;)</pre><pre>------------------------------------------output-------------------------------------<br>chunk page_content=&#39;LangChain makes it easy to build LLM-powered applications. It provides components and interfaces to help with data ingestion, retrieval, and output formatting. RecursiveCharacterTextSplitter is ideal&#39;<br><br>Chunk 1:<br>LangChain makes it easy to build LLM-powered applications. It provides components and interfaces to help with data ingestion, retrieval, and output formatting. RecursiveCharacterTextSplitter is ideal<br>chunk page_content=&#39;RecursiveCharacterTextSplitter is ideal when the structure of the content varies. CharacterTextSplitter, on the other hand, is more rigid and splits only where instructed. Choosing the right splitter&#39;<br><br>Chunk 2:<br>RecursiveCharacterTextSplitter is ideal when the structure of the content varies. CharacterTextSplitter, on the other hand, is more rigid and splits only where instructed. Choosing the right splitter<br>chunk page_content=&#39;where instructed. Choosing the right splitter depends on the nature of the input text. When paragraphs, sentences, and words are mixed, recursive splitting is more natural. In structured data,&#39;<br><br>Chunk 3:<br>where instructed. Choosing the right splitter depends on the nature of the input text. When paragraphs, sentences, and words are mixed, recursive splitting is more natural. In structured data,<br>chunk page_content=&#39;splitting is more natural. In structured data, character-based splitting works just fine. Context preservation is key in many NLP applications. Chunk overlap also helps maintain context across&#39;<br><br>Chunk 4:<br>splitting is more natural. In structured data, character-based splitting works just fine. Context preservation is key in many NLP applications. Chunk overlap also helps maintain context across<br>chunk page_content=&#39;Chunk overlap also helps maintain context across boundaries. Always test with different splitters to find what works best for your data.&#39;<br><br>Chunk 5:<br>Chunk overlap also helps maintain context across boundaries. Always test with different splitters to find what works best for your data.</pre><h3>Output Difference</h3><ul><li><strong>CharacterTextSplitter</strong> will give <strong>1 full chunk</strong> (text is never split, because no \n is found).</li></ul><h4><strong>HTMLHeaderTextSplitter</strong></h4><p>Splits HTML content based on header tags like &lt;h1&gt;, &lt;h2&gt;, etc.<br> It preserves document structure by treating each header section as a logical chunk, useful for web pages or structured HTML content.</p><pre>from langchain_text_splitters import HTMLHeaderTextSplitter<br><br>html_string = &quot;&quot;&quot;<br>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;body&gt;<br>    &lt;div&gt;<br>        &lt;h1&gt;Foo&lt;/h1&gt;<br>        &lt;p&gt;Some intro text about Foo.&lt;/p&gt;<br>        &lt;div&gt;<br>            &lt;h2&gt;Bar main section&lt;/h2&gt;<br>            &lt;p&gt;Some intro text about Bar.&lt;/p&gt;<br>            &lt;h3&gt;Bar subsection 1&lt;/h3&gt;<br>            &lt;p&gt;Some text about the first subtopic of Bar.&lt;/p&gt;<br>            &lt;h3&gt;Bar subsection 2&lt;/h3&gt;<br>            &lt;p&gt;Some text about the second subtopic of Bar.&lt;/p&gt;<br>        &lt;/div&gt;<br>        &lt;div&gt;<br>            &lt;h2&gt;Baz&lt;/h2&gt;<br>            &lt;p&gt;Some text about Baz&lt;/p&gt;<br>        &lt;/div&gt;<br>        &lt;br&gt;<br>        &lt;p&gt;Some concluding text about Foo&lt;/p&gt;<br>    &lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br>&quot;&quot;&quot;<br><br>headers_to_split_on=[<br>    (&quot;h1&quot;,&quot;Header 1&quot;),<br>    (&quot;h2&quot;,&quot;Header 2&quot;),<br>    (&quot;h3&quot;,&quot;Header 3&quot;)<br>]<br><br>html_splitter=HTMLHeaderTextSplitter(headers_to_split_on)<br>html_header_splits=html_splitter.split_text(html_string)<br>html_header_splits</pre><pre>------------------------------------------output-------------------------------------<br>[Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;}, page_content=&#39;Foo&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;}, page_content=&#39;Some intro text about Foo.&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;, &#39;Header 2&#39;: &#39;Bar main section&#39;}, page_content=&#39;Bar main section&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;, &#39;Header 2&#39;: &#39;Bar main section&#39;}, page_content=&#39;Some intro text about Bar.&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;, &#39;Header 2&#39;: &#39;Bar main section&#39;, &#39;Header 3&#39;: &#39;Bar subsection 1&#39;}, page_content=&#39;Bar subsection 1&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;, &#39;Header 2&#39;: &#39;Bar main section&#39;, &#39;Header 3&#39;: &#39;Bar subsection 1&#39;}, page_content=&#39;Some text about the first subtopic of Bar.&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;, &#39;Header 2&#39;: &#39;Bar main section&#39;, &#39;Header 3&#39;: &#39;Bar subsection 2&#39;}, page_content=&#39;Bar subsection 2&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;, &#39;Header 2&#39;: &#39;Bar main section&#39;, &#39;Header 3&#39;: &#39;Bar subsection 2&#39;}, page_content=&#39;Some text about the second subtopic of Bar.&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;, &#39;Header 2&#39;: &#39;Baz&#39;}, page_content=&#39;Baz&#39;),<br> Document(metadata={&#39;Header 1&#39;: &#39;Foo&#39;}, page_content=&#39;Some text about Baz  \nSome concluding text about Foo&#39;)]</pre><h4>RecursiveJsonSplitter</h4><p>Used to split large or deeply nested JSON objects.It recursively breaks the JSON into smaller, manageable chunks based on keys and structure while preserving context and hierarchy.</p><pre>import json<br>import requests<br><br>json_data=requests.get(&quot;https://api.smith.langchain.com/openapi.json&quot;).json()<br>json_splitter=RecursiveJsonSplitter(max_chunk_size=300)<br>json_chunks=json_splitter.split_json(json_data)<br><br>json_chunks</pre><p>A <strong>vector store</strong> and a <strong>vector database</strong> are closely related but serve slightly different purposes in handling high-dimensional vector data.</p><h3>Vector Store</h3><ul><li>A general term or abstraction for storing and retrieving vector embeddings.</li><li>Can wrap simple in-memory data (like lists or FAISS).</li><li>Often used in prototyping or small-scale/local applications.</li><li>Not necessarily persistent or scalable.</li></ul><h4>Vector Database</h4><ul><li>A full-fledged database system built for managing vector embeddings.</li><li>Provides indexing, similarity search, filtering, metadata storage, etc.</li><li>Designed for large-scale, production-ready use cases.</li><li>Supports persistence, scalability, replication, and performance tuning.</li><li>Examples: <strong>Pinecone</strong>, <strong>Qdrant</strong>, <strong>Milvus</strong>, <strong>Weaviate</strong>.</li></ul><h4><strong>Embeddings</strong></h4><h3>What Are Embeddings?</h3><p><strong>Embeddings</strong> are a way to <strong>convert words, sentences, or documents into numbers</strong> — but not just random numbers. These numbers capture the <strong>meaning</strong> of the text in a way that a machine can understand.</p><h4><strong>OpenAIEmbeddings</strong></h4><pre>import os<br>from dotenv import load_dotenv<br>from langchain_openai import OpenAIEmbeddings<br>load_dotenv() <br><br>os.environ[&quot;OPENAI_API_KEY&quot;]=os.getenv(&quot;OPENAI_API_KEY&quot;)<br>embeddings = OpenAIEmbeddings(model=&quot;text-embedding-3-large&quot;)<br><br>text=&quot;This is a tutorial on OPENAI embeddings&quot;<br><br>query_result=embeddings.embed_query(text)<br><br>print(query_result)<br>print(len(query_result)</pre><pre>[-0.004172635264694691,<br> 0.04088858142495155,<br> -0.018989549949765205,<br> -0.040317077189683914,.....<br><br><br>3072<br></pre><p>What does 3072 represent</p><h4>Why is the embedding length 3072?</h4><p>That number <strong>3072</strong> is called the <strong>embedding dimension</strong>.</p><ul><li>It means <strong>each piece of text</strong> (like a sentence or paragraph) will be turned into a vector of <strong>3072 floating-point numbers</strong>.</li><li>This vector captures the <strong>meaning, context, and relationships</strong> in a very detailed way.</li><li><strong>3072 = The size of the embedding vector returned by </strong><strong>text-embedding-3-large</strong></li></ul><h3>What Happens If You Reduce the Dimensions?</h3><p>Let’s say you reduce embedding size from <strong>3072 → 512 or 128</strong>. Here’s what happens:</p><p><strong>Pros</strong></p><ol><li><strong>Faster Computation</strong></li></ol><ul><li>Smaller vectors mean faster processing (search, similarity comparison, etc.)</li></ul><p><strong>2. Less Storage</strong></p><ul><li>Uses less memory and disk space — important for large-scale apps.</li></ul><p><strong>3.Speed in Vector DBs</strong></p><ul><li>Vector search (e.g., with Qdrant or FAISS) becomes much faster.</li></ul><h4><strong>Cons</strong></h4><ol><li><strong>Loss of Semantic Meaning</strong></li></ol><ul><li>You lose some <strong>nuance</strong> or <strong>context</strong> from the original text.</li><li>E.g., two similar sentences might appear less similar after reducing.</li></ul><p><strong>2. Lower Accuracy</strong></p><ul><li>For tasks like search, classification, or RAG, performance may drop.</li></ul><p><strong>3. Dimensionality Reduction Might Cause Overlap</strong></p><ul><li>Different texts may end up <strong>closer than they should</strong>, confusing the system.</li></ul><h4>HuggingFaceEmbeddings</h4><pre>import os<br>from sklearn.metrics.pairwise import euclidean_distances<br>from sklearn.metrics.pairwise import cosine_similarity<br><br>os.environ[&#39;HF_TOKEN&#39;]=os.getenv(&quot;HF_TOKEN&quot;)<br><br>embeddings=HuggingFaceEmbeddings(model_name=&quot;all-MiniLM-L6-v2&quot;)<br><br>#Create Sample Documents<br>documents=[&quot;Trump is President of America&quot;,<br>           &quot;Putin is President of Russia&quot;,<br>           &quot;Narendra modi is prime minister of india&quot;]<br><br><br>#Embed All the Documents<br>document_embedding=embeddings.embed_documents(documents)<br><br>#Add your Question<br>question = &quot;Who is the prime minister of INDIA&quot;<br><br># Embed the Question<br>query_embedding=embeddings.embed_query(question)<br><br>print(cosine_similarity([query_embedding],document_embedding))<br>print(euclidean_distances([query_embedding], document_embedding))</pre><pre>---------------------------------output----------------------------------<br>[[0.19956134 0.23035487 0.70161104]]<br>[[1.26525783 1.24068143 0.77251403]]</pre><p>The third document (“Narendra Modi…”) will have the <strong>highest similarity score</strong>, meaning it’s the <strong>most relevant</strong>.</p><ul><li>✅ <strong>Cosine Similarity</strong> → Measures <strong>angle</strong> between vectors (semantic similarity)</li><li>✅ <strong>Euclidean Distance</strong> → Measures <strong>physical distance</strong> between vectors (spatial distance)</li><li>⚠️ In <strong>Euclidean distance</strong>, <strong>lower is better (The lower distance the closest match).</strong></li><li>✅ In <strong>Cosine similarity</strong>, <strong>higher is better.</strong></li></ul><p>This is a <strong>mini RAG system</strong>:</p><ul><li>Convert knowledge base to embeddings ✅</li><li>Convert question to embedding ✅</li><li>Compare with cosine similarity or Euclidean distance✅</li><li>Find the best match ✅</li></ul><h4>SQL VS VECTOR DB</h4><p>Embeddings Are Not Simple Values</p><p>Traditional databases store and query:</p><ul><li><strong>Strings</strong> (like names, titles)</li><li><strong>Numbers</strong> (prices, dates)</li><li><strong>Booleans</strong> (true/false)</li><li>And do operations like: =, &lt;, &gt;, LIKE, etc.</li></ul><p>But <strong>embeddings</strong> are different:</p><ul><li>They are <strong>high-dimensional vectors</strong> (e.g., [0.23, -0.17, ..., 0.95])</li><li>You need to <strong>compare them using vector math</strong>, like:</li><li><strong>Cosine similarity</strong></li><li><strong>Euclidean distance</strong></li></ul><p>👉 <strong>SQL/NoSQL databases don’t support that kind of search natively.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*dpLS58dQRUEYck73KpmmBA.png" /></figure><h3>WHY DO WE NEED TO CREATE AN INDEX?</h3><p>Indexing helps your vector database search faster through thousands or millions of vector embeddings.</p><h3>Without Indexing</h3><p>If you just store 100,000 document vectors in a list and want to find the closest match for a query, what happens?</p><ul><li>It has to <strong>compare your query vector with every single document</strong> (brute force).</li><li>That’s slow, especially for large datasets.</li><li>It doesn’t scale.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*KszGC_gXYKXyoDnqYsiIAg.png" /></figure><h3>✅ Basic Flow (Without Indexing — Flat Search)</h3><p>Let’s say you have 1000 document vectors stored in memory using IndexFlatL2.</p><h3>🔄 Query Flow:</h3><ol><li><strong>User provides a query</strong></li><li>The system converts the query into an <strong>embedding vector</strong></li><li>FAISS runs a <strong>brute-force search</strong>:</li></ol><ul><li>Compares the query vector to <strong>all 1000 document vectors</strong></li><li>Calculates <strong>L2 (Euclidean)</strong> or <strong>cosine distance</strong></li></ul><ol><li>Ranks the results by similarity</li><li>Returns top-k most similar documents</li></ol><p>✅ <strong>Accurate</strong>, ❌ <strong>Slow</strong> if you have 1 million+ vectors</p><h3>Query Flow with Indexing (Cluster-Based)</h3><ol><li><strong>User provides a query</strong></li><li>Query is converted into an <strong>embedding vector</strong></li><li>FAISS compares the query to <strong>centroids</strong> (cluster centers)</li><li>It identifies the <strong>top N clusters</strong> that are most likely to contain similar vectors</li><li>It only searches <strong>within those clusters</strong> (e.g., 10% of total vectors)</li><li>Computes distances</li><li>Returns top-k most similar documents</li></ol><p>✅ Much <strong>faster</strong>, ❌ Might miss slightly better matches (approximate)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BBvu8UhxpLVdJnsodT6PMg.png" /></figure><pre>import faiss<br>from langchain_community.vectorstores import FAISS<br>from langchain_community.docstore.in_memory import InMemoryDocstore<br># 384 is embedding  size<br><br>index=faiss.IndexFlatL2(384)<br>#index=faiss.IndexFlatIP(384) choose any 1 <br><br>vector_store=FAISS(<br>    embedding_function=embeddings,<br>    index=index,<br>    docstore=InMemoryDocstore(),<br>    index_to_docstore_id={},<br>)<br>vector_store.index_to_docstore_id<br>results = vector_store.similarity_search(&quot;Tell me about AI&quot;, k=3)</pre><ul><li><strong>IndexFlatL2</strong> uses <strong>Euclidean distance</strong>, where <strong>lower values mean higher similarity</strong> — best for image or spatial data.</li><li><strong>IndexFlatIP</strong> uses <strong>dot product</strong>, and with <strong>normalized vectors</strong>, acts like <strong>cosine similarity</strong> — best for text embeddings.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d844d61146b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>