Harrison/searchapi (#14252)

Co-authored-by: SebastjanPrachovskij <86522260+SebastjanPrachovskij@users.noreply.github.com>
This commit is contained in:
Harrison Chase 2023-12-04 16:34:15 -08:00 committed by GitHub
parent 224aa5151d
commit 921c4b5597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 8 deletions

View File

@ -393,14 +393,24 @@ from langchain.chat_loaders.gmail import GMailLoader
## 3rd Party Integrations ## 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
>[SerpApi](https://serpapi.com/) provides a 3rd-party API to access Google search results. >[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 ```python
from langchain.utilities import GoogleSerperAPIWrapper from langchain.utilities import SerpAPIWrapper
``` ```
### YouTube ### YouTube

View File

@ -9,7 +9,7 @@
"\n", "\n",
"LangChain provides async support for Agents by leveraging the [asyncio](https://docs.python.org/3/library/asyncio.html) library.\n", "LangChain provides async support for Agents by leveraging the [asyncio](https://docs.python.org/3/library/asyncio.html) library.\n",
"\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", "\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", "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", "\n",

View File

@ -13,6 +13,7 @@ from langchain.agents.tools import Tool
from langchain.agents.utils import validate_tools_single_input from langchain.agents.utils import validate_tools_single_input
from langchain.tools.base import BaseTool from langchain.tools.base import BaseTool
from langchain.utilities.google_serper import GoogleSerperAPIWrapper from langchain.utilities.google_serper import GoogleSerperAPIWrapper
from langchain.utilities.searchapi import SearchApiAPIWrapper
from langchain.utilities.serpapi import SerpAPIWrapper from langchain.utilities.serpapi import SerpAPIWrapper
@ -64,7 +65,9 @@ class SelfAskWithSearchChain(AgentExecutor):
def __init__( def __init__(
self, self,
llm: BaseLanguageModel, llm: BaseLanguageModel,
search_chain: Union[GoogleSerperAPIWrapper, SerpAPIWrapper], search_chain: Union[
GoogleSerperAPIWrapper, SearchApiAPIWrapper, SerpAPIWrapper
],
**kwargs: Any, **kwargs: Any,
): ):
"""Initialize only with an LLM and a search chain.""" """Initialize only with an LLM and a search chain."""

View File

@ -284,6 +284,18 @@ def _import_google_serper_tool_GoogleSerperRun() -> Any:
return GoogleSerperRun 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: def _import_graphql_tool() -> Any:
from langchain.tools.graphql.tool import BaseGraphQLTool from langchain.tools.graphql.tool import BaseGraphQLTool
@ -819,6 +831,10 @@ def __getattr__(name: str) -> Any:
return _import_google_serper_tool_GoogleSerperResults() return _import_google_serper_tool_GoogleSerperResults()
elif name == "GoogleSerperRun": elif name == "GoogleSerperRun":
return _import_google_serper_tool_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": elif name == "BaseGraphQLTool":
return _import_graphql_tool() return _import_graphql_tool()
elif name == "HumanInputRun": elif name == "HumanInputRun":
@ -1023,6 +1039,8 @@ __all__ = [
"GoogleSearchRun", "GoogleSearchRun",
"GoogleSerperResults", "GoogleSerperResults",
"GoogleSerperRun", "GoogleSerperRun",
"SearchAPIResults",
"SearchAPIRun",
"HumanInputRun", "HumanInputRun",
"IFTTTWebhook", "IFTTTWebhook",
"InfoPowerBITool", "InfoPowerBITool",

View File

@ -1,7 +1,7 @@
"""Integration test for self ask with search.""" """Integration test for self ask with search."""
from langchain.agents.self_ask_with_search.base import SelfAskWithSearchChain from langchain.agents.self_ask_with_search.base import SelfAskWithSearchChain
from langchain.llms.openai import OpenAI 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: 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?" question = "What is the hometown of the reigning men's U.S. Open champion?"
chain = SelfAskWithSearchChain( chain = SelfAskWithSearchChain(
llm=OpenAI(temperature=0), llm=OpenAI(temperature=0),
search_chain=GoogleSerperAPIWrapper(), search_chain=SearchApiAPIWrapper(),
input_key="q", input_key="q",
output_key="a", output_key="a",
) )
answer = chain.run(question) answer = chain.run(question)
final_answer = answer.split("\n")[-1] final_answer = answer.split("\n")[-1]
assert final_answer == "El Palmar, Spain" assert final_answer == "Belgrade, Serbia"

View File

@ -104,5 +104,8 @@ def test_imports() -> None:
from langchain.llms import OpenAI # noqa: F401 from langchain.llms import OpenAI # noqa: F401
from langchain.retrievers import VespaRetriever # noqa: F401 from langchain.retrievers import VespaRetriever # noqa: F401
from langchain.tools import DuckDuckGoSearchResults # 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 from langchain.vectorstores import FAISS # noqa: F401

View File

@ -92,6 +92,8 @@ EXPECTED_ALL = [
"RequestsPostTool", "RequestsPostTool",
"RequestsPutTool", "RequestsPutTool",
"SceneXplainTool", "SceneXplainTool",
"SearchAPIRun",
"SearchAPIResults",
"SearxSearchResults", "SearxSearchResults",
"SearxSearchRun", "SearxSearchRun",
"ShellTool", "ShellTool",

View File

@ -94,6 +94,8 @@ _EXPECTED = [
"RequestsPostTool", "RequestsPostTool",
"RequestsPutTool", "RequestsPutTool",
"SceneXplainTool", "SceneXplainTool",
"SearchAPIResults",
"SearchAPIRun",
"SearxSearchResults", "SearxSearchResults",
"SearxSearchRun", "SearxSearchRun",
"ShellTool", "ShellTool",