In this article, we will learn how to interface LangChain with SerpAPI in Python with the help of a practical demonstration.
Generating the SerpApi API Key and OpenAI API Key
SerpApi is a Google search result API which allows us to access and connect with Google search results programmatically. To use the Google search in our program, we need to provide the SerpApi API key. This API key is available through the “serpapi.com” platform.
The “Register” and “Sign In” buttons are visible on the displayed primary interface. To obtain a SerpApi key, you must register on this site. Anyone with a SerpApi account can sign in instantly. As for the new users, the “Register” section helps them to create an account.
Click on the “Register” button and a new window opens a signup page. Here, you get some options to sign up. You can either use your GitHub account or Gmail account. If not both, simply input some other account of your choice.
Set your password, confirm it, and hit the “Sign-up” button.
The following screen displays the plan selection options, followed by the plan’s limit. It is set to free by default which allows 100 searches per month. We’ll stick with the free plan for now.
You will then be required to verify the provided email address.
We verify the email. Then, a phone number verification is needed. Supply your active phone number and put the sent code to the appeared screen. There you go! A prompt shows that the phone number is verified. Hit the “Subscribe” button to get this free monthly plan.
The user profile appears as soon we subscribe to the plan. You can see with the profile icon that the search count is 0 since this is a new account and we haven’t used it yet.
The API DASHBOARD appears on the screen’s left side. Find the “API Key” section from a list of options that it provides. Simply click on it to obtain your SerpApi API key.
Now, we have the SerpAPI key to be used in the program for Google search results.
In addition, we require the OpenAI API key. This key can be obtained from the “OpenAI.com” website. Register an account on the platform.
Fill out the required information to sign up. After that, verify the credentials like the email address and phone number. Once your OpenAI account is established, click on the user profile which opens a list of options. Select the “View API Key” and then generate your secret API key.
You have to give a name to the secret key. The key that is generated must be copied and stored safely because it disappears once you hit “Done” and you won’t be able to recover it.
Both the SerpAPI key and OpenAI API key are successfully obtained. Now, we move to the main project where we use them in our code.
Integrating the SerpAPI with LangChain
We need to have some installed dependencies in our project. Here, we install three packages – LangChain, OpenAI, and Google-search-results.
We can install it with the subsequent command:
To install OpenAI:
To install the Google-search-results, the command is:
As soon as the dependencies are installed, we may begin to develop the Python program.
In the illustration that we perform, we use LangChain to construct an agent. Then, it interacts with the agent tools and a model to build a response to the provided query.
from langchain.agents import load_tools
from langchain.agents import initialize_agent
import os
os.environ["SERPAPI_API_KEY"] = "YOUR_SERPAPI_API_KEY"
os.environ['OPENAI_API_KEY'] = "sk-YOUR_OPENAI_API_KEY"
llm = OpenAI(temperature=0.3)
tools = load_tools(["serpapi"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("who won turkey's presidential elections this year?")
We first import the needed modules into our project. From the langchain.llm module, we import the OpenAI class. Through the OpenAI class, we interact with the OpenAI LLM. From the langchain.agents package, we import the load_tools and initialize_agent functions.
The load_tools function, as the name shows, simply loads the specified tools into the project. The initialize_agent function initializes the agent with the specified llm model and tools that are loaded. Lastly, we import the “os”. This is required to set the SerpAPI and OpenAI API keys to the environment variable.
We initialize the program by setting the API keys as the environment variable. For this, we have a mapping object which is the os.environ. It stores a name and a value. We set the name for the OpenAI key as OPENAI_API_KEY, and the SerpAPI key as SERPAPI_API_KEY. The values are set to both the respective keys.
Then, we call the constructor of the OpenAI class with a temperature value of 0.3. This value controls how focused or random the generated response is. After that, the load_tools function is invoked with the required tools. Here, the tool that we want to load is SerpApi. The llm is set to the OpenAI llm. Now, the agent uses SerpApi to fetch the record from the Google search results and evaluate it using the OpenAI llm model, and ultimately generate a response.
The agent is initialized by calling the initsialize_agent function with the specified agent tools and OpenAI llm. We also provide the agent type as “zero-shot-react-description”. The verbose parameter is set to True.
Lastly, the function run is invoked with the agent and the query is provided to this function to generate a response.
We execute the previously-explained program. This gives us the result which can be seen in the following snapshot:
Conclusion
Interfacing SerpAPI with LangChain is a very helpful technique while working with AI. You learned how to use the SerpAPI in LangChain in this post. We comprehended the concept by performing a practical implementation of this methodology in Python. Moreover, the key generations for SerpAPI as well as OpenAI are explained to you before initializing the Python program.