Skip to content

The agent engineering platform.

Harrison/searchapi (#14252)

Harrison Chase921c4b559709
diff --git a/docs/docs/integrations/platforms/google.mdx b/docs/docs/integrations/platforms/google.mdx
index ec0d3003a5..a1a7b5673c 100644
--- a/docs/docs/integrations/platforms/google.mdx
+++ b/docs/docs/integrations/platforms/google.mdx
@@ -393,14 +393,24 @@ from langchain.chat_loaders.gmail import GMailLoader
## 3rd Party Integrations
+### SearchApi
+
+>[SearchApi](https://www.searchapi.io/) provides a 3rd-party API to access Google search results, YouTube search & transcripts, and other Google-related engines.
+
+See [usage examples and authorization instructions](/docs/integrations/tools/searchapi).
+
+```python
+from langchain.utilities import SearchApiAPIWrapper
+```
+
### SerpAPI
>[SerpApi](https://serpapi.com/) provides a 3rd-party API to access Google search results.
-See a [usage example and authorization instructions](/docs/integrations/tools/google_serper).
+See a [usage example and authorization instructions](/docs/integrations/tools/serpapi).
```python
-from langchain.utilities import GoogleSerperAPIWrapper
+from langchain.utilities import SerpAPIWrapper
```
### YouTube
diff --git a/docs/docs/modules/agents/how_to/async_agent.ipynb b/docs/docs/modules/agents/how_to/async_agent.ipynb
index e233372f0e..716c4e8751 100644
--- a/docs/docs/modules/agents/how_to/async_agent.ipynb
+++ b/docs/docs/modules/agents/how_to/async_agent.ipynb
@@ -9,7 +9,7 @@
"\n",
"LangChain provides async support for Agents by leveraging the [asyncio](https://docs.python.org/3/library/asyncio.html) library.\n",
"\n",
- "Async methods are currently supported for the following `Tool`s: [`GoogleSerperAPIWrapper`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/utilities/google_serper.py), [`SerpAPIWrapper`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/utilities/serpapi.py), [`LLMMathChain`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/chains/llm_math/base.py) and [`Qdrant`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/vectorstores/qdrant.py). Async support for other agent tools are on the roadmap.\n",
+ "Async methods are currently supported for the following `Tool`s: [`SearchApiAPIWrapper`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/utilities/searchapi.py), [`GoogleSerperAPIWrapper`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/utilities/google_serper.py), [`SerpAPIWrapper`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/utilities/serpapi.py), [`LLMMathChain`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/chains/llm_math/base.py) and [`Qdrant`](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/vectorstores/qdrant.py). Async support for other agent tools are on the roadmap.\n",
"\n",
"For `Tool`s that have a `coroutine` implemented (the four mentioned above), the `AgentExecutor` will `await` them directly. Otherwise, the `AgentExecutor` will call the `Tool`'s `func` via `asyncio.get_event_loop().run_in_executor` to avoid blocking the main runloop.\n",
"\n",
diff --git a/libs/langchain/langchain/agents/self_ask_with_search/base.py b/libs/langchain/langchain/agents/self_ask_with_search/base.py
index 5a4109a7cf..c95e1dd3a2 100644
--- a/libs/langchain/langchain/agents/self_ask_with_search/base.py
+++ b/libs/langchain/langchain/agents/self_ask_with_search/base.py
@@ -13,6 +13,7 @@ from langchain.agents.tools import Tool
from langchain.agents.utils import validate_tools_single_input
from langchain.tools.base import BaseTool
from langchain.utilities.google_serper import GoogleSerperAPIWrapper
+from langchain.utilities.searchapi import SearchApiAPIWrapper
from langchain.utilities.serpapi import SerpAPIWrapper
@@ -64,7 +65,9 @@ class SelfAskWithSearchChain(AgentExecutor):
def __init__(
self,
llm: BaseLanguageModel,
- search_chain: Union[GoogleSerperAPIWrapper, SerpAPIWrapper],
+ search_chain: Union[
+ GoogleSerperAPIWrapper, SearchApiAPIWrapper, SerpAPIWrapper
+ ],
**kwargs: Any,
):
"""Initialize only with an LLM and a search chain."""
diff --git a/libs/langchain/langchain/tools/__init__.py b/libs/langchain/langchain/tools/__init__.py
index cc8c37ee6a..7cc1037532 100644
--- a/libs/langchain/langchain/tools/__init__.py
+++ b/libs/langchain/langchain/tools/__init__.py
@@ -284,6 +284,18 @@ def _import_google_serper_tool_GoogleSerperRun() -> Any:
return GoogleSerperRun
+def _import_searchapi_tool_SearchAPIResults() -> Any:
+ from langchain.tools.searchapi.tool import SearchAPIResults
+
+ return SearchAPIResults
+
+
+def _import_searchapi_tool_SearchAPIRun() -> Any:
+ from langchain.tools.searchapi.tool import SearchAPIRun
+
+ return SearchAPIRun
+
+
def _import_graphql_tool() -> Any:
from langchain.tools.graphql.tool import BaseGraphQLTool
@@ -819,6 +831,10 @@ def __getattr__(name: str) -> Any:
return _import_google_serper_tool_GoogleSerperResults()
elif name == "GoogleSerperRun":
return _import_google_serper_tool_GoogleSerperRun()
+ elif name == "SearchAPIResults":
+ return _import_searchapi_tool_SearchAPIResults()
+ elif name == "SearchAPIRun":
+ return _import_searchapi_tool_SearchAPIRun()
elif name == "BaseGraphQLTool":
return _import_graphql_tool()
elif name == "HumanInputRun":
@@ -1023,6 +1039,8 @@ __all__ = [
"GoogleSearchRun",
"GoogleSerperResults",
"GoogleSerperRun",
+ "SearchAPIResults",
+ "SearchAPIRun",
"HumanInputRun",
"IFTTTWebhook",
"InfoPowerBITool",
diff --git a/libs/langchain/tests/integration_tests/chains/test_self_ask_with_search.py b/libs/langchain/tests/integration_tests/chains/test_self_ask_with_search.py
index 61ef78d922..f288f23dad 100644
--- a/libs/langchain/tests/integration_tests/chains/test_self_ask_with_search.py
+++ b/libs/langchain/tests/integration_tests/chains/test_self_ask_with_search.py
@@ -1,7 +1,7 @@
"""Integration test for self ask with search."""
from langchain.agents.self_ask_with_search.base import SelfAskWithSearchChain
from langchain.llms.openai import OpenAI
-from langchain.utilities.google_serper import GoogleSerperAPIWrapper
+from langchain.utilities.searchapi import SearchApiAPIWrapper
def test_self_ask_with_search() -> None:
@@ -9,10 +9,10 @@ def test_self_ask_with_search() -> None:
question = "What is the hometown of the reigning men's U.S. Open champion?"
chain = SelfAskWithSearchChain(
llm=OpenAI(temperature=0),
- search_chain=GoogleSerperAPIWrapper(),
+ search_chain=SearchApiAPIWrapper(),
input_key="q",
output_key="a",
)
answer = chain.run(question)
final_answer = answer.split("\n")[-1]
- assert final_answer == "El Palmar, Spain"
+ assert final_answer == "Belgrade, Serbia"
diff --git a/libs/langchain/tests/unit_tests/test_dependencies.py b/libs/langchain/tests/unit_tests/test_dependencies.py
index 2d326dcbbc..a5297dd569 100644
--- a/libs/langchain/tests/unit_tests/test_dependencies.py
+++ b/libs/langchain/tests/unit_tests/test_dependencies.py
@@ -104,5 +104,8 @@ def test_imports() -> None:
from langchain.llms import OpenAI # noqa: F401
from langchain.retrievers import VespaRetriever # noqa: F401
from langchain.tools import DuckDuckGoSearchResults # noqa: F401
- from langchain.utilities import SerpAPIWrapper # noqa: F401
+ from langchain.utilities import (
+ SearchApiAPIWrapper, # noqa: F401
+ SerpAPIWrapper, # noqa: F401
+ )
from langchain.vectorstores import FAISS # noqa: F401
diff --git a/libs/langchain/tests/unit_tests/tools/test_imports.py b/libs/langchain/tests/unit_tests/tools/test_imports.py
index dbd9bd4855..a0960a7d2f 100644
--- a/libs/langchain/tests/unit_tests/tools/test_imports.py
+++ b/libs/langchain/tests/unit_tests/tools/test_imports.py
@@ -92,6 +92,8 @@ EXPECTED_ALL = [
"RequestsPostTool",
"RequestsPutTool",
"SceneXplainTool",
+ "SearchAPIRun",
+ "SearchAPIResults",
"SearxSearchResults",
"SearxSearchRun",
"ShellTool",
diff --git a/libs/langchain/tests/unit_tests/tools/test_public_api.py b/libs/langchain/tests/unit_tests/tools/test_public_api.py
index 4d7cf8f4d0..87ebceaae3 100644
--- a/libs/langchain/tests/unit_tests/tools/test_public_api.py
+++ b/libs/langchain/tests/unit_tests/tools/test_public_api.py
@@ -94,6 +94,8 @@ _EXPECTED = [
"RequestsPostTool",
"RequestsPutTool",
"SceneXplainTool",
+ "SearchAPIResults",
+ "SearchAPIRun",
"SearxSearchResults",
"SearxSearchRun",
"ShellTool",