LangChain

How to Run an Agent as an Iterator in LangChain?

LangChain builds Natural Language Processing (NLP) applications that can understand text from humans and generate responses to their questions as well. The LangChain framework contains all the required dependencies and libraries to build these kinds of models like chat models, chatbots, etc. LangChain also builds chains to link the multiple steps of the process using the agents that help to perform all the actions.

Quick Outline

This post will demonstrate the following:

How to Run an Agent as an Iterator in LangChain

Conclusion

How to Run an Agent as an Iterator in LangChain?

Agents are used to get the pathway of the complete process and automate the process of performing all the actions within these steps. The agents can also be used to extract information in multiple iterations using independent outputs to get the dependent output. Running an agent as an iterator allows the user to get more complex outputs from the model as well.

Simply follow this guide in order to learn the process of running an agent as an iterator in LangChain:

Step 1: Installing Frameworks

Before running the agent as an iterator, we need to get the LangChain dependencies by installing the framework using the following code:

pip install langchain

After that, install the dotenv from Python using the pip command to get the environment variables and build applications that can load the configuration file from the “.env” file:

pip install python-dotenv

Now, install the last module required for this guide i.e. OpenAI that can be used to build language models:

pip install openai

Step 2: OpenAI Environment

The next step is to import os and getpass libraries to use their methods for entering the OpenAI API key:

import os

import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")

Step 3: Building Language Model

After setting up the environment, simply import the necessary libraries from the LangChain apart from that import dotenv and pydantic libraries as well:

import dotenv

from langchain.chat_models import ChatOpenAI

import pydantic

from langchain.agents import AgentExecutor, initialize_agent, AgentType

from langchain.schema import AgentFinish

from langchain.agents.tools import Tool

from langchain.chains import LLMMathChain

Once the libraries are imported successfully, simply build the Large Language Model using the ChatOpenAI() method. After that, configure the chain to build the connection between multiple components using the LLMMathChain() method:

llm = ChatOpenAI(temperature=0, model="gpt-4")

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

Step 4: Configuring Tools

After building the models and chain, simply configure the tools that can be used by the agent as an iterator in LangChain. These tools are used by the agent to perform all the required tasks for extracting the most complex data using multiple iterations:

primes = {998: 7901, 999: 7907, 1000: 7919}

class CalculatorInput(pydantic.BaseModel):
    question: str = pydantic.Field()
#Input to use the prime numbers by defining the PrimeInput
class PrimeInput(pydantic.BaseModel):
    n: int = pydantic.Field()
#define the formula that can differentiate between prime and nonprime numbers
def is_prime(n: int) -> bool:
    if n <= 1 or (n % 2 == 0 and n > 2):
        return False
    for i in range(3, int(n**0.5) + 1, 2):
        if n % i == 0:
            return False
    return True

def get_prime(n: int, primes: dict = primes) -> str:
    return str(primes.get(int(n)))
#define the async for finding the multiple numbers simultaneously
async def aget_prime(n: int, primes: dict = primes) -> str:
    return str(primes.get(int(n)))

tools = [
    Tool(
        name="GetPrime",
        func=get_prime,
        description="A tool that returns the `nth` prime number",
        args_schema=PrimeInput,
        coroutine=aget_prime,
    ),
    Tool.from_function(
        func=llm_math_chain.run,
        name="Calculator",
        description="It can easily calculate the mathematical queries",
        args_schema=CalculatorInput,
        coroutine=llm_math_chain.arun,
    ),
]

Step 5: Building Agent

The next step is to build or design the agent which can use the tools configured in the previous step to generate the output in natural language. The agent “ZERO_SHOT_REACT_DESCRIPTION” is used to perform the activities to use it as an iterator:

agent = initialize_agent(

  tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True

)

Step 6: Running Agent as an Iterator

At this point, we have configured all the components like agents, tools, and models for using the agent in LangChain. This step provides the question in the natural language and configures the steps like the “if statement” to use the agent as an iterator:

question = "What is the product of the 998th, 999th, and 1000th prime numbers"
#define iterations and integrate it with the agent to answer questions
for step in agent.iter(question):
    if output := step.get("intermediate_step"):
        action, value = output[0]
#define tools to define the agent and allow the user to make decisions
        if action.tool == "GetPrime":
            print(f"Checking whether {value} is prime...")
            assert is_prime(int(value))
        _continue = input("Should the agent continue (Y/n)?:\n")
        if _continue != "Y":
            break

After executing the above code, the model will ask the user to continue the agent or terminate it by making decisions. Hitting “Y” means that the model should continue the iteration and “n” should be used to terminate the process:

Output

The following screenshot displays the multiple iterations to get the 998th prime number in the first iteration and goes up to the 1000th prime number:

That’s all about running an agent as an iterator in LangChain.

Conclusion

To run an agent as an iterator in LangChain, simply install the modules required to get on the process like python-dotenv, OpenAI, etc. Before getting into the process of using the agent, simply build the language model and configure the required tools to build the agent. After that, simply provide the question and make the iterations to generate the answer that keeps running until the user says otherwise. This guide has elaborated on the process of running an agent as an iterator in the LangChain framework.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.