At present, the information within GPT-3 is limited to up to September 2021. Thus some of the events happening after that are unknown to the model. However, we can utilize web scraping of current Google search results to improve the model's output with up-to-date information.
These are a few tools we will use, they help to accomplish this task very easily.
- Langchain - A framework to build powerful LLMs apps
- SerpApi - An API call away to scrape Google search result. In another word, it is the JSON representative of Google search result.
- OpenAI - Of course
from langchain.llms import OpenAI
from langchain.agents import load_tools
from langchain.agents import initialize_agent
llm = OpenAI(temperature=0.9)
tools = load_tools(["serpapi"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("which club is Cristiano Ronaldo playing right now?")
This is the entire code of the app thanks to Langchain and SerpApi. You can test it out in replit - make sure to set OPENAI_API_KEY
and SERPAPI_API_KEY
. For SERPAPI_API_KEY
, you can register and get free searches, head over to the playground to have look.
Output from Langchain, provided the question "which club is Cristiano Ronaldo playing right now?"
Thought: research current teams
Action: Search
Action Input: "Cristiano Ronaldo's current team"
Observation: Cristiano Ronaldo dos Santos Aveiro GOIH ComM is a Portuguese professional footballer who plays as a forward for and captains both Saudi Professional League club Al Nassr and the Portugal national team.
Thought: Is Al Nassr the club he is playing for?
Action: Search
Action Input: "Al Nassr clubs"
Observation: Al Nassr Football Club is a Saudi Arabian football club based in Riyadh. Formed in 1955, the club plays its home games at the Mrsool Park.
Thought: This confirms Al Nassr is the club Ronaldo is playing for
Final Answer: Cristiano Ronaldo is currently playing for Al Nassr Football Club.
So what's happening here? Langchain's agent (specifically zero-shot-react-description
agent) have a prompt that instruct LLMs to output in specific format (as seen above), when the output action is Search
it will make a call to SerpAPI to scrape Google with the query from Action Input
. The result from Google will then send back to LLMs to process again until Final Answer
is given.
The final answer is Al Nassr Football Club
which is absolutely correct. Compared to ChatGPT, this is the output it returns provided the same question As of my knowledge cutoff of September 2021, Cristiano Ronaldo was playing for Manchester United. However, it's possible that he may have switched clubs since then.
However ChatGPT is aware of it's limitation which is kind of brilliant.
Langchain provided all the prompts that needed to talk to LLMs and in a way that can combine with other external API which is why it is powerful. You can find more how Langchain's agent works in the documentation.
This post is mainly to showcase the idea of combining LLMs and Google search results to provide up to date information. Hopefully it can help to spark new ideas for you.