In this article, I’ll show you how you can get your hands dirty with Langchain agents. If you are unaware of what Langchain is, I recommend you watch my recording here, wherein I just briefed you about it.
Langchain agents are the ones that use LLM to determine what needs to be done and in which order. You can quickly look at the available agents in the documentation, but I’ll list them here too.
- zero-shot-react-description
- react-docstore
- self-ask-with-search
- conversational-react-description
Here agents work via tools, and tools are nothing but are functions that will be used by agents to interact with the outside world.
List of tools that are available today are:
- python_repl
- serpapi
- wolfram-alpha
- requests
- terminal
- pal-math
- pal-colored-objects
- llm-math
- open-meteo-api
- news-api
- tmdb-api
- google-search
- searx-search
- google-serper, etc
In this article, I’m covering SerpApi.
What Is SerpApi?
SerpApi is a search tool that provides response about current events. At the time of writing this article, ChatGPT was not able to answer those questions, which are related to recent activity. Hence, we can use this tool.
Import Required Packages
To get started, we need to import the below packages:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
import os
Get OpenAPI Key
To get the OpenAI key, you need to go to https://openai.com/, login, and then grab the keys using highlighted way:
Get SerpApi Key
Go to https://serpapi.com/manage-api-key and login with your google id. Once logged in, you need to choose a plan and click on Subscribe, as shown below. You also need to verify your email and contact number.
Once everything is done, you can click on API Key on the left side and grab it from there as shown below:
Set Environment Variables
Once you get the keys, set that inside an environment variable(I’m using Windows) as shown below:
os.environ[“OPENAI_API_KEY”] = “KEY_HERE”
os.environ[“SERPAPI_API_KEY”] = “KEY_HERE”
Set LLM And Initialize Agent
Next, we need to set LLM, which is OpenAI here, and then we need to initialize the agent as shown below:
llm = OpenAI(temperature=0)
tools = load_tools([“serpapi”])
agent = initialize_agent(tools, llm, agent=”zero-shot-react-description”)
Run The Query
And final step is to run the query:
agent.run(“What is whisper API?”)
Output
On executing the above code cells, I received the below response:
‘Whisper API is an automatic speech recognition system developed by OpenAI that enables robust transcription in multiple languages. It is available through an API.’
Reference
https://langchain.readthedocs.io/