Build an LLM Agent (GK) by connecting ChatGPT with Google Search

Share it with your senior IT friends and colleagues
Reading Time: 4 minutes

Namaste and Welcome to Build It Yourself.

In the tutorial, we will build an LLM Agent using LangChain to connect ChatGPT with Google Search to get answers on recent happenings as well.

We will be using OpenAI’s API and Google SERP API to create this.

Who is this article for?

If you think chatbots and AI/ LLM are not your stuff. Just go through this article and you will realize how easy it has become to create AI tools.

We will use this Google Colab Notebook – https://colab.research.google.com/drive/186PBHnbEvkyfoGs6DBBCAzoGHy5fdkfl?usp=sharing

Let us dive into it.

Pre-requisites for building an LLM Agent

– An Open Mind to learn new things

– OpenAI’s API Key

– Google SERP API Key

Create your API key 

Don’t worry, it’s not a very technical thing. Just follow the below instructions to get it.

OpenAI provides an option to create an API key. We will just generate that.

How to get OpenAI API Key

Step 1 – Login to OpenAI – https://openai.com/

Create an account if you don’t have one already

Step 2 – Find “API keys” in the left corner, click on it and then go to the API Keys section

Step 3 – Click on “+ Create new secret key”, provide a name and you will get your API key

Make sure, you copy it and write it somewhere as you will not be able to access it again.

APIs are like messengers that transfer information from one software to another. 

Our bot will use OpenAI’s GPT model to answer questions.

How to get SERP API Key

Step 1 – Login to SERPApi – https://serpapi.com/users/sign_in

Step 2 – Once you login, you have your Private API Key visible

Why should we connect ChatGPT with Google Search

As we know LLMs like ChatGPT are pre-trained.

GPT in ChatGPT stands for Generative Pre-trained Transformer – so a Transformer which is Pre-trained and that can generate. So, there is a knowledge cutoff for them.

In ChatGPT’s case, it’s September 2021. So, ChatGPT does not have the information about thse events that occurred after Sep’21.

Let us check this

When we asked ChatGPT – Who is the Prime Minster of the UK? As the Primer Minster has changed after Sep’21, we can see the information it gave was not right.

So, lets correct that by connecting OpenAI’s model with Google Search

Connecting OpenAI’s LLM with Google Search

Step 1 – install openai using pip(package installer for Python)

Step2 – Install Google search results

Step3 – Install Langchain

Step 4 – Import the necessary libraries and add your API keys

Step 5 – Set the temperature for the LLM

The more the temperature, more the randomness in the answer or more creativity in the output.

Currently, we are asking a fact to an LLM, we have kept the temperature as 0, so that it would give a factual answer.

Step 6 – Let’s build an LLM Agent using LangChain

The Agent would determine that it is a general knowledge query and it would connect with Google Search to find the relevant information

Step 7

As we can see, Agent identified that the action is “Search” and then it used SERP API and observed that the answer is “Rishi Sunak” and then gave the right answer.

Thanks for choosing to read it completely. I can try to simplify any AI concept for you.

In case you want me to simplify any AI topic, please let me know on LinkedIn – https://www.linkedin.com/in/nikhileshtayal/

Happy unleashing your creativity!

Here is how I learned AI as a non-technical person in 4 months for free.

Let’s learn to build a basic AI/ML model in 4 minutes (Part 1)

Share it with your senior IT friends and colleagues
Nikhilesh Tayal
Nikhilesh Tayal
Articles: 45