Understanding LangChain Agents: A Beginner’s Guide to How LangChain Agents Work

Devvrat Rana
20 min readJun 2, 2024

--

Introduction:

The core idea behind agents is leveraging a language model to dynamically choose a sequence of actions to take. While chains in Lang Chain rely on hardcoded sequences of actions, agents use a language model as a reasoning engine to determine the optimal actions and their order.

Agents in Lang Chain are designed to interact with various tools, making them versatile for tasks such as grounded question-answering, API interactions, and taking specific actions based on context. LangChain offers a robust framework for working with agents, including:

- A standard interface for agents.
- A variety of pre-built agents to choose from.
- Examples of end-to-end implementations of agents.

Agents vs. Chains

In chains, actions are predetermined and fixed in the code, which limits flexibility. In contrast, agents utilize a language model to make real-time decisions about which actions to take, execute those actions, observe the results, and repeat the process until the desired outcome is achieved. This dynamic decision-making process allows agents to adapt to different scenarios and environments effectively.

In this blog, we will explore how agents in Lang Chain work, their practical applications, and how they differ fundamentally from traditional chains. By understanding these concepts, you’ll gain insights into how to leverage LangChain’s agents to build more intelligent and adaptable systems.

The core idea behind agents is to leverage a language model to dynamically choose a sequence of actions. While in chains, the sequence of actions is hardcoded in the code, agents use a language model as a reasoning engine to determine the appropriate actions and their order.

Image credit to https://www.promptingguide.ai/research/llm-agents

Key Components

LLM
LLM stands for “Large Language Model.” These are types of artificial intelligence models designed to understand and generate human language. LLMs are built using deep learning techniques, particularly neural networks with many layers (hence “large”), and they are trained on vast amounts of text data. This training allows them to learn patterns, structures, and meanings in language, enabling them to perform a wide range of tasks such as answering questions, translating languages, summarizing text, writing content, and more.

Some well-known examples of LLMs include OpenAI’s GPT series (like GPT-3 and GPT-4), Google’s BERT, and Meta’s LLaMA.

The “large” in LLM typically refers to the number of parameters (i.e., the parts of the model that get adjusted during training) which can range from millions to hundreds of billions. The more parameters, the more capable the model is at understanding and generating complex language.

Agent
The agent is the chain responsible for deciding the next step, powered by a language model, a prompt, and an output parser. Different agents have unique prompting styles, input encoding methods, and output parsing techniques. LangChain provides a variety of built-in agents and allows for the creation of custom agents.

Tools
Tools are individual components designed to perform specific tasks, such as retrieving information from external sources or processing data.

Tools are functions an agent can invoke. The `Tool` abstraction includes:
- Input schema: Informs the language model of the required parameters.
- Function to run: Typically a Python function to be invoked.

Considerations
Two key design considerations for tools:
1. Providing the agent access to the right tools.
2. Describing the tools effectively for agent use.

LangChain offers a wide range of built-in tools and allows for the easy definition of custom tools with detailed descriptions.

Toolkits
Toolkits are collections of related tools needed for specific tasks. For instance, the GitHub toolkit includes tools for searching issues, reading files, commenting, etc. LangChain provides various built-in toolkits to get started.

Memory:
Memory is essential for creating more natural and contextually relevant interactions. It enables LLMs to store and retrieve information about past user interactions, transforming them from stateless agents into intelligent conversational partners. We have covered Lang chain memory module in our another blog How to develop a chatbot using the open-source LLM Mistral-7B, Lang Chain Memory, ConversationChain, and Flask.

Why Does an Agent Need Memory?

An agent in LangChain requires memory to store and retrieve information during decision-making. Memory is essential for maintaining context and recalling previous interactions, which is crucial for providing personalized and coherent responses.

Here are a few reasons why an agent needs memory:

