Codementor Events

How to Build Your First AI Agent with LangChain and GPT-4.

Published May 22, 2024
How to Build Your First AI Agent with LangChain and GPT-4.

I'm really excited about the future of AI Agents.

The field is so new but we've already seen so many great projects, such as AutoGPT, HuggingGPT, MetaGPT, or Microsoft's AutoGen. And we're only starting!

After reading this article, you will:

  • understand the difference between the standard Large Language Models and AI Agents,
  • learn how AI Agents "reason & act" with the ReAct type of prompting,
  • know how to implement a basic AI Agent with LangChain for OpenAI language models

Are you ready?

What Are AI Agents in Plain English?

Let me explain it with an example.

Imagine you want to create a design with Canva but you don't know how to do it.

You go to ChatGPT and explain what you want to create. ChatGPT will give you instructions on:

  • how to sign up to Canva
  • how to create an empty design
  • how to add elements to the design
  • how to edit those elements, etc.

But you'll have do everything by yourself.

ChatGPT won't do anything for you in Canva, right?

It won't unless you're a ChatGPT Plus user and have access to the Canva Plugin!

Because now, you can explain ChatGPT what you need and it'll create several designs in Canva!

GPT-4 with the Canva plugin is an example of an AI Agent! Technically, GPT-4 with any plugin is an AI Agent.

So what's the difference?

GPT-4 with plugins has 2 things that standard ChatGPT is missing:

  1. A toolkit (plugins).
  2. The power to decide when and which tool to use.

So instead of asking ChatGPT "how to do XYZ," you tell ChatGPT "using the tools I give you, do XYZ."

So GPT-4 as an AI Agent will "do it for you."

But how do the Agents decide?

Let's talk about ReAct.

ReAct stands for "Reasoning & Acting."

It's a type of prompting in which Large Language Models work in a loop containing 3 steps:

  1. Thought - deciding what to do.
  2. Action - doing "it".
  3. Observation - analyzing the results of the action from step 2.

Now, let's move to the project itself!

Step-by-Step Guide to Building Your First AI Agent

The idea for the project was to expose GPT-4 limitations and then fix them using GPT-4 as an AI agent.

So I came up the query: "Who is the NBA all-time leading scorer? What's his total points to the power of 0.42?"

The query contains questions that reveal 2 GPT-4 problems:

  1. It has outdated information.
  2. It's bad at math.

And the questions are easy to answer if you have access to Google Search and a calculator.

Let's find out how to arm GPT-4 with a practical toolkit!

Prerequisites

Before diving in, install these Python packages:

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

You will also need an OpenAI API key, which you can get on the OpenAI Website (you need to sign up first).

Then, copy the generated API Key to the .env file like this:

OPENAI_API_KEY=<Your-API-Key>

Replace <Your-API-Key> with the actual key.

Then, you need to get the SerpAPI API key, that you'll get on the SerpAPI Website.

Then, copy the generated API Key to the .env file like this:

SERPAPI_API_KEY=<Your-SerpAPI-Key>

Replace <Your-SerpAPI-Key> with the actual key.

Step 1: Load API Keys.

Let’s start by securely loading your OpenAI API key from a .env file.

from dotenv import load_dotenv
load_dotenv()

Why do we need the API keys?

  1. The OpenAI API key is required to use OpenAI’s models, such as GPT-4.
  2. The SerpAPI API key is required to use Google Search.

Step 2: Testing the Standard GPT-4.

First, we need to initialize the model, and specify we want to use GPT-4:

from langchain.chat_models import ChatOpenAI
gpt4 = ChatOpenAI(model="gpt-4", temperature=0)

Then, we ask our question:

gpt4.predict("Who is the NBA all-time leading scorer? What's his total points to the power of 0.42?")

Let's see what we get as the result:

'The NBA all-time leading scorer is Kareem Abdul-Jabbar with a total of 38,387 points. His total points to the power of 0.42 is approximately 137.97.'

Both answers are wrong!

As I mentioned, we're exposing 2 problems:

  1. Outdated information. In 2023, LeBron James surpassed Kareem Abdul-Jabbar as the NBA all-time leading scorer.
  2. Bad math. Kareem's total points to the power of 0.42 is approximately 84.2.

