Documentation Index
Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
Use this file to discover all available pages before exploring further.
This page covers how to use the Serper Google Search API within LangChain. Serper is a low-cost Google Search API that can be used to add answer box, knowledge graph, and organic results data from Google Search.
It is broken into two parts: setup, and then references to the specific Google Serper wrapper.
Setup
- Go to serper.dev to sign up for a free account
- Get the api key and set it as an environment variable (
SERPER_API_KEY)
Wrappers
Utility
There exists a GoogleSerperAPIWrapper utility which wraps this API. To import this utility:
from langchain_community.utilities import GoogleSerperAPIWrapper
You can use it as part of an agent with search tools:
from langchain_community.utilities import GoogleSerperAPIWrapper
from langchain_openai import OpenAI
from langchain.agents import create_agent, Tool
import os
os.environ["SERPER_API_KEY"] = ""
os.environ["OPENAI_API_KEY"] = ""
llm = OpenAI(temperature=0)
search = GoogleSerperAPIWrapper()
tools = [
Tool(
name="Intermediate Answer",
func=search.run,
description="useful for when you need to ask with search",
)
]
agent = create_agent(
model=llm,
tools=tools,
)
agent.invoke("What is the hometown of the reigning men's U.S. Open champion?")
Output
Entering new AgentExecutor chain...
Yes.
Follow up: Who is the reigning men's U.S. Open champion?
Intermediate answer: Current champions Carlos Alcaraz, 2022 men's singles champion.
Follow up: Where is Carlos Alcaraz from?
Intermediate answer: El Palmar, Spain
So the final answer is: El Palmar, Spain
> Finished chain.
'El Palmar, Spain'
For a more detailed walkthrough of this wrapper, see Google Serper integration.
You can also easily load this wrapper as a Tool (to use with an Agent).
You can do this with:
from langchain_community.agent_toolkits.load_tools import load_tools
tools = load_tools(["google-serper"])
For more information on tools, see this overview.