Contextual Understanding: Memory helps an agent understand the context of a conversation. By storing previous messages or user inputs, the agent can refer back to them and provide more accurate and relevant responses. This ability to maintain a coherent conversation enhances the agent’s understanding of the user’s intent.

Long-Term Knowledge: Memory enables an agent to accumulate knowledge over time. By storing information, the agent can build a knowledge base to answer questions or provide recommendations. This allows the agent to deliver more informed and accurate responses based on past interactions.

Personalization: Memory allows an agent to personalize its responses based on the user’s preferences or history. By remembering previous interactions, the agent can tailor its responses to the specific needs or interests of the user. This enhances the user experience and makes the agent more effective in achieving its objectives.

Continuity: Memory ensures continuity in a conversation. By storing the conversation history, the agent can maintain a consistent dialogue with the user, creating a more natural and engaging user experience.

Lets Implement in code:

  1. Install some prerequisite Libraries:
pip install langchain openai wikipedia langchain-community

2. Setup LLM:

from langchain.agents import initialize_agent, load_tools, AgentType
from langchain.llms import OpenAI
llm = OpenAI(openai_api_key='your openai key') #provide you openai key

3. Load Tool:
You can provide the tools as arguments when initializing the toolkit or individually set up the desired tools. These tools can be chosen from LangChain’s native tools, or you can define custom tools if necessary.

tools = load_tools(["wikipedia", "llm-math"], llm=llm)

4. Initialize the Agent:
You can instantiate an agent using either the `AgentExecutor` class or the `initialize_agent` function.

AgentExecutor:
The `AgentExecutor` class is responsible for executing the agent’s actions and managing its memory. It requires an agent, a set of tools, and an optional memory object as input. This class offers a flexible and customizable way to run the agent, allowing you to specify the tools and memory to be used.

When to use AgentExecutor:
- When you need more control over executing the agent’s actions and memory management.
- When you want to specify the tools and memory used by the agent.

Initialize Agent:
The `initialize_agent` function is a convenience function provided by LangChain to simplify agent creation. It requires the agent class, the language model, and an optional list of tools as input. This function automatically initializes the agent with the specified language model and tools.

When to use initialize_agent:
- When you want a simplified way to create an agent without specifying the memory.
- When you need to create an agent with default settings quickly.

Choosing Between AgentExecutor and initialize_agent

Use `AgentExecutor` if you need more customization and control over the agent’s execution. Opt for `initialize_agent` if you prefer a more straightforward and quicker way to create an agent.

