Creating Agents using Langchain

vTeam.ai
Data Science in your pocket
3 min readSep 22, 2023
Photo by Sergiu Nista on Unsplash

Read the full blog below

VTeam | Creating Agents using Langchain

Jumping into Langchain, our tutorials have covered everything from Math to NLP. Now, let’s chat about the “Agent” thing in Langchain. Think of agents as the cool middlemen connecting language models to tools, making smart choices on the fly, and really getting the context of tasks.

We have covered multiple tutorials on Langchain till now, ranging from Mathematics, NLP problems, document analysis, and many others. In this tutorial, we will specifically focus on what is an Agent in Langchain and how we can leverage the power of agenting following a series of tutorials.

Note: If you have missed our previous parts, we have got you covered

Mathematics using LLMs and Langchain

Langchains For SQL generation, Fact-Checking, And Rewriting

LangChains For NER, Summarization, And Text-Tagging

Beginners guide for LangChain

But 1st of all, what is an Agent?

In Langchain, “agents” refer to components that play a crucial role in connecting language models to various tools and memory components. Agents are vital for several reasons:

Flexible Decision-Making: Agents in Langchain can decide the sequence of actions based on user input. Unlike predetermined chains of commands, they can dynamically adapt their behavior, making them suitable for a wide range of tasks and interactions.

Access to Tools: Agents have access to a suite of tools that they can utilize to perform tasks. These tools can be functions or resources that the agent leverages to achieve its objective.

Contextual Awareness: Langchain agents can be equipped with background context and personality traits, allowing them to respond in specific ways and understand the nuances of different tasks.

Toolkits: Langchain introduces the concept of toolkits, which are groups of tools organized to accomplish specific objectives. This modular approach enhances an agent’s ability to perform diverse tasks effectively.

Agent Runtimes: Langchain provides various agent runtimes, including the AgentExecutor, Plan-and-execute Agent, Baby AGI, and Auto GPT. These runtimes handle the execution of agent actions, including error handling, tool selection, and more.

Autonomy and Customization: Agents in Langchain can be customized to have specific contexts and tools. This allows developers to create autonomous agents that can perform tasks independently while remaining adaptable.

Overall, agents in Langchain are a powerful tool for connecting language models with practical applications. They bridge the gap between natural language understanding and task execution, offering flexibility and adaptability, making them essential components in various applications and scenarios.

We will start off by creating a basic agent 1st which intakes a query, does a Google search, and replies accordingly (LLM with internet access)

Tutorial 1: Google Search Agent with memory

1st of all, pip installs the required libraries and imports the required functions

#!pip install langchain openai google-search-results
from langchain.agents import Tool
from langchain.agents import AgentType
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI
from langchain.utilities import SerpAPIWrapper
from langchain.agents import initialize_agent

We will now create a Google Search tool using the SerpAPIWrapper which requires a API key (you can generate it on their website)

search = SerpAPIWrapper(serpapi_api_key=serp_api)
tools = [
Tool(
name = "Current Search",
func=search.run,
description="useful for when you need to answer questions about current events or the current state of the world"
),
]

As you can see, we also need to give this tool a name alongside a description so that the LLM can use it according to the situation.

Next, we will add memory to the LLM we create and pass the above search tool enabling it to Google search

memory = ConversationBufferMemory(memory_key="chat_history")
llm=OpenAI(openai_api_key=api_key)
agent_chain = initialize_agent(tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)

Time for some action

agent_chain.run(input="hi, i am bob")
agent_chain.run("what are some good dinners to make this week, if i like Italian food?")

Read the full blog on our website

VTeam | Blog

--

--