<?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 Vijaykumar Kartha on Medium]]></title>
        <description><![CDATA[Stories by Vijaykumar Kartha on Medium]]></description>
        <link>https://medium.com/@vijaykumarkartha?source=rss-1c0c72b715b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6crmKWJtTgMUZxWSpv5fOg.png</url>
            <title>Stories by Vijaykumar Kartha on Medium</title>
            <link>https://medium.com/@vijaykumarkartha?source=rss-1c0c72b715b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 01:21:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vijaykumarkartha/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[Create Your Personal Gmail AI Agent Using LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/create-your-personal-gmail-ai-agent-using-langchain-da95d1a9171a?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/da95d1a9171a</guid>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[langchain-agents]]></category>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Tue, 28 May 2024 06:47:08 GMT</pubDate>
            <atom:updated>2024-05-29T03:29:26.261Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2hkvZ6XDQ8UQehZg" /></figure><p>Would you like your own Gmail AI agent, that can help you do just about anything you do with emails and save you time and effort? You can do it in just 20 lines of code, thanks to LangChain.</p><p>Let’s first do some pre-requisite work before starting with LangChain.</p><p>We need to do the following tasks:</p><ol><li>Enable the Gmail API</li><li>Get our Google account credentials.json file in a local folder</li></ol><h3>Enable the Gmail API</h3><p>To enable the Gmail API, you need to log in at: <a href="https://console.cloud.google.com/">https://console.cloud.google.com</a> with your Google account.</p><h3>Create a New Project.</h3><ol><li>Click on <a href="https://console.cloud.google.com/projectcreate">https://console.cloud.google.com/projectcreate</a></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/0*cpjoiTsolKGxOF5u" /></figure><ol><li>Enter a name for your project and click Create.</li></ol><h3>Enable Gmail API</h3><ol><li>Click on APIs and Services.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/0*iJbv2LAHeQn3H5KU" /></figure><p>and click on Library. Or, you may directly go to <a href="https://console.cloud.google.com/apis/library">https://console.cloud.google.com/apis/library</a></p><ol><li>Search for Gmail API</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/0*fUm2fdPACLJ34KUH" /></figure><ol><li>Click Enable</li></ol><h3>Download Credentials JSON File</h3><ol><li>To download the Credentials JSON file, follow the instructions in the <a href="https://developers.google.com/gmail/api/quickstart/python#authorize_credentials_for_a_desktop_application">Google API docs</a>.</li><li>Save the JSON file as credentials.json file in the same folder as your Python program.</li></ol><p>Now, let’s start work on creating the Gmail agent using Langchain with Python.</p><h3>Create your personal Gmail agent</h3><h3>Install libraries</h3><p>From the terminal, install the following libraries.</p><pre>pip install --upgrade  google-api-python-client<br>pip install --upgrade  google-auth-oauthlib<br>pip install --upgrade  google-auth-httplib2<br>pip install -U langchain-community<br>pip install -U langchain-openai langchainhub</pre><h3>Create the Tools</h3><p>For our Gmail agent, we use GmailToolkit provided by Langchain.</p><pre>from langchain_community.agent_toolkits import GmailToolkit <br>toolkit = GmailToolkit()</pre><p>Next, we ensure that the gmail toolkit is linked to our gmail account through our credentials.json file.</p><pre>from langchain_community.tools.gmail.utils import (<br>    build_resource_service,<br>    get_gmail_credentials,<br>)</pre><pre>credentials = get_gmail_credentials(<br>    token_file=&quot;token.json&quot;,<br>    scopes=[&quot;https://mail.google.com/&quot;],<br>    client_secrets_file=&quot;credentials.json&quot;,<br>)<br>api_resource = build_resource_service(credentials=credentials)<br>toolkit = GmailToolkit(api_resource=api_resource)</pre><p>We then get the tools from the toolkit.</p><pre>tools = toolkit.get_tools()<br>tools</pre><p>We will also create the tool from Tavily if required to do a web search. You can get an API for free from tavily.com.</p><pre>import os<br>os.environ[&#39;TAVILY_API_KEY&#39;] = &quot;Your Tavily API key here&quot;</pre><h3>Create the llm</h3><pre>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;Your Open AI API key here&quot;)</pre><h3>Create the Gmail agent</h3><pre>from langchain import hub<br>from langchain.agents import AgentExecutor, create_openai_functions_agent</pre><h3>Create the prompts</h3><pre>instructions = &quot;&quot;&quot;You are an assistant.&quot;&quot;&quot;<br>base_prompt = hub.pull(&quot;langchain-ai/openai-functions-template&quot;)<br>prompt = base_prompt.partial(instructions=instructions)</pre><h3>Create the agent</h3><p>Create the agent using the llm, toolkits and the prompt.</p><pre>agent = create_openai_functions_agent(llm, toolkit.get_tools(), prompt)</pre><p>We use the AgentExecutor class from LangChain to create an AgentExecutor object from the agent.</p><pre>agent_executor = AgentExecutor(<br>    agent=agent,<br>    tools=toolkit.get_tools(),<br>    # This is set to False to prevent information about my email showing up on the screen<br>    # Normally, it is helpful to have it set to True however.<br>    verbose=False,<br>)</pre><p>That’s it. Our own personal gmail agent is ready to be invoked.</p><pre>agent_executor.invoke(<br>    {<br>        &quot;input&quot;: &quot;Create a test mail with text &#39;This is a test mail&#39; and put in the Drafts mail folder.&quot;<br>    }<br>)</pre><p>When, the program is run, you will be asked to authorize the application by signing into your Google account. Once, you sign into your Google account, you will see a screen stating that Google hasn’t verified this app.</p><p>You may click on Advanced and go to your app.</p><p>Click Continue to provide access to your Google account.</p><p>The output after invoking the agent_executor is:</p><pre>{&#39;input&#39;: &quot;Create a test mail with text &#39;This is a test mail&#39; and put in the Drafts mail folder.&quot;,<br> &#39;output&#39;: &quot;I have created a draft email with the text &#39;This is a test mail&#39;.&quot;}</pre><p>Go to your Gmail Inbox, and you will see the mail in the Drafts folder. Your Gmail AI agent has done the work for you.</p><p>Now, let’s ask for a summary of a mail that has come from a specific person.</p><pre>agent_executor.invoke(<br>    {&quot;input&quot;: &quot;Could you briefly summarize the mail that has come from SuperHuman AI&quot;}<br>)</pre><p>The output is:</p><pre>{&#39;input&#39;: &#39;Could you briefly summarize the mail that has come from SuperHuman AI&#39;,<br> &#39;output&#39;: &#39;The email from SuperHuman AI is titled &quot;Academics debate AI\&#39;s impact.&quot; It discusses various insights related to AI and technology, including topics like how AI will shape the economy, the CHIPS Act, new AI tools for productivity, AI-generated images, and more. The email provides information on recent developments in the AI and tech industry, including OpenAI\&#39;s SearchGPT, Apple\&#39;s share buyback program, TikTok\&#39;s dispute with Universal Music Group, and Anthropic AI\&#39;s app launch.&#39;}</pre><p>Congratulations! You have made your own personal Gmail agent willing to do any Gmail related work for you.</p><h3><strong>Previous Articles</strong></h3><ul><li><a href="https://medium.com/@vijaykumarkartha/begginers-guid-to-langchain-12ad239ee101">Beginner’s Guide to LangChain</a></li><li><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77">Beginner’s Guide To Retrieval Chain From LangChain</a></li><li><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-conversational-retrieval-chain-using-langchain-3ddf1357f371">Beginner’s Guide To Conversational Retrieval Chain Using LangChain</a></li><li><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-creating-ai-agents-with-langchain-eaa5c10973e6">Beginner’s Guide to Creating AI Agents With LangChain</a></li><li><a href="https://medium.com/@vijaykumarkartha/the-fascinating-mind-of-an-ai-agent-from-langchain-b54bcbdcf975">The Fascinating Mind of an AI Agent With LangChain</a></li><li><a href="https://medium.com/@vijaykumarkartha/multiple-ai-agents-creating-multi-agent-workflows-using-langgraph-and-langchain-0587406ec4e6">Multiple AI Agents: Creating Multi-Agent Workflows Using LangGraph and LangChain</a></li><li><a href="https://medium.com/@vijaykumarkartha/self-reflecting-ai-agents-using-langchain-d3a93684da92">Self-Reflecting AI Agents Using LangChain</a></li><li><a href="https://medium.com/@vijaykumarkartha/hierarchical-ai-agents-create-a-supervisor-ai-agent-using-langchain-315abbbd4133">Hierarchical AI Agents: Create a Supervisor AI Agent Using LangChain</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da95d1a9171a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hierarchical AI Agents: Create a Supervisor AI Agent Using LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/hierarchical-ai-agents-create-a-supervisor-ai-agent-using-langchain-315abbbd4133?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/315abbbd4133</guid>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[ai-agent-supervisor]]></category>
            <category><![CDATA[hierarchical-agents]]></category>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Fri, 03 May 2024 06:30:53 GMT</pubDate>
            <atom:updated>2024-05-03T06:30:53.211Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DpYHPaxxcVFxtRSU" /><figcaption>Credit: Linkedin</figcaption></figure><p>In the previous article, we learnt about multiple AI agents and created a Multi-Agent Workflow. The agents collaborated with each other to complete a complex task.</p><h3>Hierarchical Manner of Working</h3><p>But, most of us are used to working in a hierarchical manner. We have a manager that gives us the tasks; we follow their orders and submit the tasks back to them, and the manager decides on the next task for us. This helps in managing large tasks by sub-dividing the tasks and having a manager supervise the tasks amongst its subordinates.</p><h3>Supervisor AI Agent</h3><p>Similarly, we can create a supervisor AI agent under which we can have multiple subordinate AI agents who will follow the orders of the supervisors. The subordinate AI agents act like tools that can be executed by the supervisor AI agent.</p><p>This concept is illustrated in the diagram below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IxgeIvdV8GtDeN_o" /><figcaption>Credit: LangChain</figcaption></figure><p>The user interacts with the supervisor AI agent who has a team of AI agents at their disposition. The supervisor can route a message to any of the AI agents under its supervision who will do the task and communicate back to the supervisor. The supervisor will choose to route the task back to another agent, and finally when the task is complete, the output will be communicated back to the user.</p><p>Let’s create the multi-agent workflow with hierarchical agents. First, we create the llm.</p><h3>LLM</h3><pre>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;Your OpenAI API key here&quot;)</pre><h3>Tools</h3><p>Next, let us create the tools and the associated libraries for them. We will use the Tavily Search tool and the Python REPL tool.</p><pre>import os<br>os.environ[&#39;TAVILY_API_KEY&#39;] = &quot;Your Tavily API Key here&quot;</pre><pre>from typing import Annotated,List,Tuple,Union</pre><pre>from langchain_community.tools.tavily_search import TavilySearchResults<br>from langchain_core.tools import tool<br>from langchain_experimental.tools import PythonREPLTool</pre><pre>tavily_tool = TavilySearchResults(max_results = 5)</pre><pre>python_repl_tool = PythonREPLTool()</pre><h3>Worker Agents</h3><p>Now, let’s think about creating worker agents. One way is to write a function that can create a worker agent and provide it with a list of tools as needed.</p><p>The function will use the AgentExecutor class to to execute the creation of an agent.</p><pre>from langchain.agents import AgentExecutor, create_openai_tools_agent<br>from langchain_core.messages import BaseMessage, HumanMessage<br>from langchain_openai import ChatOpenAI</pre><pre>def create_agent(llm: ChatOpenAI, tools: list, system_prompt: str):<br>    # Each worker node will be given a name and some tools.<br>    prompt = ChatPromptTemplate.from_messages(<br>        [<br>            (<br>                &quot;system&quot;,<br>                system_prompt,<br>            ),<br>            MessagesPlaceholder(variable_name=&quot;messages&quot;),<br>            MessagesPlaceholder(variable_name=&quot;agent_scratchpad&quot;),<br>        ]<br>    )<br>    agent = create_openai_tools_agent(llm, tools, prompt)<br>    executor = AgentExecutor(agent=agent, tools=tools)<br>    return executor</pre><h3>Supervisor Agent</h3><p>Next, we create the Supervisor Agent. The Supervisor agent has to decide the agent who will process the next task, and also decide when the task is completed. It will get the messages from the user or the agent as Human Messages and decide on the agent who will act next, or finish the task.</p><pre>from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser<br>from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder</pre><pre>members = [&quot;Researcher&quot;, &quot;Coder&quot;]<br>system_prompt = (<br>    &quot;You are a supervisor tasked with managing a conversation between the&quot;<br>    &quot; following workers:  {members}. Given the following user request,&quot;<br>    &quot; respond with the worker to act next. Each worker will perform a&quot;<br>    &quot; task and respond with their results and status. When finished,&quot;<br>    &quot; respond with FINISH.&quot;<br>)<br># Our team supervisor is an LLM node. It just picks the next agent to process<br># and decides when the work is completed<br>options = [&quot;FINISH&quot;] + members<br># Using openai function calling can make output parsing easier for us<br>function_def = {<br>    &quot;name&quot;: &quot;route&quot;,<br>    &quot;description&quot;: &quot;Select the next role.&quot;,<br>    &quot;parameters&quot;: {<br>        &quot;title&quot;: &quot;routeSchema&quot;,<br>        &quot;type&quot;: &quot;object&quot;,<br>        &quot;properties&quot;: {<br>            &quot;next&quot;: {<br>                &quot;title&quot;: &quot;Next&quot;,<br>                &quot;anyOf&quot;: [<br>                    {&quot;enum&quot;: options},<br>                ],<br>            }<br>        },<br>        &quot;required&quot;: [&quot;next&quot;],<br>    },<br>}<br>prompt = ChatPromptTemplate.from_messages(<br>    [<br>        (&quot;system&quot;, system_prompt),<br>        MessagesPlaceholder(variable_name=&quot;messages&quot;),<br>        (<br>            &quot;system&quot;,<br>            &quot;Given the conversation above, who should act next?&quot;<br>            &quot; Or should we FINISH? Select one of: {options}&quot;,<br>        ),<br>    ]<br>).partial(options=str(options), members=&quot;, &quot;.join(members))</pre><p>We will create a supervisor chain that will run the prompt with the llm and output the response.</p><pre>supervisor_chain = (<br>    prompt<br>    | llm.bind_functions(functions=[function_def], function_call=&quot;route&quot;)<br>    | JsonOutputFunctionsParser()<br>)</pre><p>For each of the agents, we will have a node function that will convert the agent response to a Human Message. This is the trick that is used to make LLMs respond as if it is a user input, rather than coming from the LLM itself.</p><pre>def agent_node(state, agent, name):<br>    result = agent.invoke(state)<br>    return {&quot;messages&quot;: [HumanMessage(content=result[&quot;output&quot;], name=name)]}</pre><h3>Building The Graph</h3><p>Now, we have the Supervisor Agent, the function for creating worker agents, and a node function to convert an agent response into a human message, it is time now to create the worker agents and supervisor agents and build the graph by connecting them.</p><h3>Agent State</h3><p>First, we create an object for the AgentState which will be the list of the messages and the next agent to route to.</p><pre>import operator<br>from typing import Annotated, Any, Dict, List, Optional, Sequence, TypedDict<br>import functools</pre><pre>from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder<br>from langgraph.graph import StateGraph, END</pre><pre># The agent state is the input to each node in the graph<br>class AgentState(TypedDict):<br>    # The annotation tells the graph that new messages will always<br>    # be added to the current states<br>    messages: Annotated[Sequence[BaseMessage], operator.add]<br>    # The &#39;next&#39; field indicates where to route to next<br>    next: str</pre><h3>Worker Agent — Researcher</h3><p>Now, we create the worker agents using the create_agent function. First, we create a worker agent which will be a researcher using the Tavily search tool.</p><pre>research_agent = create_agent(llm, [tavily_tool], &quot;You are a web researcher.&quot;)<br>research_node = functools.partial(agent_node, agent=research_agent, name=&quot;Researcher&quot;)</pre><h3>Worker Agent — Coder</h3><p>Next, we create another worker agent which will be a coder using the PythonREPL tool.</p><pre>code_agent = create_agent(<br>    llm,<br>    [python_repl_tool],<br>    &quot;You may generate safe python code to analyze data and generate charts using matplotlib.&quot;,<br>)<br>code_node = functools.partial(agent_node, agent=code_agent, name=&quot;Coder&quot;)</pre><p>Finally, we build the graph connecting the nodes using the add_node function.</p><pre>workflow = StateGraph(AgentState)<br>workflow.add_node(&quot;Researcher&quot;, research_node)<br>workflow.add_node(&quot;Coder&quot;, code_node)<br>workflow.add_node(&quot;supervisor&quot;, supervisor_chain)</pre><p>And, then, we create all the edges in the graph using the add_edge function.</p><pre>for member in members:<br>    # We want our workers to ALWAYS &quot;report back&quot; to the supervisor when done<br>    workflow.add_edge(member, &quot;supervisor&quot;)<br># The supervisor populates the &quot;next&quot; field in the graph state<br># which routes to a node or finishes<br>conditional_map = {k: k for k in members}<br>conditional_map[&quot;FINISH&quot;] = END<br>workflow.add_conditional_edges(&quot;supervisor&quot;, lambda x: x[&quot;next&quot;], conditional_map)</pre><p>We set the entry point for the multi-agent workflow as the Supervisor which will be given the user input. And, then we compile the graph.</p><pre># Finally, add entrypoint<br>workflow.set_entry_point(&quot;supervisor&quot;)</pre><pre>graph = workflow.compile()</pre><h3>Invoking the Team</h3><p>We are now ready to invoke the AI agents team and get it to complete our tasks.</p><p>Let’s try out two simple tasks.</p><p>Task 1: Print “Hello, World!”</p><p>Task 2: Write a research report on Himalayan Pandas.</p><p><strong>Task 1: Print “Hello, World!”</strong></p><pre>for s in graph.stream(<br>    {<br>        &quot;messages&quot;: [<br>            HumanMessage(content=&quot;Code hello world and print it to the terminal&quot;)<br>        ]<br>    }<br>):<br>    if &quot;__end__&quot; not in s:<br>        print(s)<br>        print(&quot;----&quot;)</pre><p>The output is:</p><pre>{&#39;supervisor&#39;: {&#39;next&#39;: &#39;Coder&#39;}}<br>----<br>Python REPL can execute arbitrary code. Use with caution.<br>{&#39;Coder&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;The code &quot;Hello, World!&quot; has been printed to the terminal.&#39;, name=&#39;Coder&#39;)]}}<br>----<br>{&#39;supervisor&#39;: {&#39;next&#39;: &#39;FINISH&#39;}}<br>----</pre><p><strong>Task 2: Write a research report on Himalayan Pandas.</strong></p><pre>for s in graph.stream(<br>    {&quot;messages&quot;: [HumanMessage(content=&quot;Write a brief research report on pikas.&quot;)]},<br>    {&quot;recursion_limit&quot;: 100},<br>):<br>    if &quot;__end__&quot; not in s:<br>        print(s)<br>        print(&quot;----&quot;)</pre><p>The output is:</p><pre>{&#39;supervisor&#39;: {&#39;next&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;## Research Report on Himalayan Pandas\n\n### Introduction\nHimalayan Pandas, also known as Red Pandas, are fascinating and adorable animals that inhabit the mountainous regions of the Himalayas. They are unique creatures with distinct features and behaviors that make them a subject of interest and concern for conservation efforts. This research report delves into the characteristics, habitat, behavior, and conservation status of Himalayan Pandas based on information gathered from various sources.\n\n### Characteristics and Habitat\nHimalayan Pandas are comprised of two subspecies - the Himalayan Red Panda (Ailurus fulgens fulgens) and the Chinese Red Panda (A. fulgens styani). The Himalayan Red Panda resides in the mountains of northern India, Tibet, Bhutan, and Nepal, while the Chinese Red Panda is found in China&#39;s Sichuan and Yunnan provinces. These pandas are known for their reddish-brown coats, kitten-like faces, and impressive acrobatic skills in climbing and swinging on trees in their Asian forest habitats. The Himalayan Red Panda has whiter faces compared to the Chinese Red Panda, which has redder fur and striped tail rings.\n\n### Behavior\nRed Pandas, including the Himalayan Pandas, are solitary animals but display communication through tail arching, head bobbing, squealing, and unique sounds like a “huff-quack.” They are agile climbers and spend much of their time in trees, where their red fur blends with the moss clumps on their tree homes, providing camouflage against predators like snow leopards and jackals.\n\n### Conservation Status\nThe International Union for the Conservation of Nature (IUCN) considers Red Pandas, including the Himalayan Pandas, as endangered species. The Himalayan Red Panda, in particular, faces threats due to low genetic diversity and a small population size. Habitat destruction, fragmentation, deforestation, illegal activities, and human disturbances are major factors contributing to the decline in their numbers. Conservation efforts are crucial to protect these unique and charming creatures from extinction.\n\n### Conclusion\nHimalayan Pandas, with their striking appearance and intriguing behaviors, are a valuable part of the ecosystem in the Himalayan region. It is essential to raise awareness about their conservation needs and take action to safeguard their habitats and well-being. By understanding and appreciating these remarkable animals, we can contribute to their preservation and ensure a sustainable future for the Himalayan Pandas and their natural environment.&quot;, name=&#39;Researcher&#39;)]}}<br>----<br>{&#39;supervisor&#39;: {&#39;next&#39;: &#39;FINISH&#39;}}<br>----</pre><p>Congratulations! You have created a multi-agent workflow which consists of a supervisor that can supervise the work of multiple agents and return the work to the user.</p><p><strong>Previous Articles:</strong></p><p><a href="https://medium.com/@vijaykumarkartha/begginers-guid-to-langchain-12ad239ee101">Beginner’s Guide to LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77">Beginner’s Guide To Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-conversational-retrieval-chain-using-langchain-3ddf1357f371">Beginner’s Guide to Conversational Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-creating-ai-agents-with-langchain-eaa5c10973e6">Beginner’s Guide to Creating AI Agents with LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/the-fascinating-mind-of-an-ai-agent-from-langchain-b54bcbdcf975">The Fascinating Mind of an AI Agent</a></p><p><a href="https://medium.com/@vijaykumarkartha/self-reflecting-ai-agents-using-langchain-d3a93684da92">Self-Reflecting AI Agents Using LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/multiple-ai-agents-creating-multi-agent-workflows-using-langgraph-and-langchain-0587406ec4e6">Multiple AI Agents: Creating Multi-Agent Workflows Using LangChain and LangGraph</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=315abbbd4133" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Self-Reflecting AI Agents Using LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/self-reflecting-ai-agents-using-langchain-d3a93684da92?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3a93684da92</guid>
            <category><![CDATA[self-reflecting-ai-agents]]></category>
            <category><![CDATA[llm-agent]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Fri, 03 May 2024 06:22:48 GMT</pubDate>
            <atom:updated>2024-05-03T06:22:48.009Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wx1kbIImZu57hR0w" /><figcaption>Source: Linkedin</figcaption></figure><p>In our last article, we saw how an AI agent “thinks” before taking the next step. The thinking process continues till it decides it has completed the task and the flow of thinking stops.</p><p>In this article, we will look at enabling self-reflection inn AI Agents. We have given AI agents the power to “think” and “act”. Now, we will see how it can be made to self-reflect on its own action, and then decide on the next action.</p><h3>Self-Reflection</h3><p>To understand this better, let’s understand the concept of self-reflection a little more. As humans, when we self-reflect, we reflect on our action, critique it, learn from it, and then formulate our next step and action. We continue to self-reflect on the next actions till we feel we have accomplished the task as best as it can be done.</p><p>Thinking is a linear process, whereas self-reflection is a cyclical process. In thinking, we think and do an action, and think again and do the next action and so on. The next thought is not taken after reviewing the previous thought. But, in self-reflection, we think, do an action, think about the action we did, and use the feedback to think about the next action and this cycle of feedback continues till we decide that we have achieved the task to the best of our abilities.</p><h3>Thinking and Self-Reflection</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*52aP-p2IwG9bmy0j" /><figcaption>The Thinking Process and Self-Reflection Process</figcaption></figure><p>Thus, in order to get an AI agent to self-reflect, we need to create a cycle of input where the thought and action of the AI agent is fed back to the AI agent to be reflected upon, before generating the next thought and action.</p><p>This can be illustrated as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*FxlOsXP3hPYDp_sQ" /><figcaption>Credit: LangChain Blog</figcaption></figure><h3>LangGraph</h3><p>This is achieved in LangChain through LangGraph. LangGraph is a library for building stateful, multi-actor applications. A state can be a list of chat messages. And, the actor is an LLM chain. Thus, LangGraph has the ability to coordinate multiple LLMs across multiple steps of computations in a cyclical manner. A simple example is shown below with an LLM that acts as a generator and as a reflector, each connnected to a state represented by the messages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RJfjKg_LaPsVeJ2M" /><figcaption>A simple example of LangGraph</figcaption></figure><p>The main use of LangGraph is for adding cycles to an LLM application. Cycles are important for agent-like behaviours, where you call an LLM in a loop, asking it what action to take next. And, that is precisely what is needed for self-reflection.</p><h3>Creating a Self-Reflection Loop</h3><p>Let’s use LangGraph to create a self-reflection loop which will loop 6 times before outputting the final response. This simple type of reflection can improve performance by giving the LLM multiple attempts at refining its output. The same LLM is used as a generator and a reflector by adopting a different persona while critiquing the output.</p><p>First, we need to install langgraph from the terminal.</p><pre>pip install langgraph</pre><p>Next, we import MessageGraph to create the cyclical loop, and END to end the cyclical loop.</p><pre>from langgraph.graph import END, MessageGraph</pre><p>We then create a self-reflection loop that will consist of the actors (llm chains) and states (list of messages). Each actor is called a node.</p><h3>Generation Node</h3><p>First, we create the generation_node. The generation_node invokes a generate chain comprising of an llm and a generation prompt. We will create the generate chain later.</p><pre>from langchain_core.messages import AIMessage, BaseMessage, HumanMessage<br>from typing import List, Sequence</pre><pre>async def generation_node(state: Sequence[BaseMessage]):<br>	return await generate.ainvoke({&quot;messages&quot;: state})</pre><h3>Reflection Node</h3><p>Next, we create the reflection_node. The reflection_node invokes a reflect chain comprising of an llm and a reflection prompt. We will create the reflect chain later. It then returns the output of the reflect chain as a Human Message back to the generator. The generator will then respond as if the message from the reflection node is a user input.</p><pre>async def reflection_node(messages: Sequence[BaseMessage]) -&gt; List[BaseMessage]:<br>    # Other messages we need to adjust<br>    cls_map = {&quot;ai&quot;: HumanMessage, &quot;human&quot;: AIMessage}<br>    # First message is the original user request. We hold it the same for all nodes<br>    translated = [messages[0]] + [<br>        cls_map[msg.type](content=msg.content) for msg in messages[1:]<br>    ]<br>    res = await reflect.ainvoke({&quot;messages&quot;: translated})<br>    # We treat the output of this as human feedback for the generator<br>    return HumanMessage(content=res.content)</pre><h3>Self-reflection Loop Graph</h3><p>Now, we create a builder which is a MessageGraph from LangGraph to which we add the above two nodes, naming them as “reflect” and “generate”. The builder is essentially a self-reflection loop. We also set a entry point which will start the loop. For a self-reflection loop, the entry point will be set as “generator”.</p><pre>builder = MessageGraph()<br>builder.add_node(&quot;generate&quot;,generation_node)<br>builder.add_node(&quot;reflect&quot;,reflection_node)<br>builder.set_entry_point(&quot;generate&quot;)</pre><p>We define a function should_continue that will end the loop after it has run 6 times.</p><pre>from typing import List, Sequence</pre><pre>def should_continue(state: List[BaseMessage]):<br>	if len(state) &gt; 6:<br>		return END<br>	return &quot;reflect&quot;</pre><p>To create a condition when the loop stops, we create a conditional edge containing the “generate” node along with the should_continue function.</p><pre>builder.add_conditional_edges(&quot;generate&quot;,should_continue)</pre><p>Now, we create the loop by connecting the “reflect” with the “generate”.</p><pre>builder.add_edge(&quot;reflect&quot;,&quot;generate&quot;)</pre><p>Finally, we create a graph by compiling the builder.</p><pre>graph = builder.compile()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aYgJOfKMCGjpkDz7" /><figcaption>The graph for self-reflection</figcaption></figure><p>So, all that we need to do now is to create the generate chain to be invoked by the “generator” and the reflect chain to be invoked by the “reflector”.</p><h3>Generate Chain</h3><p>The generate chain will generate the output for a task. So, we create a generation_prompt with the task in it.</p><pre>from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder<br>generation_prompt = ChatPromptTemplate.from_messages(<br>	[<br>		(<br>			&quot;system&quot;, &quot;You are an essay assistant tasked with writing excellent 5-paragraph essays.&quot;<br>			&quot;Generate the best essay possible for the user&#39;s request.&quot;<br>			&quot; If the user provides critique, respond with a revised version of your previous attempts.&quot;,<br>		),<br>		MessagesPlaceholder(variable_name=&quot;messages&quot;),<br>	]<br>)</pre><p>We create an llm</p><pre>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;&lt;Your API Key here&gt;&quot;)</pre><p>We then create the generate chain by chaining the prompt with the llm. This will be invoked by the “generator” node and provide the tasks for the reflector.</p><pre>generate = generation_prompt|llm</pre><h3>Reflect Chain</h3><p>Next, we create the reflection_prompt for the reflect chain.</p><pre>reflection_prompt = ChatPromptTemplate.from_messages(<br>	[<br>		(<br>			&quot;system&quot;,<br>            &quot;You are a teacher grading an essay submission. Generate critique and recommendations for the user&#39;s submission.&quot;<br>            &quot; Provide detailed recommendations, including requests for length, depth, style, etc.&quot;,<br>    ),<br>	  MessagesPlaceholder(variable_name=&quot;messages&quot;),<br>	]<br>)</pre><p>We then create the reflect chain by chaining the reflection_prompt with the llm. The reflect chain will be invoked by the “reflector” node.</p><pre>reflect = reflection_prompt | llm</pre><p>The reflect chain will review the output of the generator and provide suggestions to improve the output. This will be send as a Human Input to the generator, essentially making it work on its own input based on the suggestions given. That’s how self-reflection happens.</p><h3>Invoking the Self-Reflecting AI Agent</h3><p>We are done creating the self-reflecting AI agent. To test it, let’s insert a message into the graph to start the self-reflection loop and see the output.</p><pre>async for event in graph.astream(<br>	[<br>	HumanMessage(content=&quot;Generate an essay on the topicality of The Little Prince and its message in modern life&quot;<br>		)<br>	],<br>	):<br>	print(event)<br>	print(&quot;________&quot;)</pre><p>The output is as given below:</p><p>Generator Output 1:</p><pre>{&#39;generate&#39;: AIMessage(content=&#39;&quot;The Little Prince&quot; by Antoine de Saint-Exupéry continues to be a timeless classic that holds significant relevance in today\\&#39;s fast-paced and materialistic world. The story follows a young prince who travels from planet to planet, meeting various characters and learning essential life lessons along the way. Through its whimsical narrative, the novella conveys profound messages about love, friendship, and the importance of seeing beyond the surface to discover the true essence of things.\\n\\nIn modern life, where technology and busyness often dominate our daily routines, the message of slowing down and cherishing the simple things in life resonates deeply. The Little Prince\\&#39;s interactions with the fox, who imparts wisdom about the nature of relationships and the importance of investing time and care into them, serve as a poignant reminder of the value of meaningful connections in an increasingly disconnected world. In a society that prioritizes productivity and efficiency, the story encourages readers to pause, reflect, and appreciate the beauty of the present moment.\\n\\nFurthermore, The Little Prince\\&#39;s encounters with characters like the vain rose and the businessman highlight the pitfalls of materialism and the emptiness of pursuing wealth and status at the expense of human connection and emotional fulfillment. In a culture that often equates success with possessions and external achievements, the novella\\&#39;s emphasis on the intangible qualities of love, empathy, and understanding serves as a powerful counter-narrative that challenges prevailing societal norms.\\n\\nThe Little Prince\\&#39;s journey to Earth and his interactions with the narrator also underscore the universal themes of loneliness, longing, and the search for meaning in a vast and sometimes indifferent world. As individuals grapple with feelings of isolation and existential questions in today\\&#39;s complex and uncertain landscape, the novella\\&#39;s exploration of these existential themes offers solace and a sense of shared humanity that transcends time and cultural boundaries.\\n\\nIn conclusion, &quot;The Little Prince&quot; continues to captivate readers of all ages with its enduring message of compassion, connection, and the pursuit of deeper truths in a world often preoccupied with superficiality and material gain. By embracing the wisdom imparted by the little prince and his companions, readers can cultivate a greater appreciation for the beauty and complexity of life, fostering a more compassionate and fulfilling existence in the process. As we navigate the challenges of modern life, the lessons of The Little Prince serve as a guiding light, reminding us of the importance of love, friendship, and authenticity in a world that often demands conformity and compromise.&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 492, &#39;prompt_tokens&#39;: 69, &#39;total_tokens&#39;: 561}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, id=&#39;run-306f954e-d4ed-4331-82ad-c4079b3032d8-0&#39;)}</pre><p>Reflector Output 1:</p><pre>{&#39;reflect&#39;: HumanMessage(content=&#39;Your essay on the topicality of &quot;The Little Prince&quot; and its message in modern life is insightful and well-structured. You have effectively highlighted the timeless themes of the novella and their relevance in today\\&#39;s society. However, here are some recommendations to further enhance your essay:\\n\\n1. **Depth and Analysis**: While you touch upon various themes such as the importance of meaningful connections, the pitfalls of materialism, and the search for meaning, consider delving deeper into each theme. Provide examples from the novella or real-life situations to illustrate how these themes manifest in modern society. Analyze the impact of these themes on individuals and communities today.\\n\\n2. **Engagement with Counterarguments**: To add depth to your analysis, consider addressing potential counterarguments. For instance, some might argue that the novella\\&#39;s messages are idealistic and impractical in today\\&#39;s fast-paced world. Acknowledge such perspectives and provide a nuanced response, perhaps by offering practical ways in which the lessons of &quot;The Little Prince&quot; can be applied in contemporary life.\\n\\n3. **Literary Analysis**: Incorporate a more detailed literary analysis of Saint-Exupéry\\&#39;s writing style, symbolism, and use of allegory in conveying the novella\\&#39;s messages. Explore how these literary techniques contribute to the enduring appeal of &quot;The Little Prince&quot; and its ability to resonate with readers across generations.\\n\\n4. **Personal Reflection**: Consider integrating personal reflections or anecdotes to connect the themes of the novella to your own experiences or observations. Sharing personal insights can enhance the authenticity and impact of your essay.\\n\\n5. **Conclusion**: While your conclusion effectively sums up the key points discussed in the essay, consider ending with a thought-provoking statement or a call to action that encourages readers to reflect on how they can apply the lessons of &quot;The Little Prince&quot; in their own lives.\\n\\n6. **Length**: Expand on each point by providing more detailed explanations and examples. Aim for a word count that allows for a thorough exploration of the themes and their relevance in modern life.\\n\\nBy incorporating these recommendations, you can further enrich your essay and offer a more comprehensive analysis of the enduring message of &quot;The Little Prince&quot; in today\\&#39;s world.&#39;, id=&#39;2f512037-a482-487c-8236-ac3877413976&#39;)}</pre><p>Generator Output 2:</p><pre>{&#39;generate&#39;: AIMessage(content=&#39;&quot;The Little Prince&quot; by Antoine de Saint-Exupéry remains a poignant and thought-provoking tale that continues to resonate with readers across generations, offering timeless lessons on love, friendship, and the complexities of human nature. In today\\&#39;s fast-paced and materialistic society, the novella\\&#39;s messages hold particular relevance, urging individuals to pause, reflect, and seek deeper connections amidst the distractions of modern life.\\n\\nOne of the central themes of &quot;The Little Prince&quot; is the importance of meaningful connections and relationships. Through the little prince\\&#39;s interactions with characters like the fox, who imparts the wisdom of taming and cultivating relationships, readers are reminded of the value of investing time and care into building genuine connections. In a world where surface-level interactions often prevail, the novella serves as a gentle reminder to prioritize quality over quantity in our relationships, fostering deeper bonds and emotional fulfillment.\\n\\nMoreover, Saint-Exupéry\\&#39;s exploration of the pitfalls of materialism and the emptiness of pursuing wealth and status at the expense of human connection remains strikingly relevant in today\\&#39;s consumer-driven culture. The encounters with characters like the vain rose and the businessman serve as cautionary tales, highlighting the hollowness of prioritizing material possessions over the intangible but invaluable qualities of love, empathy, and understanding. In a society that often equates success with external achievements, the novella challenges readers to look beyond material wealth and status symbols to find true fulfillment in authentic relationships and emotional connections.\\n\\nThe search for meaning and a sense of belonging in a vast and indifferent world is another theme that resonates deeply in &quot;The Little Prince.&quot; As individuals grapple with feelings of loneliness, isolation, and existential questions in today\\&#39;s complex and fast-paced world, the novella\\&#39;s exploration of these universal themes offers solace and a sense of shared humanity. The little prince\\&#39;s journey to Earth and his interactions with the narrator underscore the common human experience of longing for connection, understanding, and a sense of purpose in a world that can often feel isolating and overwhelming.\\n\\nIn conclusion, &quot;The Little Prince&quot; continues to captivate readers with its enduring message of compassion, connection, and the pursuit of deeper truths in a world that can often seem shallow and superficial. By embracing the lessons of the novella and reflecting on its themes of love, friendship, and authenticity, readers can cultivate a greater appreciation for the beauty and complexity of life, fostering a more meaningful and fulfilling existence in the process. As we navigate the challenges of modern life, the wisdom of &quot;The Little Prince&quot; serves as a guiding light, reminding us to cherish the simple joys, prioritize genuine connections, and seek out the deeper truths that give meaning to our lives.&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 543, &#39;prompt_tokens&#39;: 1010, &#39;total_tokens&#39;: 1553}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, id=&#39;run-548df4b8-afcc-47ad-a588-157c3377801b-0&#39;)}</pre><p>Reflector Output 2:</p><pre>{&#39;reflect&#39;: HumanMessage(content=&#39;Your essay on the topicality of &quot;The Little Prince&quot; and its message in modern life is well-crafted and effectively conveys the enduring relevance of the novella\\&#39;s themes. Here are some suggestions for further enhancing your essay:\\n\\n1. **Elaboration and Examples**: While you discuss the themes of meaningful connections, materialism, and the search for meaning, consider providing more detailed examples or anecdotes from the novella to support your points. For instance, you could delve deeper into specific scenes or dialogues that exemplify these themes and analyze their significance in the context of modern society.\\n\\n2. **Contemporary Relevance**: To strengthen the connection between the novella and modern life, consider incorporating real-world examples or current events that illustrate how the themes of &quot;The Little Prince&quot; manifest in today\\&#39;s society. This can help readers better understand the practical applications of the novella\\&#39;s messages in the context of contemporary challenges and experiences.\\n\\n3. **Critical Analysis**: Offer a more critical analysis of the themes presented in &quot;The Little Prince&quot; and their implications for individuals and society. Consider exploring potential contradictions or complexities within the novella\\&#39;s messages and discussing how they intersect with contemporary values, beliefs, and behaviors.\\n\\n4. **Cultural Context**: Discuss how cultural factors influence the interpretation and application of the novella\\&#39;s themes in different societies. Consider examining how cultural norms, values, and traditions shape individuals\\&#39; understanding of concepts such as love, friendship, and materialism, and how these factors impact the relevance of &quot;The Little Prince&quot; in diverse cultural contexts.\\n\\n5. **Language and Style**: Pay attention to the language and style of your essay to ensure clarity and coherence. Consider varying your sentence structure and incorporating transitions to improve the flow of your arguments. Additionally, aim to use descriptive language that engages the reader and evokes a deeper emotional response to the themes discussed.\\n\\n6. **Conclusion**: While your conclusion effectively summarizes the key points of your essay, you may consider ending with a compelling statement that reinforces the enduring significance of &quot;The Little Prince&quot; and encourages readers to reflect on how they can apply its lessons in their own lives.\\n\\nBy incorporating these recommendations, you can further enrich your essay and provide a more comprehensive analysis of the timeless messages of &quot;The Little Prince&quot; and their relevance in contemporary society.&#39;, id=&#39;80cf3efa-1a98-444f-a86e-03721d4c0ac6&#39;)}</pre><p>Generator Output 3:</p><pre>{&#39;generate&#39;: AIMessage(content=&#39;&quot;The Little Prince&quot; by Antoine de Saint-Exupéry remains a timeless masterpiece that continues to offer profound insights into the complexities of human relationships and the search for meaning in a fast-paced and materialistic world. Through its whimsical narrative and allegorical storytelling, the novella imparts enduring lessons on love, friendship, and the importance of looking beyond the superficial to discover the true essence of life.\\n\\nOne of the central themes of &quot;The Little Prince&quot; is the significance of meaningful connections and relationships. The little prince\\&#39;s interactions with the fox, who imparts the wisdom of taming and nurturing relationships, serve as a poignant reminder of the value of investing time and care into fostering genuine connections. For example, the fox\\&#39;s advice to the little prince about seeing with the heart rather than the eyes underscores the importance of emotional depth and understanding in relationships, a message that resonates deeply in a society often preoccupied with appearances and instant gratification.\\n\\nIn today\\&#39;s materialistic culture, where possessions and status symbols often take precedence over authentic human connections, the novella\\&#39;s critique of materialism remains as relevant as ever. The encounters with characters like the vain rose and the businessman underscore the emptiness of pursuing wealth and possessions at the expense of emotional fulfillment and empathy. By highlighting the fleeting nature of material wealth and the lasting impact of love and compassion, &quot;The Little Prince&quot; challenges readers to reevaluate their priorities and seek value in the intangible qualities that enrich their lives.\\n\\nFurthermore, the novella\\&#39;s exploration of the search for meaning and belonging in a vast and seemingly indifferent world strikes a universal chord in contemporary society. As individuals navigate feelings of isolation, existential questions, and the pursuit of purpose in a rapidly changing world, the little prince\\&#39;s quest for understanding and connection resonates as a timeless reminder of the human experience. The story\\&#39;s themes of loneliness, longing, and the universal need for companionship and purpose offer solace and reflection in a world that can often feel fragmented and chaotic.\\n\\nIn conclusion, &quot;The Little Prince&quot; endures as a literary classic that transcends time and culture, offering profound insights into the human condition and the enduring values of love, friendship, and authenticity. By embracing the lessons of the novella and reflecting on its timeless themes, readers can cultivate a deeper appreciation for the beauty and complexity of life, fostering meaningful connections and a sense of purpose in an increasingly fast-paced and materialistic world. As we navigate the challenges of modern life, the wisdom of &quot;The Little Prince&quot; serves as a guiding beacon, reminding us to cherish the simple joys, prioritize genuine relationships, and seek out the deeper truths that give meaning to our existence.&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 534, &#39;prompt_tokens&#39;: 2022, &#39;total_tokens&#39;: 2556}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, id=&#39;run-6ec5b8a3-0e60-4fda-b197-9096770a6cd6-0&#39;)}</pre><p>Reflector Output 3:</p><pre>{&#39;reflect&#39;: HumanMessage(content=&#39;Your essay on the topicality of &quot;The Little Prince&quot; and its message in modern life is well-written and engaging. You have effectively explored the central themes of the novella and their relevance in today\\&#39;s society. Here are some suggestions to further enhance your essay:\\n\\n1. **In-depth Analysis**: While you touch upon the themes of meaningful connections, materialism, and the search for meaning, consider delving deeper into each theme. Provide more specific examples from the novella to illustrate these concepts and analyze how they resonate with contemporary experiences. Offering a detailed analysis will enrich your essay and provide a deeper understanding of the novella\\&#39;s enduring messages.\\n\\n2. **Comparative Analysis**: Consider comparing and contrasting the themes presented in &quot;The Little Prince&quot; with other literary works or cultural artifacts that address similar topics. Exploring how different narratives approach themes of love, friendship, and materialism can offer a broader perspective on the universality of these concepts and their interpretations across different contexts.\\n\\n3. **Interdisciplinary Insights**: Incorporate insights from other disciplines, such as psychology, sociology, or philosophy, to enrich your analysis of the novella\\&#39;s themes. Drawing connections between the psychological aspects of relationships, societal influences on materialism, or existential theories on the search for meaning can provide a multidimensional perspective on the themes explored in &quot;The Little Prince.&quot;\\n\\n4. **Personal Reflection**: Consider sharing personal reflections or anecdotes that relate to the themes of the novella. Connecting the lessons of &quot;The Little Prince&quot; to your own experiences or observations can add a personal touch to your essay and create a more intimate connection with the reader.\\n\\n5. **Language and Style**: Pay attention to the language used in your essay to ensure clarity and coherence. Consider varying your sentence structures, using descriptive language to evoke imagery, and incorporating literary devices to enhance the eloquence of your writing. Strive for a balance between academic analysis and vivid storytelling to captivate the reader\\&#39;s attention.\\n\\n6. **Conclusion**: While your conclusion effectively summarizes the key points of your essay, consider ending with a thought-provoking statement or a call to action that prompts readers to reflect on the enduring lessons of &quot;The Little Prince&quot; and consider how they can apply them in their own lives.\\n\\nBy incorporating these recommendations, you can further elevate your essay and provide a comprehensive exploration of the timeless messages of &quot;The Little Prince&quot; and their significance in contemporary society.&#39;, id=&#39;96f11d9d-487f-425d-aa97-a97b4507cb5b&#39;)}</pre><p>Generator Output 4:</p><pre>{&#39;generate&#39;: AIMessage(content=&#39;&quot;The Little Prince&quot; by Antoine de Saint-Exupéry stands as a literary gem that continues to offer profound insights into the complexities of human relationships and the pursuit of meaning in a modern, fast-paced world. Through its allegorical storytelling and timeless themes, the novella imparts enduring lessons on love, friendship, and the intrinsic value of looking beyond the surface to uncover the essence of life.\\n\\nCentral to the narrative of &quot;The Little Prince&quot; is the theme of meaningful connections and relationships. The interactions between the little prince and characters like the fox exemplify the importance of investing time and care into nurturing genuine connections. For instance, the fox\\&#39;s wisdom about taming and bonding captures the essence of emotional depth and understanding in relationships, echoing a poignant message in a society often fixated on superficial interactions and fleeting connections.\\n\\nIn today\\&#39;s materialistic culture, where the pursuit of wealth and possessions often overshadows the significance of human connection, the novella\\&#39;s critique of materialism remains as pertinent as ever. The encounters with characters like the vain rose and the avaricious businessman serve as cautionary tales, underscoring the emptiness of prioritizing material wealth over empathy and emotional fulfillment. By shedding light on the transient nature of material possessions and the enduring impact of love and compassion, &quot;The Little Prince&quot; challenges readers to reassess their values and seek richness in the intangible qualities that enrich their lives.\\n\\nFurthermore, the novella\\&#39;s exploration of existential themes such as loneliness, longing, and the quest for purpose resonates deeply in contemporary society. As individuals grapple with feelings of isolation and the search for meaning in a world marked by rapid change and uncertainty, the little prince\\&#39;s journey serves as a universal reminder of the human experience. The story\\&#39;s themes of connection, introspection, and the pursuit of authenticity provide solace and contemplation in a world often characterized by noise and distraction.\\n\\nIn conclusion, &quot;The Little Prince&quot; endures as a timeless masterpiece that transcends generations, offering profound insights into the human spirit and the enduring values of love, friendship, and authenticity. By internalizing the lessons of the novella and reflecting on its universal themes, readers can foster a deeper appreciation for the intricacies of life, nurturing genuine connections and seeking out the profound truths that give meaning to existence. As we navigate the complexities of modern life, the wisdom of &quot;The Little Prince&quot; serves as a guiding compass, prompting us to cherish the simple joys, prioritize authentic relationships, and embark on a journey of self-discovery and connection in a world that beckons for depth and sincerity.&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 518, &#39;prompt_tokens&#39;: 3045, &#39;total_tokens&#39;: 3563}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, id=&#39;run-1fbf3665-3f03-42a3-97d5-83385882680b-0&#39;)}</pre><p>The generator output is reviewed by the reflector which gives suggestions and based on the suggestions, the generator improves the work. Congratulations! You have build a self-reflecting AI agent that reviews its own work and improves it.</p><h3>Previous Articles:</h3><p><a href="https://medium.com/@vijaykumarkartha/begginers-guid-to-langchain-12ad239ee101">Beginner’s Guide to LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77">Beginner’s Guide To Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-conversational-retrieval-chain-using-langchain-3ddf1357f371">Beginner’s Guide to Conversational Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-creating-ai-agents-with-langchain-eaa5c10973e6">Beginner’s Guide to Creating AI Agents with LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/the-fascinating-mind-of-an-ai-agent-from-langchain-b54bcbdcf975">The Fascinating Mind of an AI Agent</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3a93684da92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiple AI Agents: Creating Multi-Agent Workflows Using LangGraph and LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/multiple-ai-agents-creating-multi-agent-workflows-using-langgraph-and-langchain-0587406ec4e6?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/0587406ec4e6</guid>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[ai-multiple-agent]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[multi-agent-workflows]]></category>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Wed, 01 May 2024 16:23:51 GMT</pubDate>
            <atom:updated>2024-05-03T06:24:15.092Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*19WdU1AXe6P71hSs" /></figure><h3>Self-Reflection</h3><p>Self-reflecting AI agents use a generator to produce output and a reflector to review the generator’s work. The same Large Language Model (LLM) is used for both the generator and the reflector, but each has different prompts, resulting in a self-reflecting AI agent.</p><p>This method of using the same LLM in two different roles in a cyclical manner is facilitated by the LangGraph framework from LangChain.</p><h3>Multi-agent Workflows</h3><p>The LangGraph framework can also be used to create multi-agent workflows. Just like in the self-reflecting AI agent, the LLM can take on multiple roles, each acting as a different AI agent. This is the concept of multi-agents.</p><h3>Multi-Agent</h3><p>A multi-agent system involves connecting independent actors, each powered by a large language model, in a specific arrangement.</p><p>Each agent can have its own prompt, LLM, tools, and other custom code to collaborate with other agents. However, the same LLM can also assume different roles based on the prompts provided.</p><h3>Using LangGraph for Multi-Agent Workflows</h3><p>LangGraph is well-suited for creating multi-agent workflows because it allows two or more agents to be connected as a graph. Each agent is an independent actor, and the connections between agents are represented by edges. Each connecting edge can have a control condition that guides the flow of information from one agent to another. Each agent has a state that can be updated with information during each flow.</p><h3>Collaborative Multi-Agents</h3><p>Much like human collaboration, different AI agents in a collaborative multi-agent workflow communicate using a shared scratchpad of messages. This allows each agent to view other agents’ work and observe all the individual steps taken.</p><p>The various AI agents could be based on the same LLM but in different roles.</p><p>We’ll now create a multi-agent workflow for generating a chart of Malaysia’s GDP over the past five years.</p><p>This workflow would require a researcher to search the Internet and come up with the data of Malaysia’s GDP over the past five years, and a chart generator that uses Python code to generate the chart based on the data obtained by the researcher.</p><p>So, the agents are:</p><ol><li>Researcher — Uses an Internet search tool to research a question.</li><li>Chart Generator — Creates a chart by executing code with the data provided by the researcher.</li></ol><h3>Blueprint for the Graph</h3><p>We will construct a blueprint for a graph showing the agent nodes and the connections between them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ffzadZCKXJT7n4JaRVFvcQ.jpeg" /></figure><h3>Information Flow In the Graph</h3><p>A. The User sends the input to the Researcher.</p><p>B. The researcher sends the message to the router.</p><p>C. Based on the content of the message, the router either decides to call a tool, or sends a message to Chart Generator.</p><p>D. The tool does its work and sends the output to the Researcher. The Researcher may again call the tool or send a message to the Chart Generator. Accordingly, the router calls the tools or sends the message to the Chart Generator.</p><p>E. The Chart Generator sends the reply to the router which may call the tool or send the reply back to the researcher.</p><p>F. Finally, when either the Researcher or Chart Generator sends the message as Final Answer, then the router routes the Final Answer to the User.</p><p>Let’s now create a multi-agent workflow as per the above graph.</p><h3>Install Packages</h3><p>First, we need to install all the langchain packages.</p><pre>pip install langchain langchain_openai langgraph langchain_core langsmith langchain_experimental</pre><p>We also need to install some packages for storing data (pandas) and creating charts (matplotlib).</p><pre>pip install pandas matplotlib</pre><h3>Import libraries</h3><p>Next, we’ll import the libraries required for creating multi-agent workflows.</p><pre>import json</pre><pre>from langchain_core.messages import (<br>    AIMessage,<br>    BaseMessage,<br>    ChatMessage,<br>    FunctionMessage,<br>    HumanMessage,<br>)<br>from langchain.tools.render import format_tool_to_openai_function<br>from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder<br>from langgraph.graph import END, StateGraph<br>from langgraph.prebuilt.tool_executor import ToolExecutor, ToolInvocation</pre><h3>Create Tools</h3><h3>Tavily Search Tool</h3><p>Next, we’ll set up the tools. We need an Internet Search Tool. We’ll use tavily for that.</p><pre>import os<br>os.environ[&#39;TAVILY_API_KEY&#39;] = &quot;&lt;Your Tavily API Key here&gt;&quot;</pre><pre>from langchain_core.tools import tool<br>from langchain_community.tools.tavily_search import TavilySearchResults<br>tavily_tool = TavilySearchResults(max_results=5)</pre><h3>Python Tool</h3><p>We also need a tool to execute python code. We’ll use a tool named PythonREPL from langchain_experimental.utilities.</p><pre>from langchain_experimental.utilities import PythonREPL<br>from typing import Annotated</pre><pre>repl = PythonREPL()<br>@tool<br>def python_repl(code: Annotated[str, &quot;The python code to execute to generate your chart.&quot;]):<br>    &quot;&quot;&quot;Use this to execute python code. If you want to see the output of a value, you should print it out with `print(...)`. This is visible to the user.&quot;&quot;&quot;<br>    try:<br>        result = repl.run(code)<br>    except BaseException as e:<br>        return f&quot;Failed to execute. Error: {repr(e)}&quot;<br>    return f&quot;Succesfully executed:\\\\n`python\\\\\\\\n{code}\\\\\\\\n`\\\\nStdout: {result}&quot;</pre><p>We’ll put the tools into a list.</p><pre>tools = [tavily_tool, python_repl]</pre><h3>Create State</h3><p>Next, we’ll create the object for state. The state will be a list of messages that will be passed from one node to another.</p><pre>import operator<br>from typing import Annotated, List, Sequence, Tuple, TypedDict, Union</pre><pre>from langchain.agents import create_openai_functions_agent<br>from langchain.tools.render import format_tool_to_openai_function<br>from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder</pre><pre>from langchain_openai import ChatOpenAI<br>from typing_extensions import TypedDict</pre><pre># This defines the object that is passed between each node<br># in the graph. We will create different nodes for each agent and tool<br>class AgentState(TypedDict):<br>    messages: Annotated[Sequence[BaseMessage], operator.add]<br>    sender: str</pre><h3>Create Tool Node</h3><p>We will use each tool based on the content of the message. We’ll create a tool_node function that will execute a tool based on the message.</p><pre>tool_executor = ToolExecutor(tools)</pre><pre>def tool_node(state):<br>    &quot;&quot;&quot;This runs tools in the graph</pre><pre>    It takes in an agent action and calls that tool and returns the result.&quot;&quot;&quot;<br>    messages = state[&quot;messages&quot;]<br>    # Based on the continue condition<br>    # we know the last message involves a function call<br>    last_message = messages[-1]<br>    # We construct an ToolInvocation from the function_call<br>    tool_input = json.loads(<br>        last_message.additional_kwargs[&quot;function_call&quot;][&quot;arguments&quot;]<br>    )<br>    # We can pass single-arg inputs by value<br>    if len(tool_input) == 1 and &quot;__arg1&quot; in tool_input:<br>        tool_input = next(iter(tool_input.values()))<br>    tool_name = last_message.additional_kwargs[&quot;function_call&quot;][&quot;name&quot;]<br>    action = ToolInvocation(<br>        tool=tool_name,<br>        tool_input=tool_input,<br>    )<br>    # We call the tool_executor and get back a response<br>    response = tool_executor.invoke(action)<br>    # We use the response to create a FunctionMessage<br>    function_message = FunctionMessage(<br>        content=f&quot;{tool_name} response: {str(response)}&quot;, name=action.tool<br>    )<br>    # We return a list, because this will get added to the existing list<br>    return {&quot;messages&quot;: [function_message]}</pre><h3>Create the Router</h3><p>Now that we’ve set up the tool node, let’s create the router that will decide where to send information based on the content of the message from state.</p><pre># Either agent can decide to end<br>def router(state):<br>    # This is the router<br>    messages = state[&quot;messages&quot;]<br>    last_message = messages[-1]<br>    if &quot;function_call&quot; in last_message.additional_kwargs:<br>        # The previus agent is invoking a tool<br>        return &quot;call_tool&quot;<br>    if &quot;FINAL ANSWER&quot; in last_message.content:<br>        # Any agent decided the work is done<br>        return &quot;end&quot;<br>    return &quot;continue&quot;</pre><p>Next, we set up the AI agents researcher and chart generator.</p><h3>Create the LLM</h3><p>First, let’s create the llm.</p><pre>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;&lt;Your API Key here&gt;&quot;)</pre><h3>Create Agents</h3><p>First, we create a create_agent function that will create an agent and append system messages provided to the function. We will use this function later to create the researcher and chart generator.</p><pre>import json</pre><pre>from langchain_core.messages import (<br>    AIMessage,<br>    BaseMessage,<br>    ChatMessage,<br>    FunctionMessage,<br>    HumanMessage,<br>)<br>from langchain_core.utils.function_calling import convert_to_openai_function<br>from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder<br>from langgraph.graph import END, StateGraph<br>from langgraph.prebuilt.tool_executor import ToolExecutor, ToolInvocation</pre><pre>def create_agent(llm, tools, system_message: str):<br>    &quot;&quot;&quot;Create an agent.&quot;&quot;&quot;<br>    functions = [convert_to_openai_function(t) for t in tools]</pre><pre>    prompt = ChatPromptTemplate.from_messages(<br>        [<br>            (<br>                &quot;system&quot;,<br>                &quot;You are a helpful AI assistant, collaborating with other assistants.&quot;<br>                &quot; Use the provided tools to progress towards answering the question.&quot;<br>                &quot; If you are unable to fully answer, that&#39;s OK, another assistant with different tools &quot;<br>                &quot; will help where you left off. Execute what you can to make progress.&quot;<br>                &quot; If you or any of the other assistants have the final answer or deliverable,&quot;<br>                &quot; prefix your response with FINAL ANSWER so the team knows to stop.&quot;<br>                &quot; You have access to the following tools: {tool_names}.\\\\n{system_message}&quot;,<br>            ),<br>            MessagesPlaceholder(variable_name=&quot;messages&quot;),<br>        ]<br>    )<br>    prompt = prompt.partial(system_message=system_message)<br>    prompt = prompt.partial(tool_names=&quot;, &quot;.join([tool.name for tool in tools]))<br>    return prompt | llm.bind_functions(functions)</pre><p>Next, we create an agent_node function that will do the task of appending messages and the sender’s name. This will be used for routing purposes.</p><pre># Helper function to create a node for a given agent<br>def agent_node(state, agent, name):<br>    result = agent.invoke(state)<br>    # We convert the agent output into a format that is suitable to append to the global state<br>    if isinstance(result, FunctionMessage):<br>        pass<br>    else:<br>        result = HumanMessage(**result.dict(exclude={&quot;type&quot;, &quot;name&quot;}), name=name)<br>    return {<br>        &quot;messages&quot;: [result],<br>        # Since we have a strict workflow, we can<br>        # track the sender so we know who to pass to next.<br>        &quot;sender&quot;: name,<br>    }</pre><p>Now, we create the researcher agent and the chart generator agent.</p><pre>research_agent= create_agent(<br>    llm,<br>    [tavily_tool],<br>    system_message=&quot;You should provide accurate data for the chart generator to use.&quot;,<br>)</pre><pre>chart_agent= create_agent(<br>    llm,<br>    [python_repl],<br>    system_message=&quot;Any charts you display will be visible by the user.&quot;,<br>)</pre><h3>Create Researcher Node and Chart Node From Agent Node</h3><p>Then, we create the research_node function and the chart_node function from the research_agent and chart_agent functions. We use the functools library to create a function from another function agent_node.</p><pre>import functools<br>research_node= functools.partial(agent_node, agent=research_agent, name=&quot;Researcher&quot;)</pre><pre>chart_node= functools.partial(agent_node, agent=chart_agent, name=&quot;Chart Generator&quot;)</pre><h3>Create Graph</h3><p>We can now put all the nodes which includes the research_node, chart_node, and tool_node into a graph named workflow. We also add the conditional edges that will route messages as per the conditions fulfilled. We finally set the Researcher node to be the entry point. So, the user input will go first to the researcher. At the end, we compile the graph.</p><pre>workflow= StateGraph(AgentState)</pre><pre>workflow.add_node(&quot;Researcher&quot;, research_node)<br>workflow.add_node(&quot;Chart Generator&quot;, chart_node)<br>workflow.add_node(&quot;call_tool&quot;, tool_node)</pre><pre>workflow.add_conditional_edges(<br>    &quot;Researcher&quot;,<br>    router,<br>    {&quot;continue&quot;: &quot;Chart Generator&quot;, &quot;call_tool&quot;: &quot;call_tool&quot;, &quot;end&quot;: END},<br>)<br>workflow.add_conditional_edges(<br>    &quot;Chart Generator&quot;,<br>    router,<br>    {&quot;continue&quot;: &quot;Researcher&quot;, &quot;call_tool&quot;: &quot;call_tool&quot;, &quot;end&quot;: END},<br>)</pre><pre>workflow.add_conditional_edges(<br>    &quot;call_tool&quot;,<br># Each agent node updates the &#39;sender&#39; field# the tool calling node does not, meaning<br># this edge will route back to the original agent# who invoked the tool<br>		lambda x: x[&quot;sender&quot;],<br>    {<br>        &quot;Researcher&quot;: &quot;Researcher&quot;,<br>        &quot;Chart Generator&quot;: &quot;Chart Generator&quot;,<br>    },<br>)<br>workflow.set_entry_point(&quot;Researcher&quot;)<br>graph= workflow.compile()</pre><h3>Invoking the Multi-Agent Workflows</h3><p>We are now ready to invoke the multi-agent workflow.</p><pre>for s in graph.stream(<br>    {<br>        &quot;messages&quot;: [<br>            HumanMessage(<br>                content=&quot;Fetch the Malaysia&#39;s GDP over the past 5 years,&quot;<br>                &quot; then draw a line graph of it.&quot;<br>                &quot; Once you code it up, finish.&quot;<br>            )<br>        ],<br>    },<br>    # Maximum number of steps to take in the graph<br>    {&quot;recursion_limit&quot;: 150},<br>):<br>    print(s)<br>    print(&quot;----&quot;)</pre><p>The output is:</p><pre>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;&#39;, additional_kwargs={&#39;function_call&#39;: {&#39;arguments&#39;: &#39;{&quot;query&quot;:&quot;Malaysia GDP by year&quot;}&#39;, &#39;name&#39;: &#39;tavily_search_results_json&#39;}}, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 22, &#39;prompt_tokens&#39;: 221, &#39;total_tokens&#39;: 243}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;function_call&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-313ac64c-fed0-4505-b00d-715c7ff0aa59-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;call_tool&#39;: {&#39;messages&#39;: [FunctionMessage(content=&quot;tavily_search_results_json response: [{&#39;url&#39;: &#39;&lt;https://data.worldbank.org/indicator/NY.GDP.MKTP.KD.ZG?locations=MY&gt;&#39;, &#39;content&#39;: &#39;GDP growth (annual %) - Malaysia from The World Bank: Data.&#39;}, {&#39;url&#39;: &#39;&lt;https://tradingeconomics.com/malaysia/gdp&gt;&#39;, &#39;content&#39;: &#39;GDP in Malaysia averaged 109.79 USD Billion from 1960 until 2022, reaching an all time high of 407.03 USD Billion in 2022 and a record low of 1.90 USD Billion\\\\xa0...&#39;}, {&#39;url&#39;: &#39;&lt;https://www.macrotrends.net/global-metrics/countries/MYS/malaysia/gdp-gross-domestic-product&gt;&#39;, &#39;content&#39;: &#39;Malaysia gdp for 2019 was $365.18B, a 1.78% increase from 2018. Download Historical Data Save as Image\\\\xa0...&#39;}, {&#39;url&#39;: &#39;&lt;https://data.worldbank.org/indicator/NY.GDP.MKTP.CD?locations=MY&gt;&#39;, &#39;content&#39;: &#39;1960 1980 2000 2020 0.00 225b 450b Malaysia. 1960 - 2022. 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007\\\\xa0...&#39;}, {&#39;url&#39;: &#39;&lt;https://www.worldometers.info/gdp/malaysia-gdp/&gt;&#39;, &#39;content&#39;: &#39;Nominal (current) Gross Domestic Product (GDP) of Malaysia is $406,306,000,000 (USD) as of 2022. · Real GDP (constant, inflation adjusted) of Malaysia reached\\\\xa0...&#39;}]&quot;, name=&#39;tavily_search_results_json&#39;)]}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;&#39;, additional_kwargs={&#39;function_call&#39;: {&#39;arguments&#39;: &#39;{&quot;query&quot;:&quot;Malaysia GDP historical data&quot;}&#39;, &#39;name&#39;: &#39;tavily_search_results_json&#39;}}, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 22, &#39;prompt_tokens&#39;: 632, &#39;total_tokens&#39;: 654}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;function_call&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-442c7d99-8b3c-4a32-99ec-e0739ac059dd-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;call_tool&#39;: {&#39;messages&#39;: [FunctionMessage(content=&#39;tavily_search_results_json response: [{\\&#39;url\\&#39;: \\&#39;&lt;https://data.worldbank.org/indicator/NY.GDP.MKTP.KD.ZG?locations=MY&amp;view=chart\\&gt;&#39;, \\&#39;content\\&#39;: \\&#39;GDP growth (annual %) - Malaysia. World Bank national accounts data, and OECD National Accounts data files. License : CC BY-4.0. LineBarMap. Also Show Share Details. Label. 1961 - 2022.\\&#39;}, {\\&#39;url\\&#39;: \\&#39;&lt;https://tradingeconomics.com/malaysia/gdp\\&gt;&#39;, \\&#39;content\\&#39;: \\&#39;The Gross Domestic Product (GDP) in Malaysia was worth 407.03 billion US dollars in 2022, according to official data from the World Bank. The GDP value of Malaysia represents 0.17 percent of the world economy. This page provides - Malaysia GDP - actual values, historical data, forecast, chart, statistics, economic calendar and news.\\&#39;}, {\\&#39;url\\&#39;: \\&#39;&lt;https://www.macrotrends.net/global-metrics/countries/MYS/malaysia/gdp-growth-rate\\&gt;&#39;, \\&#39;content\\&#39;: \\&#39;Malaysia gdp growth rate for 2022 was 8.65%, a 5.35% increase from 2021. Malaysia gdp growth rate for 2021 was 3.30%, a 8.75% increase from 2020. Malaysia gdp growth rate for 2020 was -5.46%, a 9.87% decline from 2019. Malaysia gdp growth rate for 2019 was 4.41%, a 0.43% decline from 2018. Download Historical Data. Save as Image.\\&#39;}, {\\&#39;url\\&#39;: \\&#39;&lt;https://data.worldbank.org/indicator/NY.GDP.MKTP.CD?locations=MY&amp;view=chart\\&gt;&#39;, \\&#39;content\\&#39;: \\&#39;All Countries and Economies. Country. Most Recent Year. Most Recent Value(Millions) GDP (current US$) - Malaysia from The World Bank: Data.\\&#39;}, {\\&#39;url\\&#39;: \\&#39;&lt;https://www.macrotrends.net/global-metrics/countries/MYS/malaysia/gdp-gross-domestic-product\\&gt;&#39;, \\&#39;content\\&#39;: &quot;Malaysia gdp for 2022 was $407.03B, a 8.88% increase from 2021. Malaysia gdp for 2021 was $373.83B, a 10.78% increase from 2020. Malaysia gdp for 2020 was $337.46B, a 7.59% decline from 2019. Malaysia gdp for 2019 was $365.18B, a 1.78% increase from 2018. GDP at purchaser\\&#39;s prices is the sum of gross value added by all resident producers in the ...&quot;}]&#39;, name=&#39;tavily_search_results_json&#39;)]}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;Based on the search results, I found the GDP data for Malaysia for the past 5 years as follows:\\n\\n- 2018: $365.18 billion\\n- 2019: $337.46 billion\\n- 2020: $373.83 billion\\n- 2021: $407.03 billion\\n- 2022: $407.03 billion\\n\\nNow, I will proceed to create a line graph to visualize this data.\\nI will now generate a line graph to visualize Malaysia&#39;s GDP over the past 5 years.&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 113, &#39;prompt_tokens&#39;: 1210, &#39;total_tokens&#39;: 1323}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-e498d73b-c688-4e38-aa9b-b4cfa39c34c7-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;&#39;, additional_kwargs={&#39;function_call&#39;: {&#39;arguments&#39;: &#39;{&quot;code&quot;:&quot;import matplotlib.pyplot as plt\\\\n\\\\ndata = {\\\\n    \\&#39;Year\\&#39;: [2018, 2019, 2020, 2021, 2022],\\\\n    \\&#39;GDP (in billion USD)\\&#39;: [365.18, 337.46, 373.83, 407.03, 407.03]\\\\n}\\\\n\\\\nplt.figure(figsize=(10, 6))\\\\nplt.plot(data[\\&#39;Year\\&#39;], data[\\&#39;GDP (in billion USD)\\&#39;], marker=\\&#39;o\\&#39;, color=\\&#39;b\\&#39;, linestyle=\\&#39;-\\&#39;, linewidth=2)\\\\nplt.title(\\&#39;Malaysia GDP Over the Past 5 Years\\&#39;)\\\\nplt.xlabel(\\&#39;Year\\&#39;)\\\\nplt.ylabel(\\&#39;GDP (in billion USD)\\&#39;)\\\\nplt.grid(True)\\\\nplt.show()&quot;}&#39;, &#39;name&#39;: &#39;python_repl&#39;}}, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 175, &#39;prompt_tokens&#39;: 1338, &#39;total_tokens&#39;: 1513}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;function_call&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-11c4756f-097b-4733-8ecf-6091f5be5bd4-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}}<br>----</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*Sz0Eyg6eNKzOR7VNULwDNg.png" /></figure><pre>{&#39;call_tool&#39;: {&#39;messages&#39;: [FunctionMessage(content=&quot;python_repl response: Succesfully executed:\\\\n`python\\\\\\\\nimport matplotlib.pyplot as plt\\n\\ndata = {\\n    &#39;Year&#39;: [2018, 2019, 2020, 2021, 2022],\\n    &#39;GDP (in billion USD)&#39;: [365.18, 337.46, 373.83, 407.03, 407.03]\\n}\\n\\nplt.figure(figsize=(10, 6))\\nplt.plot(data[&#39;Year&#39;], data[&#39;GDP (in billion USD)&#39;], marker=&#39;o&#39;, color=&#39;b&#39;, linestyle=&#39;-&#39;, linewidth=2)\\nplt.title(&#39;Malaysia GDP Over the Past 5 Years&#39;)\\nplt.xlabel(&#39;Year&#39;)\\nplt.ylabel(&#39;GDP (in billion USD)&#39;)\\nplt.grid(True)\\nplt.show()\\\\\\\\n`\\\\nStdout: &quot;, name=&#39;python_repl&#39;)]}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;The line graph showing Malaysia&#39;s GDP over the past 5 years has been successfully generated. The graph displays the GDP (in billion USD) on the y-axis and the years from 2018 to 2022 on the x-axis. The GDP values are plotted as points connected by lines.\\n\\nIs there anything else you would like to analyze or any other assistance needed?&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 75, &#39;prompt_tokens&#39;: 1514, &#39;total_tokens&#39;: 1589}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-a6d15c89-6cb8-4f98-9cbb-67bb92214dc5-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;I have successfully generated the line graph showing Malaysia&#39;s GDP over the past 5 years. If you have any more data to analyze or need further assistance, feel free to let me know!&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 39, &#39;prompt_tokens&#39;: 1579, &#39;total_tokens&#39;: 1618}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-9c1709e6-f6ee-4a8c-b0ae-f363d65a5a2f-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;Great! If you have any more data to analyze or any other tasks you&#39;d like assistance with, feel free to ask. I&#39;m here to help!&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 32, &#39;prompt_tokens&#39;: 1634, &#39;total_tokens&#39;: 1666}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-1f869abb-930c-4b19-ab7e-c068a407f134-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;I&#39;m glad the line graph for Malaysia&#39;s GDP over the past 5 years was successfully generated. If you have any more data to analyze or any other tasks you&#39;d like assistance with, feel free to ask. I&#39;m here to help!&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 50, &#39;prompt_tokens&#39;: 1656, &#39;total_tokens&#39;: 1706}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-4c614614-5fc6-4d6b-abd8-5b1b85607f5a-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;Thank you! If you have any more data to analyze or if you need assistance with any other tasks in the future, feel free to reach out. Have a great day!&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 36, &#39;prompt_tokens&#39;: 1722, &#39;total_tokens&#39;: 1758}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-659e7259-1906-4c07-8bb5-53c8d6ac7dd2-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;Thank you! If you have any more data to analyze or if you need assistance with any other tasks in the future, feel free to reach out. Have a great day!&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 36, &#39;prompt_tokens&#39;: 1748, &#39;total_tokens&#39;: 1784}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-f87114d6-08aa-4a54-8490-1a1145a30263-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;Great collaboration! If you have any more questions or need assistance in the future, feel free to ask. Have a wonderful day!&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 27, &#39;prompt_tokens&#39;: 1800, &#39;total_tokens&#39;: 1827}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-b12ec2b6-61bb-485c-81cf-279ade302e02-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;Thank you! It was a pleasure collaborating with you. If you have any more questions or need assistance in the future, don&#39;t hesitate to ask. Have a wonderful day!&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 36, &#39;prompt_tokens&#39;: 1817, &#39;total_tokens&#39;: 1853}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-3c7cf207-ce49-416f-bb02-81a2a3410d5a-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;It was a pleasure collaborating with you! If you have any more questions or need assistance in the future, feel free to reach out. Have a great day!&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 33, &#39;prompt_tokens&#39;: 1869, &#39;total_tokens&#39;: 1902}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-1d5d0ed0-c20a-4745-a16f-6adcaa8c2c81-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;Likewise! If you ever have more questions or need help in the future, feel free to ask. Have a fantastic day!&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 27, &#39;prompt_tokens&#39;: 1892, &#39;total_tokens&#39;: 1919}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-bb88218f-ab2f-4ee5-b381-d75e1bd1d1cf-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;Likewise! If you ever have more questions or need help in the future, feel free to ask. Have a fantastic day!&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 27, &#39;prompt_tokens&#39;: 1935, &#39;total_tokens&#39;: 1962}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-50293d69-34ff-455d-8b34-3a20a0a1bb91-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}}<br>----<br>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;I&#39;m glad we were able to collaborate effectively! If you have any more questions or need assistance in the future, feel free to reach out. Have a wonderful day!&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 35, &#39;prompt_tokens&#39;: 1952, &#39;total_tokens&#39;: 1987}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-67208ed1-0d31-4b43-b2e8-92619b806deb-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}</pre><pre>{&#39;Researcher&#39;: {&#39;messages&#39;: [HumanMessage(content=&quot;Thank you! It was a pleasure collaborating with you. If you have any more questions or need assistance in the future, don&#39;t hesitate to ask. Have a wonderful day!&quot;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 36, &#39;prompt_tokens&#39;: 2087, &#39;total_tokens&#39;: 2123}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Researcher&#39;, id=&#39;run-bc8186f2-1f7e-477e-b1ff-0020c7171aa3-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Researcher&#39;}}<br>----<br>{&#39;Chart Generator&#39;: {&#39;messages&#39;: [HumanMessage(content=&#39;FINAL ANSWER&#39;, response_metadata={&#39;token_usage&#39;: {&#39;completion_tokens&#39;: 4, &#39;prompt_tokens&#39;: 2139, &#39;total_tokens&#39;: 2143}, &#39;model_name&#39;: &#39;gpt-3.5-turbo&#39;, &#39;system_fingerprint&#39;: &#39;fp_3b956da36b&#39;, &#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None}, name=&#39;Chart Generator&#39;, id=&#39;run-45177a17-7ae8-43b0-b73e-f9d3d5c5d456-0&#39;, tool_calls=[], invalid_tool_calls=[])], &#39;sender&#39;: &#39;Chart Generator&#39;}</pre><p>Congratulations! You have create a multi-agent workflow. Now, you are on the way to create multiple AI agents working together to achieve complex tasks.</p><p>Previous Articles:</p><p><a href="https://medium.com/@vijaykumarkartha/begginers-guid-to-langchain-12ad239ee101">Beginner’s Guide to LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77">Beginner’s Guide To Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-conversational-retrieval-chain-using-langchain-3ddf1357f371">Beginner’s Guide to Conversational Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-creating-ai-agents-with-langchain-eaa5c10973e6">Beginner’s Guide to Creating AI Agents with LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/the-fascinating-mind-of-an-ai-agent-from-langchain-b54bcbdcf975">The Fascinating Mind of an AI Agent</a></p><p><a href="https://medium.com/@vijaykumarkartha/self-reflecting-ai-agents-using-langchain-d3a93684da92">Self-Reflecting AI Agents Using LangChain</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0587406ec4e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Fascinating Mind of an AI Agent from LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/the-fascinating-mind-of-an-ai-agent-from-langchain-b54bcbdcf975?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/b54bcbdcf975</guid>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Wed, 01 May 2024 00:51:02 GMT</pubDate>
            <atom:updated>2024-05-01T00:51:02.686Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hXgTeg9jIaxl6M5ugoZ6yg.png" /><figcaption>Credit: Linkedin</figcaption></figure><p>In the last article, we saw a demonstration of an AI agent. There are multiple types of agents.</p><p>In the last article, we looked at an Open AI Functions agent. In this article, we will look at another type of agent which is called a ReAct agent. The “ReAct” here doesn’t stand for the React Javascript framework. It is a compination of Reason + Action = ReAct.</p><p>Let’s first create the ReAct agent and then we will learn how it works.</p><p>For this agent also, we will have an LLM, a prompt, and tools. Let’s get them ready.</p><h3>The LLM</h3><pre>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;Your OpenAI API key here&quot;)</pre><h3>The ReAct Agent Prompt</h3><p>For the ReAct agent, we will use another prompt that is suited for the mechanism that a ReAct agent works with.</p><p>It is called “hwchase17/react”.</p><pre>from langchain import hub<br>prompt = hub.pull(&quot;hwchase17/react&quot;)</pre><h3>Tools</h3><h3>Retriever Tool</h3><p>The retriever tool retrieves relevant data from a vector database containing data from an external source, in this example, a website, and uses that as context for any question related to the website. For more about retriever tool, read my previous article: <a href="https://www.linkedin.com/pulse/beginners-guide-retrieval-chain-using-langchain-vijaykumar-kartha-kuinc">Beginner’s Guide To Retrieval Chain Using LangChain</a></p><pre>from langchain_community.document_loaders import WebBaseLoader<br> loader = WebBaseLoader(&quot;&lt;https://www.dasa.org&gt;&quot;)</pre><pre>docs = loader.load()<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>text_splitter = RecursiveCharacterTextSplitter()<br>documents = text_splitter.split_documents(docs)<br>from langchain_openai import OpenAIEmbeddings<br>embeddings = OpenAIEmbeddings()<br>from langchain_community.vectorstores import FAISS<br>vector = FAISS.from_documents(documents, embeddings)<br>retriever = vector.as_retriever()</pre><pre>from langchain.tools.retriever import create_retriever_tool<br>retriever_tool = create_retriever_tool (retriever, &quot;DASA_search&quot;, &quot;Search for information about DASA. For any questions about DASA, you must use this tool&quot;)</pre><h3>Search Tool</h3><pre>import os<br>os.environ[&#39;TAVILY_API_KEY&#39;] = &quot;&lt;Your Tavily API Key here&gt;&quot;<br>from langchain_community.tools.tavily_search import TavilySearchResults<br>search = TavilySearchResults()</pre><p>We add all the tools into a tools list.</p><pre>tools = [retriever_tool, search]</pre><h3>Creating The ReAct Agent</h3><p>To create the ReAct agent, we import the create_react_agent library and the AgentExecutor library.</p><pre>from langchain.agents import create_react_agent<br>from langchain.agents import AgentExecutor</pre><p>Now, it is time to create the ReAct agent.</p><pre>agent = create_react_agent(llm,tools,prompt)<br>agent_executor=AgentExecutor(agent=agent,tools=tools,verbose=True)</pre><h3>Invoking the ReAct agent</h3><p>Now, our AI ReAct agent is ready to be invoked.</p><pre>agent_executor.invoke({&quot;input&quot;:&quot;What technological advancements have significantly impacted renewable energy production in the last decade?&quot;})</pre><h3>Output</h3><p>The output is:</p><blockquote>&gt; Entering new AgentExecutor chain…</blockquote><blockquote>I should search for information on recent technological advancements in renewable energy production. Action: tavily_search_results_json Action Input: recent technological advancements in renewable energy production</blockquote><blockquote>I should search for more specific information on each of the technological advancements mentioned in the search results. Action: tavily_search_results_json Action Input: super-efficient solar cells breakthrough technologies</blockquote><blockquote>I should also search for more information on other mentioned technological advancements like digital inclusion, clean energy technologies, and decarbonization. Action: tavily_search_results_json Action Input: digital inclusion initiatives</blockquote><blockquote>I should also search for more information on clean energy technologies and decarbonization. Action: tavily_search_results_json Action Input: clean energy technologies advancements</blockquote><blockquote>&gt; Finished chain.</blockquote><pre>{&#39;input&#39;: &#39;What technological advancements have significantly impacted renewable energy production in the last decade?&#39;,<br>&#39;output&#39;: &#39;Some of the technological advancements include super-efficient solar cells using perovskites, digital inclusion initiatives to bridge the digital divide, and advancements in clean energy technologies aimed at decarbonization.&#39;}</pre><h3>The Fascinating Mind of the ReAct Agent</h3><p>How exactly does the ReAct Agent think? To know the mind of the AI agent, we need to know the prompts send to the AI Agent.</p><p>To know what prompts are send to the ReAct agent, and the responses of the AI ReAct agent at each step, we use a tracing tool provided by LangChain called LangSmith.</p><p>First, get an API key from <a href="https://smith.langchain.com/">https://smith.langchain.com/</a>. Then, have the below code in your program:</p><pre>os.environ[&quot;LANGCHAIN_TRACING_V2&quot;]=&quot;true&quot;<br>os.environ[&quot;LANGCHAIN_API_KEY&quot;]=&quot;&lt;Your API Key here&gt;&quot;</pre><p>The traces of the actions of the AI agent will get stored in the default project at <a href="https://smith.langchain.com/">https://smith.langchain.com/</a>.</p><h3>The Magical Prompt that Makes the AI Agent Think</h3><p>Let’s look at the prompt that was sent to the AI agent.</p><blockquote>Answer the following questions as best you can. You have access to the following tools:<br>DASA_search: Search for information about DASA. For any questions about DASA, you must use this tool<br>tavily_search_results_json: A search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events. Input should be a search query.</blockquote><blockquote>Use the following format:</blockquote><blockquote>Question: the input question you must answer<br>Thought: you should always think about what to do<br>Action: the action to take, should be one of [DASA_search, tavily_search_results_json]<br>Action Input: the input to the action<br>Observation: the result of the action<br>... (this Thought/Action/Action Input/Observation can repeat N times)<br>Thought: I now know the final answer<br>Final Answer: the final answer to the original input question</blockquote><blockquote>Begin!</blockquote><blockquote>Question: What technological advancements have significantly impacted renewable energy production in the last decade?<br>Thought:</blockquote><p>The above prompt uses the prompt template from hwchase17/react. The prompt ends with:</p><p>Thought:</p><p>This indicates to the LLM that it has to output a thought first and then do an action using one of the tools based on this thought. And, this is how the AI agent is being made to think. It is being told to think first and then act!. And, lo, it does that as per the order.</p><p>Below are the traces of the AI actions collected from LangSmith.</p><h3>AI</h3><blockquote>I should search for information on recent technological advancements in renewable energy production.<br>Action: tavily_search_results_json<br>Action Input: recent technological advancements in renewable energy production</blockquote><p>The next prompt send to the AI agent is:</p><pre>Answer the following questions as best you can. You have access to the following tools:<br>DASA_search: Search for information about DASA. For any questions about DASA, you must use this tool<br>tavily_search_results_json: A search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events. Input should be a search query.<br>Use the following format:<br>Question: the input question you must answer<br>Thought: you should always think about what to do<br>Action: the action to take, should be one of [DASA_search, tavily_search_results_json]<br>Action Input: the input to the action<br>Observation: the result of the action<br>... (this Thought/Action/Action Input/Observation can repeat N times)<br>Thought: I now know the final answer<br>Final Answer: the final answer to the original input question<br>Begin!<br>Question: What technological advancements have significantly impacted renewable energy production in the last decade?<br>Thought:I should search for information on recent technological advancements in renewable energy production.<br>Action: tavily_search_results_json<br>Action Input: recent technological advancements in renewable energy production<br>Observation: [{&#39;url&#39;: &#39;&lt;https://www.weforum.org/agenda/2023/09/renewable-energy-innovations-climate-emergency/&gt;&#39;, &#39;content&#39;: &#39;The first alliance to accelerate digital inclusion\\nOctober 20, 2023\\nThe first alliance to accelerate digital inclusion\\nOctober 20, 2023\\nAbout Us\\nEvents\\nMedia\\nMore from the Forum\\nPartners &amp; Members\\nLanguage Editions\\nPrivacy Policy &amp; Terms of Service\\n© 2023 World Economic Forum Kate Whiting\\nNovember 8, 2023\\nAnnual Meeting 2023 | Cooperation in a Fragmented World\\nWe need to move beyond the idea that AI is the solution to everything, expert says\\nJessica Wanger\\nOctober 23, 2023\\n Image:\\xa0REUTERS/Nathan Frandino\\nListen to the article\\nHow is the World Economic Forum fighting the climate crisis?\\nCreate a free account and access your personalized content collection with our latest publications and analyses.\\n Andrea Willige\\nNovember 13, 2023\\nBreathing new life into the SDGs: What is the UN Summit of the Future in 2024?\\n The Agenda Weekly\\nA weekly update of the most important issues driving the global agenda\\nYou can unsubscribe at any time using the link in our emails.&#39;}, {&#39;url&#39;: &#39;&lt;https://www.iea.org/news/rapid-progress-of-key-clean-energy-technologies-shows-the-new-energy-economy-is-emerging-faster-than-many-think&gt;&#39;, &#39;content&#39;: &quot;Fossil Fuels\\nRenewables\\nElectricity\\nLow-Emission Fuels\\nTransport\\nIndustry\\nBuildings\\nEnergy Efficiency and Demand\\nCarbon Capture, Utilisation and Storage\\nDecarbonisation Enablers\\nGlobal Energy Transitions Stocktake\\nCritical Minerals\\nRussia&#39;s War on Ukraine\\nClimate Change\\nGlobal Energy Crisis\\nInvestment\\nSaving Energy\\nEnergy Security\\nNet Zero Emissions\\nEnergy Efficiency\\nData explorers\\nUnderstand and manipulate data with easy to use explorers and trackers\\nData sets\\nFree and paid data sets from across the energy system available for download\\nPolicies database\\nPast, existing or planned government policies and measures\\nChart Library\\nAccess every chart published across all IEA reports and analysis\\nWorld Energy Outlook 2023\\nFlagship report — October 2023\\nEnergy Efficiency 2023\\nFuel report — November 2023\\nLeveraging Fossil Fuel Capabilities for Clean Energy Transitions\\nAssessment of opportunities in Oman\\nCountry report — November 2023\\nNet Zero Roadmap: The pace of deployment of some clean energy technologies – such as solar PV and electric vehicles – shows what can be achieved with sufficient ambition and policy action, but faster change is urgently needed across most components of the energy system to achieve net zero emissions by 2050, according to the IEA’s latest evaluation of global progress.\\n The IEA also released today the newly redesigned Clean Energy Technology Guide, an interactive digital database that allows users to visualise the readiness and geographical distribution of more than 500 different innovative technologies or components across the global energy system, along with the accompanying Clean Energy Demonstration Projects Database.\\n Stronger policy support and greater investment are needed across a wide range of different technologies, in all regions of the world, to enable a broader and faster shift towards clean energy to keep net zero emissions by 2050 within reach.\\n While progress can be observed across all of the 50-plus components of the energy system evaluated in Tracking Clean Energy Progress, the majority are not yet on a path consistent with net zero emissions by 2050.&quot;}, {&#39;url&#39;: &#39;&lt;https://www.technologyreview.com/2024/01/08/1085124/super-efficient-solar-cells-breakthrough-technologies/&gt;&#39;, &#39;content&#39;: &#39;10 Breakthrough Technologies\\nby Emma Foehringer Merchant\\nShare\\nPopular\\nDeep Dive\\nClimate change and energy\\nWhat’s coming next for fusion research\\nA year ago, scientists generated net energy with a fusion reactor. Super-efficient solar cells: 10 Breakthrough Technologies 2024\\nSolar cells that combine traditional silicon with cutting-edge perovskites could push the efficiency of solar panels to new heights.\\n In May, UK-based Oxford PV said it had reached an efficiency of 28.6% for a commercial-size perovskite tandem cell, which is significantly larger than those used to test the materials in the lab, and it plans to deliver its first panels and ramp up manufacturing in 2024. The latest iteration of a legacy\\nAdvertise with MIT Technology Review\\n© 2024 MIT Technology Review\\nAbout\\nHelp Beyond Silicon, Caelux, First Solar, Hanwha Q Cells, Oxford PV, Swift Solar, Tandem PV\\n3 to 5 years\\nIn November 2023, a buzzy solar technology broke yet another world record for efficiency.&#39;}, {&#39;url&#39;: &#39;&lt;https://www.technologyreview.com/2024/04/24/1091129/fully-decarbonized-world-renewable-energy/&gt;&#39;, &#39;content&#39;: &#39;As more and more clean, renewable energy comes online, we need to continue with policies that support research and development on the new technologies required to recover all kinds of materials ...&#39;}, {&#39;url&#39;: &#39;&lt;https://www.sciencedirect.com/science/article/pii/S0960148121011162&gt;&#39;, &#39;content&#39;: &#39;This article presents some of the main findings from the SDEWES conferences of 2021 within the field of renewable energy. More specifically, results are summarized and contextualised within solar energy and thermal comfort, wind power resource assessment, and biogas and biomass resources and technology. The two last sections deal more broadly ...&#39;}]<br>Thought:</pre><p>In the second prompt, the result of the first prompt is appended as Observation: followed by Thought:</p><p>And, this prompts the LLM to respond with the next thought and action as below.</p><blockquote>I should search for more specific information on each of the technological advancements mentioned in the search results.<br>Action: tavily_search_results_json<br>Action Input: super-efficient solar cells breakthrough technologies</blockquote><p>This continues for three times. Below are the responses of the AI agent at each step.</p><blockquote>I should also search for more information on other mentioned technological advancements like digital inclusion, clean energy technologies, and decarbonization.<br>Action: tavily_search_results_json<br>Action Input: digital inclusion initiatives</blockquote><blockquote>I should also search for more information on clean energy technologies and decarbonization.<br>Action: tavily_search_results_json<br>Action Input: clean energy technologies advancements</blockquote><blockquote>I now know the technological advancements that have significantly impacted renewable energy production in the last decade.<br>Final Answer: Some of the technological advancements include super-efficient solar cells using perovskites, digital inclusion initiatives to bridge the digital divide, and advancements in clean energy technologies aimed at decarbonization.</blockquote><p>All these steps are recorded in a list named intermediate_steps. There are two types of steps send to intermediate_steps: AgentAction and AgentFinish. When the type is AgentFinish, then it means that AI agent has to come to the end of its task and the output is ready to be provided to the user.</p><h3>The Mystery Behind the AI Agent</h3><p>So, to recap, the mystery behind the actions of an AI agent lies in the prompt send to the AI Agent. It instructs the AI agent to think and act by using one of the tools, and repeat it N number of times. This makes the AI Agent to reason by generating a thought and then acting based on the thought.</p><p>Do our minds also work the same way? Do we also reason because we are told to? Or, is there a reasoning ability part from being told by others? Or, are we told so many times, that it gets hardwired into our brains. Can the same thing be done within an LLM? The fascination continues!</p><p>In the next article, we will look at another ability of AI agents which is to self-reflect, and see the mystery behind self-reflection.</p><p><strong>Previous Articles:</strong></p><p><a href="https://medium.com/@vijaykumarkartha/begginers-guid-to-langchain-12ad239ee101">Beginner’s Guide to LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77">Beginner’s Guide To Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-conversational-retrieval-chain-using-langchain-3ddf1357f371">Beginner’s Guide to Conversational Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-creating-ai-agents-with-langchain-eaa5c10973e6">Beginner’s Guide to Creating AI Agents with LangChain</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b54bcbdcf975" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beginner’s Guide to Creating AI Agents With LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/beginners-guide-to-creating-ai-agents-with-langchain-eaa5c10973e6?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/eaa5c10973e6</guid>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[ai-education]]></category>
            <category><![CDATA[ai-agent-tools]]></category>
            <category><![CDATA[openai-functions-agent]]></category>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Mon, 29 Apr 2024 23:38:10 GMT</pubDate>
            <atom:updated>2024-04-29T23:38:10.692Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p73svEilmgpS-Wle" /><figcaption>Credit: Linkedin</figcaption></figure><p>Now, we come to the most exciting part of using LangChain which is that of creating AI Agents.</p><p>In the previous articles, we learnt how to use chains in order to get an LLM to respond to our questions. We also learnt how to get the LLM to use external sources of data as context, as well as use the chat history as context for generating its response.</p><h3>Difference between Chains and AI Agents</h3><p>In chains, a sequence of actions is hardcoded (in code). But, in agents, the actions are not hardcoded. We let the language model decide which actions to take and in which order.</p><p>In an AI agent, we are equipping the LLM with the ability to reason and determine the next action. Soon, you will see how fascinating it is to observe the reasoning of an LLM.</p><h3>Tools</h3><p>AI Agents are provided with a set of tools. They are free to decide which tool they can use based on the tool’s description and the task at hand.</p><p>The retriever mentioned in the previous articles which searches a vector database for relevant pieces of information is a tool.</p><p>Another simple example of a tool is Web Search. There are multiple APIs available that allow AI Agents to search the web in order to find a particular piece of information based on the task given to them.</p><p>The <em>langchain_community.tools</em> package has a huge list of tools that can be provided to the AI Agent.</p><h3>Retriever Tool</h3><p>A retriever tool is used to retrieve relevant data from a vector database based on the user input and sent to an LLM as context for the user input. This is done when the source data is large and cannot be send as part of the user input in the prompt. In the below example of retriever tool, data from a website <a href="https://www.dasa.org/">https://www.dasa.org</a> is stored in a vector database, and the retriever tool is used to retrieve relevant information from the vector database to be passed to the LLM as context.</p><p>In order to provide the tools to the AI Agent, we first need to create the retriever and then import the tool.</p><p>To know more creating the retriever, please read my earlier article on <a href="https://www.linkedin.com/pulse/beginners-guide-retrieval-chain-using-langchain-vijaykumar-kartha-kuinc">Creating Retrieval Chains Using LangChain</a>.</p><pre>from langchain_community.document_loaders import WebBaseLoader<br>loader = WebBaseLoader(&quot;https://www.dasa.org&quot;)</pre><pre>docs = loader.load()<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>text_splitter = RecursiveCharacterTextSplitter()<br>documents = text_splitter.split_documents(docs)<br>from langchain_openai import OpenAIEmbeddings<br>embeddings = OpenAIEmbeddings()<br>from langchain_community.vectorstores import FAISS<br>vector = FAISS.from_documents(documents, embeddings)<br>retriever = vector.as_retriever()</pre><p>Let’s now import the create_retriever_tool package. We import it as below:</p><pre>from langchain.tools.retriever import create_retriever_tool</pre><p>Next, we create the tool by giving it a name and description.</p><pre>retriever_tool = create_retriever_tool (retriever, &quot;DASA_search&quot;, &quot;Search for information about DASA. For any questions about DASA, you must use this tool&quot;)</pre><h3>Web Search Tool</h3><p>Let’s create another tool that can be used to search the Internet. This can be done through the Tavily Search tool for which you need an API that you can get for free at <a href="http://tavily.com/">tavily.com</a>.</p><pre>import os<br>os.environ[&#39;TAVILY_API_KEY&#39;] = &quot;&lt;Your Tavily API Key here&gt;&quot;<br>from langchain_community.tools.tavily_search import TavilySearchResults<br>search = TavilySearchResults()</pre><p>Now, we have two tools that we can give to the AI Agent. Let us put them in a list.</p><pre>tools = [retriever_tool, search]</pre><h3>Create the AI Agent</h3><p>Now that we have the tools, we can create an agent to use them.</p><p>First, install langchain hub using the terminal.</p><pre>pip install langchainhub</pre><p>Install the langchain-openai package.</p><pre>pip install langchain-openai</pre><p>Next, we will get a predefined prompt from a prompt hub that will be useful to get the llm to act as an AI Agent.</p><pre>from langchain import hub<br>prompt = hub.pull(&quot;hwchase17/openai-functions-agent&quot;)</pre><p>Let’s learn more about this prompt that makes the llm act as an AI agent.</p><p>The prompt is pulled from a hub of prompts and is specifically designed for creating AI agents.</p><p>It consists of an Agent Scratch Pad which is used by the llm to think about possible actions and deciding on an action to proceed towards its goal.</p><p>We will cover the fascinating details about how AI agents think in the next article.</p><p>Now, let’s get an llm ready.</p><pre>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;Your API Key here&quot;)</pre><p>Next, let us import some libraries from LangChain to create agents and execute agents.</p><pre>from langchain.agents import create_openai_functions_agent<br>from langchain.agents import AgentExecutor</pre><p>We are now ready to create an AI agent. The AI agent needs an llm, tools and a prompt.</p><pre>agent = create_openai_functions_agent(llm,tools,prompt)<br>agent_executor=AgentExecutor(agent=agent,tools=tools,verbose=True)</pre><h3>Invoke the AI Agent</h3><p>All that is needed to be done is to invoke the AI Agent.</p><pre>agent_executor.invoke({&quot;input&quot;:&quot;What is the mission of DASA&quot;})</pre><p>The output is:</p><p>&gt; Entering new AgentExecutor chain…</p><p>Invoking: DASA_search with {‘query’: ‘mission of DASA’}</p><pre>{&#39;input&#39;: &#39;What is the mission of DASA&#39;,<br>&#39;output&#39;: &#39;The mission of DASA (DevOps Agile Skills Association) is to facilitate the journey towards flow, business agility, and value maximization. DASA aims to help organizations transform into high-performance digital organizations by prioritizing customer-focused innovation, fostering a culture of agility and teamwork, and emphasizing key principles crucial for effective adoption and transition to a DevOps-centric approach. DASA offers a portfolio of talent and guidance products to boost enterprise transformation success and provides a collaborative space for leaders and professionals to share ideas and adopt best practices in the field.&#39;}</pre><p>Based on the user input, the AI agent decided to use the retriever tool.</p><p>Let’s ask some other question not related to DASA</p><pre>agent_executor.invoke({&quot;input&quot;:&quot;What is weather in Kuala Lumpur?&quot;})</pre><p>The output is:</p><p>&gt; Entering new AgentExecutor chain…</p><p>Invoking: tavily_search_results_json with {‘query’: ‘weather in Kuala Lumpur’}</p><pre>{&#39;input&#39;: &#39;What is weather in Kuala Lumpur?&#39;,<br>&#39;output&#39;: &#39;The current weather in Kuala Lumpur is partly cloudy with a temperature of 29.0°C (84.2°F). The humidity is at 84% and the wind speed is 3.6 km/h coming from the east-southeast direction. The visibility is 10.0 km and the UV index is 7.0.&#39;}</pre><p>This time, the AI agent decided to use the Tavily Search tool to search the Internet instead of the retriever tool.</p><p>Congratulations! You have created a thinking AI agent that can decide on its own what step to take.</p><p>In the next article, we will have a peep into the “mind” of an AI Agent to know exactly what “thoughts” make the AI Agent decide on the next step.</p><h3>Previous Articles:</h3><p><a href="https://medium.com/@vijaykumarkartha/begginers-guid-to-langchain-12ad239ee101">Beginner’s Guide to LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77">Beginner’s Guide To Retrieval Chain From LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-conversational-retrieval-chain-using-langchain-3ddf1357f371">Beginner’s Guide to Conversational Retrieval Chain From LangChain</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eaa5c10973e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beginner’s Guide To Conversational Retrieval Chain Using LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/beginners-guide-to-conversational-retrieval-chain-using-langchain-3ddf1357f371?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ddf1357f371</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[conversational-retrieval]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[llm-applications]]></category>
            <category><![CDATA[chatbots]]></category>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Mon, 29 Apr 2024 00:45:36 GMT</pubDate>
            <atom:updated>2024-04-29T00:45:36.875Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2_n1oXU7lBmyLKHc" /></figure><p>In the last article, we created a retrieval chain that can answer only single questions. Let’s now learn about Conversational Retrieval Chain which will allows us to create chatbots that can answer follow up questions. This requires that the LLM has knowledge of the history of the conversation. LangChain provides us with Conversational Retrieval Chain that works not just on the recent input, but the whole chat history.</p><h3>Ingredients:</h3><p><strong>Chains:</strong> <em>create_history_aware_retriever, create_stuff_documents_chain, create_retrieval_chain</em></p><p><strong>llm</strong> — OpenAI</p><p><strong>Retriever</strong></p><p><strong>Prompts:</strong></p><pre>prompt_search_query = ChatPromptTemplate.from_messages([<br>MessagesPlaceholder(variable_name=&quot;chat_history&quot;),<br>(&quot;user&quot;,&quot;{input}&quot;),<br>(&quot;user&quot;,&quot;Given the above conversation, generate a search query to look up to get information relevant to the conversation&quot;)<br>)]</pre><pre>prompt_get_answer = ChatPromptTemplate.from_messages([<br>(&quot;system&quot;, &quot;Answer the user&#39;s questions based on the below context:\\n\\n{context}&quot;),<br>MessagesPlaceholder(variable_name=&quot;chat_history&quot;,<br>(&quot;user&quot;,&quot;{input}&quot;),<br>])</pre><h3>Creating the Conversational Retrieval Chain:</h3><h3>LLM</h3><p>First, let us have an llm created as mentioned in the previous articles.</p><pre># Import ChatOpenAI and create an llm with the Open AI API key<br>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;&lt;Your Open AI API key here&quot;)</pre><h3>Retriever</h3><p>We create a retriever with a vector store as below. You may refer to the previous article on Retrieval Chain to know more about the below code.</p><p>The below retriever first downloads data from a website <a href="https://www.dasa.org/">https://www.dasa.org</a> into a vector store and then retrieve relevant information from the vector store based on the user input.</p><pre>from langchain_community.document_loaders import WebBaseLoader<br>loader = WebBaseLoader(&quot;https://www.dasa.org&quot;)</pre><pre>docs = loader.load()<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>text_splitter = RecursiveCharacterTextSplitter()<br>documents = text_splitter.split_documents(docs)<br>from langchain_openai import OpenAIEmbeddings<br>embeddings = OpenAIEmbeddings()<br>from langchain_community.vectorstores import FAISS<br>vector = FAISS.from_documents(documents, embeddings)<br>retriever = vector.as_retriever()</pre><p>For the Retrieval chain, we got a retriever to fetch documents from the vector store relevant to the user input. For the Conversational retrieval chain, we have to get the retriever fetch documents relevant not only to the user input but also to the chat history. Therefore, the retriever needs to have a query based not only on the user input but also on the relevant documents from the chat history. In order to do this, we provide the LLM with the chat history and user input and ask it to derive a search query for the retriever to fetch the relevant data from the vector store.</p><h3>Prompt To Generate Search Query For Retriever</h3><p>The prompt contains the user input, the chat history, and a message to generate a search query.</p><p>We need to first import a special kind of prompt template called the <em>MessagesPlaceholder</em>. The <em>MessagesPlaceholder</em> is a prompt template that assumes that the variable provided to it as input is a list of messages. In our case, the variable is <em>chat_history</em>, which is the list of previous messages.</p><p>The <em>MessagesPlaceholder</em> is part of the prompt along with user input and user message.</p><pre>from langchain_core.prompts import ChatPromptTemplate<br>from langchain_core.prompts import MessagesPlaceholder<br><br>prompt_search_query = ChatPromptTemplate.from_messages([<br>MessagesPlaceholder(variable_name=&quot;chat_history&quot;),<br>(&quot;user&quot;,&quot;{input}&quot;),<br>(&quot;user&quot;,&quot;Given the above conversation, generate a search query to look up to get information relevant to the conversation&quot;)<br>])</pre><h3>Retriever Chain</h3><p>We use the <em>create_history_aware_retriever</em> chain to retrieve the relevant data from the vector store.</p><pre>from langchain.chains import create_history_aware_retriever</pre><p>The <em>create_history_aware_retriever</em> creates a chain that does the following steps:</p><ol><li>Sends a prompt to the LLM with the chat_history and user input to generate a search query for the retriever</li><li>The retriever uses the search query to obtain the relevant documents from the vector store.</li></ol><p>So, the inputs to the <em>create_history_aware_retriever</em> are the llm, retriever and the prompt.</p><pre>retriever_chain = create_history_aware_retriever(llm, retriever, prompt_search_query)</pre><p>The <em>retriever_chain</em> will return the retrieved documents from the vector store that are relevant to the user input and chat history.</p><h3>Prompt To Get Response From LLM Based on Chat History</h3><p>The next step is to send the retrieved documents from the vector store along with a prompt to the llm to get the response to the user input.</p><p>We create a prompt containing the context (retrieved documents from vector store), chat history and the user input.</p><pre>prompt_get_answer = ChatPromptTemplate.from_messages([<br>(&quot;system&quot;, &quot;Answer the user&#39;s questions based on the below context:\\n\\n{context}&quot;),<br>MessagesPlaceholder(variable_name=&quot;chat_history&quot;,<br>(&quot;user&quot;,&quot;{input}&quot;),<br>])</pre><h3>Document Chain</h3><p>Next, we create a chain using <em>create_stuff_documents_chain</em> which will send the prompt to the llm.</p><pre>from langchain.chains.combine_documents import create_stuff_documents_chain<br>document_chain=create_stuff_documents_chain(llm,prompt_get_answer)</pre><p>To recap, we now have a <em>retriever_chain</em> that retrieves the relevant data from vector store, and <em>document_chain</em> that sends the <em>chat_history</em>, relevant data and user input to the llm.</p><h3>Conversational Retrieval Chain</h3><p>So, in the final step, we combine <em>retriever_chain</em> and <em>document_chain</em> using <em>create_retrieval_chain</em> to create a Conversational retrieval chain.</p><pre>from langchain.chains import create_retrieval_chain<br>retrieval_chain = create_retrieval_chain(retriever_chain, document_chain)</pre><h3>Invoking the Chain</h3><p>All that is remaining is to invoke it.</p><p>To test it, we create a sample <em>chat_history</em> and then invoke the <em>retrieval_chain</em>.</p><pre>from langchain_core.messages import HumanMessage, AIMessage<br><br>chat_history = [HumanMessage(content=&quot;Does DASA allow anybody to be certified?&quot;, AIMessage(content=&quot;Yes&quot;)]<br>response = retrieval_chain.invoke({<br>&quot;chat_history&quot;:chat_history,<br>&quot;input&quot;:&quot;How?&quot;<br>})<br>print (response[&#39;answer&#39;])</pre><p>The output is:</p><pre>DASA offers certification programs for individuals who are interested in enhancing their skills and knowledge in DevOps principles. By enrolling in the certification programs, individuals can undergo training and assessment to become certified in various roles such as DASA DevOps Leader™, DASA DevOps Coach™, DASA DevOps Product Owner™, and more. The certification programs are designed to help individuals improve their capabilities and contribute to high-performance digital organizations.</pre><p>Thus, the output for the user input “How” has taken the chat history into account. You have successfully created a Conversational Retrieval Chatbot.</p><p>In the next article, we will look at creating:</p><p>AI Agent — Where the LLM decides what step to take</p><h3>Previous Articles:</h3><p><a href="https://medium.com/@vijaykumarkartha/begginers-guid-to-langchain-12ad239ee101">Beginner’s Guide to LangChain</a></p><p><a href="https://medium.com/@vijaykumarkartha/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77">Beginner’s Guide To Retrieval Chain From LangChain</a></p><p>If you like my articles, please follow me to read more articles on AI and AI agents.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ddf1357f371" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beginner’s Guide To Retrieval Chain From LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/beginners-guide-to-retrieval-chain-from-langchain-f307b1a20e77?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f307b1a20e77</guid>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Fri, 26 Apr 2024 14:08:32 GMT</pubDate>
            <atom:updated>2024-04-26T14:08:32.092Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous article, we saw how LangChain can be used to create a simple LLM chain with a prompt template. In this article, we will see how LangChain can be used as a Retrieval Chain when there is too much data to pass to the LLM as context. The source for the data can be anything: a SQL table, the Internet or documents.</p><p>The logic is as follows.</p><p>Instead of sending the complete data along with the user question to the LLM, we convert the data into vector embeddings and store them in a special kind of database called the vector store.</p><p>Every time, the user asks a question, a retriever will fetch only the relevant pieces of information from the vector store and pass it along with the user question as a prompt to the LLM.</p><h3>Vector Store and Vector Embeddings</h3><p>Let’s look at closely at what a vector store and vector embeddings are. A vector store is used to store data in the form of vector embeddings. Vector Embeddings are numerical representations of source data, that captures the meaning and relationship of words, phrases and other data types.</p><p>For example, the vector embeddings for “dog” and “puppy” would be close together because they share a similar meaning and often appear in similar contexts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0-d589LapP3rNuEd" /></figure><p>Source: LangChain</p><p>When user asks a question, the retriever creates a vector embedding of the user question and then retrieves only those vector embeddings from the vector store that are ‘most similar’ to the vector embedding of the user question. This, therefore, prevents the need for the entire source data to be sent to the LLM each time the user asks a question.</p><p>For our example, let’s say we want a website to be the context for the user question to the LLM.</p><h3>Loading Data From Website</h3><p>First, we need to obtain the data from the website. To do this, we will use the WebBase Loader.</p><p>This requires installing BeautifulSoup library which is used to retrieve html data from web pages.</p><pre>pip install beautifulsoup4</pre><p>After that, we use WebBaseLoader to extract all the data from the website.</p><pre>from langchain_community.document_loaders import WebBaseLoader<br>loader = WebBaseLoader(&quot;https://www.dasa.org&quot;)</pre><pre>docs = loader.load()</pre><p>Now, we have the data from the website <a href="https://www.dasa.org/">https://www.dasa.org</a> as unstructured textual data in “docs”.</p><h3>Splitting Data Into Fragments</h3><p>In order to create vector embeddings, we need to split the textual data into fragments based on number of characters. For this, we will use <a href="https://dev.to/eteimz/understanding-langchains-recursivecharactertextsplitter-2846">RecursiveCharacterTextSplitter</a>. The default fragment size is 1000 characters.</p><pre>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>text_splitter = RecursiveCharacterTextSplitter()<br>documents = text_splitter.split_documents(docs)</pre><p>Now, we have the split text stored in “documents”.</p><p>Next, we need to convert the split text into vector embeddings.</p><h3>Converting Text Into Vector Embeddings</h3><p>For creating vector embeddings, we can use an embedding model from OpenAIEmbeddings</p><pre>from langchain_openai import OpenAIEmbeddings<br>embeddings = OpenAIEmbeddings()</pre><h3>Storing in a Vector Store</h3><p>Next, we need a vector store to store the vector embeddings. We can use FAISS, a vector store developed by Facebook.</p><p>First, from the terminal, we need to install the required package to work with the vector store.</p><pre>pip install faiss-cpu</pre><p>Then, we import the vector store.</p><pre>from langchain_community.vectorstores import FAISS</pre><p>Now, we convert the split text “documents” into vector embeddings using the “embeddings” algorithm and put them into the vector store FAISS.</p><pre>vector = FAISS.from_documents(documents, embeddings)</pre><p>The process we have done till now is visually represented below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Df5pR3qxIzLcJayO" /></figure><p>Process of creating vector embeddings and inserting into vector store</p><h3>Creating a Retrieval Chain</h3><p>Now that we have the data in the vector store, let’s create a retrieval chain. For the retrieval chain, we need a prompt.</p><p>The prompt will have the retrieved data and the user question. To do this, we use a prompt template.</p><pre># Import ChatOpenAI and create an llm with the Open AI API key<br>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;&lt;Your Open AI API Key here&gt;&quot;)</pre><pre># Create a prompt template<br>from langchain_core.prompts import ChatPromptTemplate<br>prompt = ChatPromptTemplate.from_template(&quot;&quot;&quot;Answer the following question based only on the provided context:<br>&lt;context&gt;<br>{context}<br>&lt;/context&gt;<br>Question: {input}&quot;&quot;&quot;)</pre><p>Here, “context” is the retrieved data, and “input” is the user question. Remember to insert your OpenAI API Key in the code.</p><p>We use a document chain called create_stuff_documents_chain to send the prompt to the llm.</p><pre>from langchain.chains.combine_documents import create_stuff_documents_chain<br>document_chain = create_stuff_documents_chain(llm,prompt)</pre><p>All that is left now is to retrieve the data that is closely related to the user question from the vector store and provide it to the LLM as part of the prompt. This is illustrated in the figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*M0xAx-3vBRhZ5leU" /></figure><p>Sending the prompt with retrieved data</p><p>This is done using a retriever and a retrieval chain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pAp57meAixM9ixH2" /></figure><pre>retriever = vector.as_retriever()<br>from langchain.chains import create_retrieval_chain<br>retrieval_chain = create_retrieval_chain(retriever, document_chain)</pre><p>Finally, we can now invoke this chain.</p><pre>response = retrieval_chain.invoke({&quot;input&quot;:&quot;What are the talent products delivered by DASA&quot;})<br>print (response[&quot;answer&quot;])</pre><p>The answer given is highly accurate due to the context provided.</p><pre>The talent products delivered by DASA focus on enhancing individual and team capabilities in organizations, preparing them for high-performance environments that boost enterprise agility, customer centricity, and innovation.</pre><p>To sum it up, the Retrieval Chain from LangChain uses the input question to retrieve the relevant data from a vector store, and sends only the relevant data as context along with the user question to the LLM. This enables a large amount of data to be used as context for the LLM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*tn4IBAYOeDu-pdWe" /></figure><p>How Retrieval Chain from Langchain works</p><p>In the next articles, we will look at how to create:</p><p>Conversational Retrieval LLM Chain — When we want the LLM to remember the conversation history</p><p>Agent — Where the LLM decides what step to take</p><p><strong>Previous Article:</strong></p><p><a href="https://medium.com/@humansofdatascience/begginers-guid-to-langchain-12ad239ee101">Building LLM Applications With LangChain</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f307b1a20e77" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beginner’s Guide to LangChain]]></title>
            <link>https://vijaykumarkartha.medium.com/begginers-guid-to-langchain-12ad239ee101?source=rss-1c0c72b715b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/12ad239ee101</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[create-ai-agent]]></category>
            <category><![CDATA[ai-agent]]></category>
            <dc:creator><![CDATA[Vijaykumar Kartha]]></dc:creator>
            <pubDate>Fri, 26 Apr 2024 02:41:17 GMT</pubDate>
            <atom:updated>2024-04-26T02:43:22.910Z</atom:updated>
            <content:encoded><![CDATA[<p>LangChain is a Python Package to build applications powered by Large Language Models such as ChatGPT.</p><p>To install LangChain, in the terminal, run:</p><pre>pip install langchain</pre><p>To use LangChain with OpenAI, you need to use the langchain-openai package.</p><p>In the terminal, run:</p><pre>pip install langchain-openai</pre><p>Once both the packages are installed, let’s create a simple LLM chain.</p><p>Imagine getting the power of ChatGPT in a single line of code.</p><pre>from langchain_openai import ChatOpenAI<br>llm = ChatOpenAI(api_key=&quot;&lt;Insert your Open AI API key here&gt;”)</pre><p>To use the LLM, this one line of code is enough:</p><pre>llm.invoke(&quot;What is the first law of thermodynamics?&quot;)</pre><p>The output is:</p><pre>AIMessage(content=&#39;The first law of thermodynamics, also known as the law of energy conservation, states that energy cannot be created or destroyed in an isolated system. It can only change forms or be transferred from one object to another. This law is also known as the principle of conservation of energy.&#39;, response_metadata={&#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None})</pre><p>Suppose we want to provide some standard information along with the input question, we can use a prompt template.</p><pre>from langchain_core.prompts import ChatPromptTemplate</pre><p>For example, a prompt template would be:</p><p>[</p><p>(“system”,”You are an expert Physics teacher.”),</p><p>(”user”,” {input}”)</p><p>]</p><p>The prompt template consists of two parts: the system message and the user message. The system message is used to provide a persona to the LLM or standard instructions for the LLM. The user message consists of the user input.</p><p>Create the prompt using the prompt template as below:</p><pre>prompt = ChatPromptTemplate.from_messages([<br>(&quot;system&quot;,&quot;You are an expert Physics teacher.&quot;),<br>(&quot;user&quot;,&quot; {input}&quot;)<br>])</pre><p>So, now we have an LLM and a prompt created from a prompt template.</p><p>We can create a pipeline consisting of the prompt and the LLM in a simple chain:</p><pre>chain = prompt | llm</pre><p>To run this chain, use</p><pre>chain.invoke({&quot;input&quot;:&quot;What is the first law of thermodynamics?&quot;})</pre><p>The response is:</p><pre>AIMessage(content=&#39;The first law of thermodynamics, also known as the law of energy conservation, states that energy cannot be created or destroyed in an isolated system. It can only change forms or be transferred from one object to another. In other words, the total energy of a closed system remains constant over time. This principle is often expressed mathematically as ΔU = Q - W, where ΔU is the change in internal energy of the system, Q is the heat added to the system, and W is the work done by the system.&#39;, response_metadata={&#39;finish_reason&#39;: &#39;stop&#39;, &#39;logprobs&#39;: None})</pre><p>To make the output simple and appear as a string, we can use the below code:</p><pre>from langchain_core.output_parsers import StrOutputParser<br>output_parser = StrOutputParser()</pre><p>We can link the output_parser as below:</p><pre>chain = prompt | llm | output_parser</pre><p>and invoke the chain with the same question input</p><pre>chain.invoke({&quot;input&quot;:&quot;What is the first law of thermodynamics?&quot;})</pre><p>The output is now:</p><pre>&#39;The first law of thermodynamics, also known as the law of energy conservation, states that energy cannot be created or destroyed in an isolated system. It can only change forms or be transferred from one part of the system to another. In other words, the total energy of a closed system remains constant over time. This principle is a fundamental concept in thermodynamics and plays a key role in understanding energy transformations in various physical processes.&#39;</pre><p>Thus, we have set up a basic LLM Chain using LangChain.</p><p>In this article, we have touched on the basics of llms, prompts, prompt templates and output parsers using the LangChain package.</p><p>In the next articles, we will look at how to create:</p><h3>Retrieval LLM Chain — When we have lot more information such as documents to provide to the LLM as context</h3><h3>Conversational Retrieval LLM Chain — When we want the LLM to remember the conversation history</h3><h3>Agent — Where the LLM decides what step to take</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12ad239ee101" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>