Google Search API integration with serper.dev (wrapper, tests, docs, … (#909)
Adds Google Search integration with [Serper](https://serper.dev) a
low-cost alternative to SerpAPI (10x cheaper + generous free tier).
Includes documentation, tests and examples. Hopefully I am not missing
anything.
Developers can sign up for a free account at
[serper.dev](https://serper.dev) and obtain an api key.
## Usage
```python
from langchain.utilities import GoogleSerperAPIWrapper
from langchain.llms.openai import OpenAI
from langchain.agents import initialize_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
)
]
self_ask_with_search = initialize_agent(tools, llm, agent="self-ask-with-search", verbose=True)
self_ask_with_search.run("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'
```
2023-02-16 06:47:17 +00:00
|
|
|
"""Integration test for Serper.dev's Google Search API Wrapper."""
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper
|
Google Search API integration with serper.dev (wrapper, tests, docs, … (#909)
Adds Google Search integration with [Serper](https://serper.dev) a
low-cost alternative to SerpAPI (10x cheaper + generous free tier).
Includes documentation, tests and examples. Hopefully I am not missing
anything.
Developers can sign up for a free account at
[serper.dev](https://serper.dev) and obtain an api key.
## Usage
```python
from langchain.utilities import GoogleSerperAPIWrapper
from langchain.llms.openai import OpenAI
from langchain.agents import initialize_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
)
]
self_ask_with_search = initialize_agent(tools, llm, agent="self-ask-with-search", verbose=True)
self_ask_with_search.run("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'
```
2023-02-16 06:47:17 +00:00
|
|
|
|
|
|
|
|
2023-05-18 04:40:39 +00:00
|
|
|
def test_search_call() -> None:
|
|
|
|
"""Test that call gives the correct answer from search."""
|
Google Search API integration with serper.dev (wrapper, tests, docs, … (#909)
Adds Google Search integration with [Serper](https://serper.dev) a
low-cost alternative to SerpAPI (10x cheaper + generous free tier).
Includes documentation, tests and examples. Hopefully I am not missing
anything.
Developers can sign up for a free account at
[serper.dev](https://serper.dev) and obtain an api key.
## Usage
```python
from langchain.utilities import GoogleSerperAPIWrapper
from langchain.llms.openai import OpenAI
from langchain.agents import initialize_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
)
]
self_ask_with_search = initialize_agent(tools, llm, agent="self-ask-with-search", verbose=True)
self_ask_with_search.run("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'
```
2023-02-16 06:47:17 +00:00
|
|
|
search = GoogleSerperAPIWrapper()
|
|
|
|
output = search.run("What was Obama's first name?")
|
|
|
|
assert "Barack Hussein Obama II" in output
|
2023-05-04 05:35:48 +00:00
|
|
|
|
|
|
|
|
2023-05-18 04:40:39 +00:00
|
|
|
def test_news_call() -> None:
|
|
|
|
"""Test that call gives the correct answer from news search."""
|
|
|
|
search = GoogleSerperAPIWrapper(type="news")
|
|
|
|
output = search.run("What's new with stock market?").lower()
|
|
|
|
assert "stock" in output or "market" in output
|
|
|
|
|
|
|
|
|
2023-05-04 05:35:48 +00:00
|
|
|
async def test_results() -> None:
|
|
|
|
"""Test that call gives the correct answer."""
|
|
|
|
search = GoogleSerperAPIWrapper()
|
|
|
|
output = search.results("What was Obama's first name?")
|
|
|
|
assert "Barack Hussein Obama II" in output["answerBox"]["answer"]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_async_call() -> None:
|
|
|
|
"""Test that call gives the correct answer."""
|
|
|
|
search = GoogleSerperAPIWrapper()
|
|
|
|
output = await search.arun("What was Obama's first name?")
|
|
|
|
assert "Barack Hussein Obama II" in output
|
|
|
|
|
|
|
|
|
|
|
|
async def test_async_results() -> None:
|
|
|
|
"""Test that call gives the correct answer."""
|
|
|
|
search = GoogleSerperAPIWrapper()
|
|
|
|
output = await search.aresults("What was Obama's first name?")
|
|
|
|
assert "Barack Hussein Obama II" in output["answerBox"]["answer"]
|