The Fascinating Mind of an AI Agent from LangChain
In the last article, we saw a demonstration of an AI agent. There are multiple types of agents.
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.
Let’s first create the ReAct agent and then we will learn how it works.
For this agent also, we will have an LLM, a prompt, and tools. Let’s get them ready.
The LLM
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(api_key="Your OpenAI API key here")
The ReAct Agent Prompt
For the ReAct agent, we will use another prompt that is suited for the mechanism that a ReAct agent works with.
It is called “hwchase17/react”.
from langchain import hub
prompt = hub.pull("hwchase17/react")
Tools
Retriever Tool
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: Beginner’s Guide To Retrieval Chain Using LangChain
from langchain_community.document_loaders import WebBaseLoader
loader = WebBaseLoader("<https://www.dasa.org>")
docs = loader.load()
from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
from langchain_community.vectorstores import FAISS
vector = FAISS.from_documents(documents, embeddings)
retriever = vector.as_retriever()
from langchain.tools.retriever import create_retriever_tool
retriever_tool = create_retriever_tool (retriever, "DASA_search", "Search for information about DASA. For any questions about DASA, you must use this tool")
Search Tool
import os
os.environ['TAVILY_API_KEY'] = "<Your Tavily API Key here>"
from langchain_community.tools.tavily_search import TavilySearchResults
search = TavilySearchResults()
We add all the tools into a tools list.
tools = [retriever_tool, search]
Creating The ReAct Agent
To create the ReAct agent, we import the create_react_agent library and the AgentExecutor library.
from langchain.agents import create_react_agent
from langchain.agents import AgentExecutor
Now, it is time to create the ReAct agent.
agent = create_react_agent(llm,tools,prompt)
agent_executor=AgentExecutor(agent=agent,tools=tools,verbose=True)
Invoking the ReAct agent
Now, our AI ReAct agent is ready to be invoked.
agent_executor.invoke({"input":"What technological advancements have significantly impacted renewable energy production in the last decade?"})
Output
The output is:
> Entering new AgentExecutor chain…
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
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
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
I should also search for more information on clean energy technologies and decarbonization. Action: tavily_search_results_json Action Input: clean energy technologies advancements
> Finished chain.
{'input': 'What technological advancements have significantly impacted renewable energy production in the last decade?',
'output': '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.'}
The Fascinating Mind of the ReAct Agent
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.
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.
First, get an API key from https://smith.langchain.com/. Then, have the below code in your program:
os.environ["LANGCHAIN_TRACING_V2"]="true"
os.environ["LANGCHAIN_API_KEY"]="<Your API Key here>"
The traces of the actions of the AI agent will get stored in the default project at https://smith.langchain.com/.
The Magical Prompt that Makes the AI Agent Think
Let’s look at the prompt that was sent to the AI agent.
Answer the following questions as best you can. You have access to the following tools:
DASA_search: Search for information about DASA. For any questions about DASA, you must use this tool
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.Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [DASA_search, tavily_search_results_json]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!
Question: What technological advancements have significantly impacted renewable energy production in the last decade?
Thought:
The above prompt uses the prompt template from hwchase17/react. The prompt ends with:
Thought:
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.
Below are the traces of the AI actions collected from LangSmith.
AI
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
The next prompt send to the AI agent is:
Answer the following questions as best you can. You have access to the following tools:
DASA_search: Search for information about DASA. For any questions about DASA, you must use this tool
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.
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [DASA_search, tavily_search_results_json]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: What technological advancements have significantly impacted renewable energy production in the last decade?
Thought: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
Observation: [{'url': '<https://www.weforum.org/agenda/2023/09/renewable-energy-innovations-climate-emergency/>', 'content': '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 & Members\\nLanguage Editions\\nPrivacy Policy & 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.'}, {'url': '<https://www.iea.org/news/rapid-progress-of-key-clean-energy-technologies-shows-the-new-energy-economy-is-emerging-faster-than-many-think>', 'content': "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'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."}, {'url': '<https://www.technologyreview.com/2024/01/08/1085124/super-efficient-solar-cells-breakthrough-technologies/>', 'content': '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.'}, {'url': '<https://www.technologyreview.com/2024/04/24/1091129/fully-decarbonized-world-renewable-energy/>', 'content': '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 ...'}, {'url': '<https://www.sciencedirect.com/science/article/pii/S0960148121011162>', 'content': '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 ...'}]
Thought:
In the second prompt, the result of the first prompt is appended as Observation: followed by Thought:
And, this prompts the LLM to respond with the next thought and action as below.
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
This continues for three times. Below are the responses of the AI agent at each step.
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 initiativesI should also search for more information on clean energy technologies and decarbonization.
Action: tavily_search_results_json
Action Input: clean energy technologies advancementsI now know the technological advancements that have significantly impacted renewable energy production in the last decade.
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.
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.
The Mystery Behind the AI Agent
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.
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!
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.
Previous Articles:
Beginner’s Guide To Retrieval Chain From LangChain
Beginner’s Guide to Conversational Retrieval Chain From LangChain