Agents

Agents maintain a level of autonomy by involving an LLM in order to determine which sequence of actions to follow.

Cobus Greyling
6 min readApr 27, 2023

--

Considering the diagram below, upon receiving a request, Agents leverage LLMs to make a decision on which Action to take. After an Action is completed, the Agent enters the Observation step. From Observation step Agent shares a Thought; if a final answer is not reached, the Agent cycles back to another Action in order to move closer to a Final Answer.

There is a whole array of Actions available to the LangChain Agent.

⭐️ Please follow me on LinkedIn for updates on Conversational AI ⭐️

The code below shows a complete working example of a LangChain Agent answering an extremely ambiguous and complex question:

Who is regarded as the father of the iPhone and what is the square root of his year of birth?

The agent has a few actions at its disposal:

  1. LLM Math,
  2. SerpApi, below is a screenshot of the SerpApi website. SerpApi makes data extraction from search engine results actionable.
  3. GPT-4 (gpt-4–0314).
SerpApi

The Actions used for the Agent are loaded in the following way:

tools = load_tools([“serpapi”, “llm-math”], llm=llm)

Here is the complete code which you can copy, paste into a notebook and run. You can just change the question to whatever you want to ask the Agent. And obviously insert your API key for SerpApi and OpenAI.

pip install langchain
pip install google-search-results
pip install openai

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI

import os
os.environ['OPENAI_API_KEY'] = str("xxxxxxxxxx")
os.environ["SERPAPI_API_KEY"] = str("xxxxxxxxxx")
llm = OpenAI(temperature=0,model_name='gpt-4-0314')

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

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

agent.run("Who is regarded as the farther of the iPhone and what is the square root of his year of birth?")

Here is the response from the Agent in text and the screenshot from the notebook:

> Entering new AgentExecutor chain...
I need to find out who is regarded as the father of the iPhone and his year of birth. Then, I will calculate the square root of his year of birth.
Action: Search
Action Input: father of the iPhone year of birth
Observation: Family. Steven Paul Jobs was born in San Francisco, California, on February 24, 1955, to Joanne Carole Schieble and Abdulfattah "John" Jandali (Arabic: عبد الف ...
Thought:I found that Steve Jobs is regarded as the father of the iPhone and he was born in 1955. Now I will calculate the square root of his year of birth.
Action: Calculator
Action Input: sqrt(1955)
Observation: Answer: 44.21538193886829
Thought:I now know the final answer.
Final Answer: Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22.

> Finished chain.
'Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22.

⭐️ Please follow me on LinkedIn for updates on Conversational AI ⭐️

Selecting the right agent type plays a big role in the output of the Agent. Below you can see a list of available agent types from the LangChain framework.

Source

When the Agent type is changed from ZERO_SHOT_REACT_DESCRIPTION to CHAT_ZERO_SHOT_REACT_DESCRIPTION the output looks quite different:

> Entering new AgentExecutor chain...
Thought: I need to find out who is regarded as the father of the iPhone and his year of birth. Then, I will calculate the square root of his year of birth.
Action:
```
{
"action": "Search",
"action_input": "father of the iPhone year of birth"
}
```
Observation: Family. Steven Paul Jobs was born in San Francisco, California, on February 24, 1955, to Joanne Carole Schieble and Abdulfattah "John" Jandali (Arabic: عبد الف ...
Thought:Steve Jobs is regarded as the father of the iPhone, and he was born in 1955. Now I will calculate the square root of 1955.
Action:
```
{
"action": "Calculator",
"action_input": "sqrt(1955)"
}
```

Observation: Answer: 44.21538193886829
Thought:I now know the final answer.
Final Answer: Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22.

> Finished chain.
'Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22.

Arming the Agent with a number of tools in the form of Actions will definitely make the Agent more autonomous and resourceful.

However, this will come at a cost, as the charge per call API for each Agent interaction can mount quite quickly.

⭐️ Please follow me on LinkedIn for updates on Conversational AI ⭐️

I’m currently the Chief Evangelist @ HumanFirst. I explore and write about all things at the intersection of AI and language; ranging from LLMs, Chatbots, Voicebots, Development Frameworks, Data-Centric latent spaces and more.

https://www.linkedin.com/in/cobusgreyling
https://www.linkedin.com/in/cobusgreyling

--

--

Cobus Greyling

I explore and write about all things at the intersection of AI & language; LLMs/NLP/NLU, Chat/Voicebots, CCAI. www.cobusgreyling.com