• ML Spring
  • Posts
  • Understanding AI Agents in the age of LLMs!

Understanding AI Agents in the age of LLMs!

The ingredient for building powerful LLM apps! πŸš€

Today, I'll provide you with a simple yet comprehensive explanation of what an AI agent is.

LLMs combined with tools like LangChain have unlocked a multitude of possibilities in content generation, coding & analytics.

The concept of Agents has a pivotal role in this!

You can think of an Agent as an AI brain that uses an LLM for reasoning, planning & taking actions.

So, why do we need an agent in the first place? ❓

Language models (LLMs) are limited to the knowledge they have been trained on, and this knowledge can quickly become obsolete. (Retraining such a large model every day with the latest information is not feasible.)

Here are some shortcomings of LLMs:

  • They can hallucinate.

  • Their results are not always factual.

  • They have limited or no knowledge of current events.

  • LLMs struggle with complex calculations.

This is where an AI agent comes in. It can utilize external tools to overcome these limitations.

So, what do we mean when we say "tools"❓

As the name suggest as tool is used by an agent to accomplish a certain task!

A tool can be:

  • Google Search: to get the latest info

  • Python REPL: to execute code

  • Wolfram: to carry out complex calculations

  • An external API: to fetch a specific information

LangChain offers a generic framework to to easily use all such tool. (a code example coming up shortly)

Two broad categories of AI agents:

1️⃣ Action Agents:

Action agents are responsible for executing simple and straightforward tasks. For example, they may call an external weather API to retrieve the latest weather information.

2️⃣ Plan and Execute Agents:

These agents first devise a plan that includes multiple actions, which they then execute sequentially.

Particularly useful for more complex tasks, two of the famous examples are AutoGPT & BabayAGI (links will be shared in subsequest tweets)

Agent have memory❗️

Based on what we understand so far, an Agents uses an LLM as it's brain.

But it also has memory to keep track of all the actions taken and their responses, if it's a conversation agent it keeps track of the conversation history.

This makes an Agent sort of an AI Brain! 🧠

Now let's see things in Action!

Refer the code snippet below as you read the following.

Here's what's happening:

πŸ”ΉΒ Query: Agent received the query: "What is 100 multiplied by the number of Grand Slams won by Rafael Nadal"

πŸ”ΈΒ Leveraging Tools: The Agent will first use Google search to determine the number of Grand Slams won by Rafael Nadal & finds that the number is 22.

πŸ”ΉΒ Processing intermediate result: Now that agentgets the number 22 it need to multiply it with 100 & leverages the second tool available which is llm math.

πŸ”ΈPreparing final response: Once the agent has gathered and processed all the info, it prepares a natural language response to addresses the original query.

Check this outπŸ‘‡

You can copy the code from here:

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

# Loading the LLM
llm = OpenAI(model="text-davinci-003", temperature=0)

# Loading Tools that the agent can access
tools = load_tools(["google-search", "llm-math"], llm=llm)

# Initializing the agent & we pass in the llm & tools that it can use
agent = initialize_agent(tools, llm,
                         agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
                         verbose=True)

# Testing the agent
query = "What's the result of 100 multiplied by the number of Grand Slams won by Rafael Nadal?"
response = agent.run(query)

If you liked this content & are interested in:

  • Python 🐍

  • ML/MLOps πŸ› 

  • CV/NLP πŸ—£

  • LLMs 🧠

Find me on Twitter (X)

Everyday, I share tutorials on above topics!

Subscribe to keep reading

This content is free, but you must be subscribed to ML Spring to continue reading.

Already a subscriber?Sign In.Not now