So let's fix the answers!

Step 3: Giving GPT-4 Tools: Google Search & Calculator.

AI Agents have tools and the power to use them.

So let's provide GPT-4 with some tools:

from langchain.utilities import SerpAPIWrapper
from langchain.agents import Tool, load_tools
from langchain.chat_models import ChatOpenAI

search = SerpAPIWrapper()

#initialize GPT-4
gpt4 = ChatOpenAI(model="gpt-4", temperature=0)

# create the serp tool
serp_tool = Tool(
  name="Search",
  func=search.run,
  description="useful for when you need to answer questions about current events. You should ask targeted questions",
)

# Initialize tools with calculator and the model
gpt4_tools = load_tools(["llm-math"], llm=gpt4)
# add the serp tool
gpt4_tools = gpt4_tools + [serp_tool]

This code snippet contains several important parts:

  1. Initializing the GPT-4 model.
  2. Importing and initializing the SerpAPIWrapper
  3. Creating the serp_tool and giving it a description on when it should be used.
  4. Initializing the toolkit with llm-math for calculations.
  5. Adding the SERP tool to the toolkit.

As a result, we defined 2 tools: Google Search and a calculator.

Step 4: Initializing the Agent.

It's time to initialize the AI Agent. Here's how:

from langchain.agents import AgentType, initialize_agent

# initialize GPT-4 Agent
gpt4agent = initialize_agent(
  gpt4_tools,
  ChatOpenAI(model="gpt-4", temperature=0),
  agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
  verbose=True,
)

We need the initialize_agent function with the following parameters:

  1. Tools. We defined them in the previous step.
  2. Model. Again, it's our GPT-4.
  3. Agent Type. Here's where we define we want to use the ReAct framework.
  4. We also set verbose=True to see every step of our ReAct chain.

Awesome! Time to play around!

Step 5: Testing our AI Agent.

To run the agent, we just need the run() function:

gpt4agent.run("Who is the NBA all-time leading scorer? What's his total points to the power of 0.42?")

Let's analyze the Agent's ReAct process.

  • Thought 1: I need to find out who the NBA all-time leading scorer is and how many total points he scored. I will use the Search tool to find this information.
  • Action 1: Using Google to find the NBA all-time leading scorer and his total points.
  • Observation 1: LeBron James points tracker: NBA's all-time top scorer closing in on 47,000 career points. Team USA's two-time Olympic gold medallist broke the career NBA regular season points scoring record held by Kareem Abdul-Jabbar. Basketball hero LeBron James is now the NBA's all-time top scorer.
  • Thought 2: The NBA all-time leading scorer is LeBron James. But I need to find the exact total points scored by LeBron James. I will use the Search tool again to find this information.
  • Action 2: Using Google to find LeBron's total points.
  • Observation 2: 38,652
  • Thought 3: LeBron James has scored a total of 38,652 points in the NBA. Now I need to calculate 38,652 to the power of 0.42. I will use the Calculator tool for this.
  • Action 3: Using calculator to get the result of 38,652^0.42
  • Observation 3: Answer: 84.45244506356971
  • Thought 4: I now know the final answer. The NBA all-time leading scorer is LeBron James and 38,652 (his total points) to the power of 0.42 is approximately 84.45.
  • Final Answer: The NBA all-time leading scorer is LeBron James and 38,652 (his total points) to the power of 0.42 is approximately 84.45.

So the Agent went through 3 iterations.

I especially love how it improved the search query to get the exact points!

What's your favorite part?

Conclusions

That’s it!

It wasn’t too bad, was it? LangChain makes our lives easier.

AI Agents aren’t much different from the standard Large Language Models. We just give them some tools and let them decide what to do.

But this simple concept powers all the great projects I mentioned at the beginning of the article!

What else would you like to read about? Leave a comment ✍️

Discover and read more posts from Krzysztof Ograbek
get started
post comments2Replies
Cassie Bratt
5 months ago

I m so excited about to build first AI agent with langchain. Amazing information you have shared with us.

Krzysztof Ograbek
5 months ago

Awesome! Let me know if you need help.