VTeam AI

Beginner Tutorials

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

Divider

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)

Divider

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?")

Tut11

Tut12

Next, let’s complicate a few things. We will be enabling this agent with multiple, varied tools.

Tutorial 2: Multi-tooling agent

Again, pip install and import required libraries

#!pip install langchain openai google-search-results langchain-experimental

from langchain.chat_models import ChatOpenAI

from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner

from langchain.llms import OpenAI

from langchain.utilities import SerpAPIWrapper

from langchain.agents.tools import Tool

from langchain.chains import LLMMathChain

Next, we will create a list of 2 tools, one being the Google Search API alongside a Calculator tool

search = SerpAPIWrapper(serpapi_api_key=serp_api)

llm = OpenAI(temperature=0,openai_api_key=api_key)

llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)

tools = [

    Tool(

        name = "Search",

        func=search.run,

        description="useful for when you need to answer questions about current events"

    ),

    Tool(

        name="Calculator",

        func=llm_math_chain.run,

        description="useful for when you need to answer questions about math"

    ),

]

Not going straight, we will create a Plan & Execute agent this time. What is it?

Plan-and-execute agents accomplish an objective by first planning what to do and then executing the sub-tasks. 

  • The planning is almost always done by an LLM.

  • The execution is usually done by a separate agent (equipped with tools).

Below are the steps of creating a Plan & Execute agent

model = ChatOpenAI(openai_api_key=api_key)

planner = load_chat_planner(model)

executor = load_agent_executor(model, tools, verbose=True)

agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)

Time for some action. We will be asking questions from 2 different domains together. In the logs generated, we can see how the agent uses the 2 tools.

agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")

Tut21

Tut23

Tut24

As you can see, the LLM planned the whole answer and used both Search and Calculator tools.

In the last tutorial, we will see how to create a custom tool (till now we are using existing tools) and use it to create an Agent.

Tutorial 3: Custom tool

We will be creating a custom tool that interacts with the Yahoo Finance app (the finance library) and gives answers based on it. Let’s get started with creating our custom tool

!pip install langchain openai s langchain-experimental yfinance

Next, we will create a couple of functions that can give us current stock price and % stock price change for a given stock

import yfinance as yf

from datetime import datetime, timedelta

def get_current_stock_price(ticker):

    """Method to get current stock price"""

    ticker_data = yf.Ticker(ticker)

    recent = ticker_data.history(period="1d")

    return {"price": recent.iloc[0]["Close"], "currency": ticker_data.info["currency"]}

def get_stock_performance(ticker, days):

    """Method to get stock price change in percentage"""

    past_date = datetime.today() - timedelta(days=days)

    ticker_data = yf.Ticker(ticker)

    history = ticker_data.history(start=past_date)

    old_price = history.iloc[0]["Close"]

    current_price = history.iloc[-1]["Close"]

    return {"percent_change": ((current_price - old_price) / old_price) * 100}

Next, we will, using the pydantic BaseModel and Langchain BaseTool, convert these functions into 2 sepratae tools

from typing import Type

from pydantic import BaseModel, Field

from langchain.tools import BaseTool

class CurrentStockPriceInput(BaseModel):

    """Inputs for get_current_stock_price"""

    ticker: str = Field(description="Ticker symbol of the stock")

class CurrentStockPriceTool(BaseTool):

    name = "get_current_stock_price"

    description = """

        Useful when you want to get current stock price.

        You should enter the stock ticker symbol recognized by the yahoo finance

        """

    args_schema: Type[BaseModel] = CurrentStockPriceInput

    def _run(self, ticker: str):

        price_response = get_current_stock_price(ticker)

        return price_response

    def _arun(self, ticker: str):

        raise NotImplementedError("get_current_stock_price does not support async")

class StockPercentChangeInput(BaseModel):

    """Inputs for get_stock_performance"""

    ticker: str = Field(description="Ticker symbol of the stock")

    days: int = Field(description="Timedelta days to get past date from current date")

class StockPerformanceTool(BaseTool):

    name = "get_stock_performance"

    description = """

        Useful when you want to check performance of the stock.

        You should enter the stock ticker symbol recognized by the yahoo finance.

        You should enter days as number of days from today from which performance needs to be check.

        output will be the change in the stock price represented as a percentage.

        """

    args_schema: Type[BaseModel] = StockPercentChangeInput

    def _run(self, ticker: str, days: int):

        response = get_stock_performance(ticker, days)

        return response

    def _arun(self, ticker: str):

        raise NotImplementedError("get_stock_performance does not support async")

As we are ready with the custom tools, we will now create Langchain agent with these tools

from langchain.agents import AgentType

from langchain.chat_models import ChatOpenAI

from langchain.agents import initialize_agent

llm = ChatOpenAI(openai_api_key=api_key)

tools = [CurrentStockPriceTool(), StockPerformanceTool()]

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

Time for testing our custom tools

agent.run("What is the current price of Microsoft stock? How it has performed over past 6 months?")

Tut31

As you can see, the agent is able to use the custom tool and reply to user queries.

In conclusion, delving into the world of LangChain and its agent creation tutorials opens up a realm of possibilities for harnessing the power of Large Language Models (LLMs) to enhance various applications. These tutorials, as highlighted in the sources, provide a comprehensive roadmap for both beginners and seasoned developers.

LangChain introduces the concept of agents, which act as decision-makers, capable of utilizing a suite of tools based on user input. The tutorials guide you through the process of creating your own LangChain agents, empowering you to leverage advanced LLM Chains and AI-driven decision-making processes.

Moreover, LangChain simplifies the interaction with LLMs, making it easier to develop applications that require AI-generated text. Whether you are building Question-Answering systems or other language-related applications, LangChain's chains and agents serve as powerful tools for creating complex language models.

In summary, these tutorials provide a solid foundation for mastering LangChain agents, enabling you to embark on a journey of AI-enhanced application development, making your projects smarter and more efficient. So, take the first step into the world of LangChain agents and unlock the potential of LLMs for your applications.

Disclaimer: The views and opinions expressed in this blog post are solely those of the authors and do not reflect the official policy or position of any of the mentioned tools. This blog post is not a form of advertising and no remuneration was received for the creation and publication of this post. The intention is to share our findings and experiences using these tools and is intended purely for informational purposes.