agent = initialize_agent(tools , llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

Types of Agents in LangChain

Agents in LangChain utilize a language model to determine the sequence of actions and their order.

1. Zero-shot ReAct:
The Zero-shot ReAct Agent is a language generation model capable of creating realistic contexts, even without specific training data. It serves various purposes, including generating creative text formats, language translation, and crafting diverse creative content.

Lets put all code together to develop Zero-Shot React Agent:

from langchain.agents import initialize_agent, load_tools, AgentType
from langchain.llms import OpenAI
llm = OpenAI(openai_api_key=userdata.get('openaikey'))
tools = load_tools(["wikipedia", "llm-math"], llm=llm)
agent = initialize_agent(tools , llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
result_1=agent.run("I have 500 rupees and want to buy a toy priced at 355 rupees. Please let me know how much money I will have left after purchasing the toy. ")
print(result_1)
> Entering new AgentExecutor chain...
This is a simple subtraction problem.
Action: Calculator
Action Input: 500 - 355
Observation: Answer: 145
Thought: I now know the final answer
Final Answer: After purchasing the toy, I will have 145 rupees left.

> Finished chain.
After purchasing the toy, I will have 145 rupees left.
result_2=agent.run("Who is the Prime Minister of India and what is his age?")
print(result_2)
> Entering new AgentExecutor chain...
I should first use wikipedia to find the current Prime Minister of India
Action: wikipedia
Action Input: Prime Minister of India
Observation: Page: Prime Minister of India
Summary: The prime minister of India (IAST: Bhārat kē Pradhānamantrī) is the head of government of the Republic of India. Executive authority is vested in the prime minister and his chosen Council of Ministers, despite the president of India being the nominal head of the executive. The prime minister has to be a member of one of the houses of bicameral Parliament of India, alongside heading the respective house. The prime minister and their cabinet are at all times responsible to the Lok Sabha.
The prime minister is appointed by the president of India; however, the prime minister has to enjoy the confidence of the majority of Lok Sabha members, who are directly elected every five years, lest the prime minister shall resign. The prime minister can be a member of the Lok Sabha or of the Rajya Sabha, the upper house of the parliament. The prime minister controls the selection and dismissal of members of the Union Council of Ministers; and allocation of posts to members within the government.
The longest-serving prime minister was Jawaharlal Nehru, also the first prime minister, whose tenure lasted 16 years and 286 days. His premiership was followed by Lal Bahadur Shastri's short tenure and Indira Gandhi's 11- and 4-year-long tenures, both politicians belonging to the Indian National Congress. After Indira Gandhi's assassination, her son Rajiv Gandhi took charge until 1989, when a decade with five unstable governments began. This was followed by the full terms of P. V. Narasimha Rao, Atal Bihari Vajpayee, Manmohan Singh, and Narendra Modi. Modi is the 14th and current prime minister of India, serving since 26 May 2014.

Page: List of prime ministers of India
Summary: The prime minister of India is the chief executive of the Government of India and chair of the Union Council of Ministers. Although the president of India is the constitutional, nominal, and ceremonial head of state, in practice and ordinarily, the executive authority is vested in the prime minister and their chosen Council of Ministers. The prime minister is the leader elected by the party with a majority in the lower house of the Indian parliament, the Lok Sabha, which is the main legislative body in the Republic of India. The prime minister and their cabinet are at all times responsible to the Lok Sabha. The prime minister can be a member of the Lok Sabha or of the Rajya Sabha, the upper house of the parliament. The prime minister ranks third in the order of precedence.

The prime minister is appointed by the president of India; however, the prime minister has to enjoy the confidence of the majority of Lok Sabha members, who are directly elected every five years, unless a prime minister resigns. The prime minister is the presiding member of the Council of Ministers of the Union government. The prime minister unilaterally controls the selection and dismissal of members of the council; and allocation of posts to members within the government. This council, which is collectively responsible to the Lok Sabha as per Article 75(3), assists the president regarding the operations under the latter's powers; however, by the virtue of Article 74 of the Constitution, such 'aid and advice' tendered by the council is binding.
Since 1947, India has had 14 prime ministers. Jawaharlal Nehru was India's first prime minister, serving as prime minister of the Dominion of India from 15 August 1947 until 26 January 1950, and thereafter of the Republic of India until his death in May 1964. (India conducted its first post-independence general elections in 1952). Earlier, Nehru had served as prime minister of the Interim Government of India during the British Raj from 2 September 1946 until 14 August 1947, his party, the Indian National Congress having won the 1946 Indian provincial elections. Nehru was succeeded by Lal Bahadur Shastri, whose 1 year 7-month term ended in his death in Tashkent, then in the USSR, where he had signed the Tashkent Declaration bet
Thought: Now that I know the current Prime Minister, I can use wikipedia again to find his age
Action: wikipedia
Action Input: Narendra Modi
Observation: Page: Narendra Modi
Summary: Narendra Damodardas Modi (Gujarati: [ˈnəɾendɾə dɑmodəɾˈdɑs ˈmodiː] ; born 17 September 1950) is an Indian politician who has served as the 14th prime minister of India since May 2014. Modi was the chief minister of Gujarat from 2001 to 2014 and is the Member of Parliament (MP) for Varanasi. He is a member of the Bharatiya Janata Party (BJP) and of the Rashtriya Swayamsevak Sangh (RSS), a right wing Hindu nationalist paramilitary volunteer organisation. He is the longest-serving prime minister from outside the Indian National Congress.
Modi was born and raised in Vadnagar in northeastern Gujarat, where he completed his secondary education. He was introduced to the RSS at the age of eight. At age 18, he was married to Jashodaben Modi, whom he abandoned soon after, only publicly acknowledging her four decades later when legally required to do so. Modi became a full-time worker for the RSS in Gujarat in 1971. The RSS assigned him to the BJP in 1985 and he held several positions within the party hierarchy until 2001, rising to the rank of general secretary.
In 2001, Modi was appointed Chief Minister of Gujarat and elected to the legislative assembly soon after. His administration is considered complicit in the 2002 Gujarat riots, and has been criticised for its management of the crisis. A little over 1,000 people were killed, according to official records, three-quarters of whom were Muslim; independent sources estimated 2,000 deaths, mostly Muslim. A Special Investigation Team appointed by the Supreme Court of India in 2012 found no evidence to initiate prosecution proceedings against him. While his policies as chief minister, which were credited for encouraging economic growth, were praised, Modi's administration was criticised for failing to significantly improve health, poverty and education indices in the state. In the 2014 Indian general election, Modi led the BJP to a parliamentary majority, the first for a party since 1984. His administration increased direct foreign investment, and it reduced spending on healthcare, education, and social-welfare programmes. Modi began a high-profile sanitation campaign, controversially initiated the 2016 demonetisation of high-denomination banknotes and introduced the Goods and Services Tax, and weakened or abolished environmental and labour laws.
Modi's administration launched the 2019 Balakot airstrike against an alleged terrorist training camp in Pakistan. The airstrike failed, and the deaths of six Indian personnel to friendly fire was later revealed: but the action had nationalist appeal. Modi's party won the 2019 general election which followed. In its second term, his administration revoked the special status of Jammu and Kashmir, an Indian-administered portion of the disputed Kashmir region, and introduced the Citizenship Amendment Act, prompting widespread protests, and spurring the 2020 Delhi riots in which Muslims were brutalised and killed by Hindu mobs, sometimes with the complicity of police forces controlled by the Modi administration. Three controversial farm laws led to sit-ins by farmers across the country, eventually causing their formal repeal. Modi oversaw India's response to the COVID-19 pandemic, during which 4.7 million Indians died, according to the World Health Organization's estimates.
Under Modi's tenure, India has experienced democratic backsliding, or the weakening of democratic institutions, individual rights, and freedom of expression. As prime minister, he has received consistently high approval ratings. Modi has been described as engineering a political realignment towards right-wing politics. He remains a controversial figure domestically and internationally, over his Hindu nationalist beliefs and handling of the Gujarat riots, which have been cited as evidence of a majoritarian and exclusionary social agenda.

Page: Narendra Modi Stadium
Summary: The Narendra Modi Stadium (NMS), formerly known as Motera Stadium, is an intern
Thought: I should now use wikipedia to find the age of Narendra Modi
Action: wikipedia
Action Input: Narendra Modi age
Observation: Page: Jashodaben Modi
Summary: Jashodaben Narendrabhai Modi (née Chimanlal Modi; born 1952) is a retired Indian school teacher. She is the estranged wife of Narendra Modi, the Prime Minister of India. The couple were married in 1968 when she was about 17 and Modi was 18. A short time into the marriage, Narendra Modi enstranged her. He did not acknowledge the marriage publicly until he was legally required to do so prior to the 2014 Indian general elections to the Lok Sabha. In 2015, Jashodaben Modi retired from her teaching job.

Page: Premiership of Narendra Modi
Summary: The premiership of Narendra Modi began on 26 May 2014 with his swearing-in as the Prime Minister of India at the Rashtrapati Bhavan. He became the 14th Prime Minister of India, succeeding Manmohan Singh of the Indian National Congress. Modi's first cabinet consisted of 45 ministers, 25 fewer than the previous United Progressive Alliance government. 21 ministers were added to the council of ministers on 9 November 2014.
In 2019, he was elected as the prime minister of India for the second time and sworn in at the Rashtrapati Bhavan on 30 May 2019. His second cabinet consisted of 54 ministers and initially had 51 ministers, which was expanded to 77 ministers during a reshuffle on 7 July 2021. His premiership has, to a considerable extent, practiced high command culture.
Some media sources cite India experiencing democratic backsliding under Modi's premiership, however this claim is denied by other sources.

Page: PM Narendra Modi
Summary: PM Narendra Modi is a 2019 Hindi-language biographical drama film directed by Omung Kumar, and written by Anirudh Chawla and Vivek Oberoi. The film is jointly produced by Suresh Oberoi, Sandip Singh, Anand Pandit, Acharya Manish under the banner of Legend Studios. The film's plot is based on the life of Narendra Modi, the 14th Prime Minister of India.
Principal photography for the film began in January 2019. The soundtrack was composed by Hitesh Modak and Shashi-Khushi. The credits in the movie for lyrics were given to Javed Akhtar, Sameer, Abhendra Upadhyay, Irshad Kamil, Parry G. and Lavraj, and released by T-Series. Javed Akhtar later confirmed that he has not written any songs in the film and is shocked along with Sameer to see their names in the credits. The film was theatrically released in India on 24 May 2019. The film received heavy criticism from the audience and the film was panned by critics, and Oberoi's performance was criticized. It has grossed over ₹11.51 crore in India.
Thought: I now have all the information I need to calculate the age of Narendra Modi
Action: Calculator
Action Input: 2021 - 1950
Observation: Answer: 71
Thought: I now know the final answer
Final Answer: The current Prime Minister of India, Narendra Modi, is 71 years old.

> Finished chain.
The current Prime Minister of India, Narendra Modi, is 71 years old.

2. Conversational ReAct
The Conversational ReAct agent is tailored for conversational environments. Its prompt is crafted to make the agent helpful and engaging in conversations. It employs the React framework to determine the appropriate tool to utilize and relies on memory to recall past conversational interactions.

from langchain.agents import initialize_agent, load_tools
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory
llm = OpenAI(openai_api_key=userdata.get('openaikey'))
tools = load_tools(["llm-math"], llm=llm)
memory = ConversationBufferMemory(memory_key="chat_history")
conversational_agent = initialize_agent(
agent='conversational-react-description',
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
memory=memory,)
result_3=conversational_agent.run("Who is the Prime Minister of India and what is his age?")
print(result_3)
> Entering new AgentExecutor chain...

Thought: Do I need to use a tool? Yes
Action: Calculator
Action Input: Age of the Prime Minister of India - 1940
Observation: Answer: 81
Thought: Do I need to use a tool? No
AI: The current Prime Minister of India is Narendra Modi, and he was born on September 17, 1950, making him 70 years old.

> Finished chain.
The current Prime Minister of India is Narendra Modi, and he was born on September 17, 1950, making him 70 years old.
result_3=conversational_agent.run("Please let me know his birth place")
print(result_3)
> Entering new AgentExecutor chain...
Thought: Do I need to use a tool? No
AI: Narendra Modi was born in Vadnagar, Bombay State (now Gujarat), India.

> Finished chain.
Narendra Modi was born in Vadnagar, Bombay State (now Gujarat), India

Please refer to Google colab for full code: https://colab.research.google.com/drive/1SS9AbZZZ0dzXPJ0I4z4spI9WFLY8zpUc#scrollTo=SFjyd_UC1ZqG

Other type of Agents:

3. ReAct Docstore:
This agent employs the React framework to interact with a docstore. It requires both a Search tool and a Lookup tool, each having identical names. The Search tool is responsible for querying documents, while the Lookup tool is utilized to search for terms within the most recently retrieved document.

LangChain docstores enable the storage and retrieval of information using conventional retrieval methods. One such docstore is Wikipedia, providing access to the wealth of information available on the site.

from langchain import Wikipedia
from langchain.agents.react.base import DocstoreExplorer
from langchain.agents import Tool

docstore=DocstoreExplorer(Wikipedia())
tools = [
Tool(
name="Search",
func=docstore.search,
description='search wikipedia'
),
Tool(
name="Lookup",
func=docstore.lookup,
description='lookup a term in wikipedia'
)
]

docstore_agent = initialize_agent(
tools,
llm,
agent="react-docstore",
verbose=True,
max_iterations=10
)

docstore_agent("Whats the age of Indian PM?")

Output:

> Entering new AgentExecutor chain...
Thought: I need to search Indian PM and find their age.
Action: Search[Indian PM]
Observation: The prime minister of India (IAST: Bhārat kē Pradhānamantrī) is the head of government of the Republic of India. Executive authority is vested in the prime minister and his chosen Council of Ministers, despite the president of India being the nominal head of the executive. The prime minister has to be a member of one of the houses of bicameral Parliament of India, alongside heading the respective house. The prime minister and his cabinet are at all times responsible to the Lok Sabha.
The prime minister is appointed by the president of India; however, the prime minister has to enjoy the confidence of the majority of Lok Sabha members, who are directly elected every five years, lest the prime minister shall resign. The prime minister can be a member of the Lok Sabha or of the Rajya Sabha, the upper house of the parliament. The prime minister controls the selection and dismissal of members of the Union Council of Ministers; and allocation of posts to members within the government.
The longest-serving prime minister was Jawaharlal Nehru, also the first prime minister, whose tenure lasted 16 years and 286 days. His premiership was followed by Lal Bahadur Shastri's short tenure and Indira Gandhi's 11- and 4-year-long tenures, both politicians belonging to the Indian National Congress. After Indira Gandhi's assassination, her son Rajiv Gandhi took charge until 1989, when a decade with five unstable governments began. This was followed by the full terms of P. V. Narasimha Rao, Atal Bihari Vajpayee, Manmohan Singh, and Narendra Modi. Modi is the 14th and current prime minister of India, serving since 26 May 2014.
Thought: The question does not specify a specific Indian PM. I can assume they are referring to the current PM, Narendra Modi, and find his age.
Action: Search[Narendra Modi]
Observation: Narendra Damodardas Modi (Gujarati: [ˈnəɾendɾə dɑmodəɾˈdɑs ˈmodiː] ; born 17 September 1950) is an Indian politician who has served as the 14th Prime Minister of India since 26 May 2014. Modi was the chief minister of Gujarat from 2001 to 2014 and is the Member of Parliament (MP) for Varanasi. He is a member of the Bharatiya Janata Party (BJP) and of the Rashtriya Swayamsevak Sangh (RSS), a right wing Hindu nationalist paramilitary volunteer organisation. He is the longest-serving prime minister outside the Indian National Congress.
Modi was born and raised in Vadnagar in northeastern Gujarat, where he completed his secondary education. He was introduced to the RSS at the age of eight. At the age of 18, he was married to Jashodaben Modi, whom he abandoned soon after, only publicly acknowledging her four decades later when legally required to do so. Modi became a full-time worker for the RSS in Gujarat in 1971. The RSS assigned him to the BJP in 1985 and he rose through the party hierarchy, becoming general secretary in 1998. In 2001, Modi was appointed Chief Minister of Gujarat and elected to the legislative assembly soon after. His administration is considered complicit in the 2002 Gujarat riots, and has been criticised for its management of the crisis. According to official records, a little over 1,000 people were killed, three-quarters of whom were Muslim; independent sources estimated 2,000 deaths, mostly Muslim. A Special Investigation Team appointed by the Supreme Court of India in 2012 found no evidence to initiate prosecution proceedings against him. While his policies as chief minister were credited for encouraging economic growth, his administration was criticised for failing to significantly improve health, poverty and education indices in the state.
In the 2014 Indian general election, Modi led the BJP to a parliamentary majority, the first for a party since 1984. His administration increased direct foreign investment, and it reduced spending on healthcare, education, and social-welfare programmes. Modi began a high-profile sanitation campaign, controversially initiated a demonetisation of banknotes and introduced the Goods and Services Tax, and weakened or abolished environmental and labour laws. Modi's administration launched the 2019 Balakot airstrike against an alleged terrorist training camp in Pakistan. The airstrike failed, but the action had nationalist appeal. Modi's party won the 2019 general election which followed. In its second term, his administration revoked the special status of Jammu and Kashmir, and introduced the Citizenship Amendment Act, prompting widespread protests, and spurring the 2020 Delhi riots in which Muslims were brutalised and killed by Hindu mobs. Three controversial farm laws led to sit-ins by farmers across the country, eventually causing their formal repeal. Modi oversaw India's response to the COVID-19 pandemic, during which, according to the World Health Organization's estimates, 4.7 million Indians died. In the 2024 general election, Modi's party lost its majority in the lower house of Parliament and formed a government leading the National Democratic Alliance coalition.
Under Modi's tenure, India has experienced democratic backsliding, or the weakening of democratic institutions, individual rights, and freedom of expression. As prime minister, he has received consistently high approval ratings. Modi has been described as engineering a political realignment towards right-wing politics. He remains a controversial figure domestically and internationally, over his Hindu nationalist beliefs and handling of the Gujarat riots, which have been cited as evidence of a majoritarian and exclusionary social agenda.
Thought: According to the information, Narendra Modi was born on September 17, 1950. To find his age, I need to subtract the current year (2021) from his birth year (1950).
Action: Finish[71]

> Finished chain.
{'input': 'Whats the age of Indian PM?', 'output': '71'}

4. Self-ask with search:

The Self-Ask approach enables a Language Learning Model (LLM) to provide answers to questions it wasn’t specifically trained on. While the model may not have a direct answer to a question, it can generate answers to sub-questions that are present within its dataset.

from langchain import hub
from langchain.agents import AgentExecutor, create_self_ask_with_search_agent
from langchain_community.llms import Fireworks
from langchain_community.tools.tavily_search import TavilyAnswer

We will initialize the tools we want to use. This is a good tool because it gives us answers (not documents)

For this agent, only one tool can be used and it needs to be named “Intermediate Answer”

tools = [TavilyAnswer(max_results=1, name="Intermediate Answer")]
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/self-ask-with-search")

# Choose the LLM that will drive the agent
llm = Fireworks()

# Construct the Self Ask With Search Agent
agent = create_self_ask_with_search_agent(llm, tools, prompt)


# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

Conclusion:

In this blog, we’ve delved into the LangChain Agent module for developing agent-based applications, exploring various agents and tools while considering conversation history. We introduced three types of agents: the Zero-shot ReAct Agent, Conversational Agent, ReAct Docstore and Self-ask with search catering to beginners.
However, our exploration doesn’t conclude here. Stay tuned for our next blog, where we’ll explore the advanced RAG Agents with different Planning techinques and Tools, providing deeper insights into refining and optimizing RAG models for enhanced performance.
Join us as we further explore the potential of these groundbreaking methodologies in revolutionizing the realm of Generative AI.

My other blogs:

1. Creating a Chatbot Using Open-Source LLM and RAG Technology with Lang Chain and Flask

2. Build a Chatbot with Advance RAG System: with LlamaIndex, OpenSource LLM, Flask and LangChain

3. How to build Chatbot with advance RAG system with by using LlamaIndex and OpenSource LLM with Flask…

4. How to develop a chatbot using the open-source LLM Mistral-7B, Lang Chain Memory, ConversationChain, and Flask.

References:

--

--