diff --git a/langchain/tools/__init__.py b/langchain/tools/__init__.py index 3c034f83b84..9fa308b91ef 100644 --- a/langchain/tools/__init__.py +++ b/langchain/tools/__init__.py @@ -1,8 +1,10 @@ """Core toolkit implementations.""" from langchain.tools.base import BaseTool -from langchain.tools.ddg_search.tool import DuckDuckGoSearchTool +from langchain.tools.bing_search.tool import BingSearchResults, BingSearchRun +from langchain.tools.ddg_search.tool import DuckDuckGoSearchResults, DuckDuckGoSearchRun from langchain.tools.google_places.tool import GooglePlacesTool +from langchain.tools.google_search.tool import GoogleSearchResults, GoogleSearchRun from langchain.tools.ifttt import IFTTTWebhook from langchain.tools.openapi.utils.api_models import APIOperation from langchain.tools.openapi.utils.openapi_utils import OpenAPISpec diff --git a/langchain/tools/bing_search/tool.py b/langchain/tools/bing_search/tool.py index c851c3ee845..dd57295c717 100644 --- a/langchain/tools/bing_search/tool.py +++ b/langchain/tools/bing_search/tool.py @@ -22,3 +22,24 @@ class BingSearchRun(BaseTool): async def _arun(self, query: str) -> str: """Use the tool asynchronously.""" raise NotImplementedError("BingSearchRun does not support async") + + +class BingSearchResults(BaseTool): + """Tool that has capability to query the Bing Search API and get back json.""" + + name = "Bing Search Results JSON" + description = ( + "A wrapper around Bing Search. " + "Useful for when you need to answer questions about current events. " + "Input should be a search query. Output is a JSON array of the query results" + ) + num_results: int = 4 + api_wrapper: BingSearchAPIWrapper + + def _run(self, query: str) -> str: + """Use the tool.""" + return str(self.api_wrapper.results(query, self.num_results)) + + async def _arun(self, query: str) -> str: + """Use the tool asynchronously.""" + raise NotImplementedError("BingSearchResults does not support async") diff --git a/langchain/tools/ddg_search/tool.py b/langchain/tools/ddg_search/tool.py index 33044241c4b..103eb8c4b3c 100644 --- a/langchain/tools/ddg_search/tool.py +++ b/langchain/tools/ddg_search/tool.py @@ -4,9 +4,9 @@ from pydantic import Field from langchain.tools.base import BaseTool from langchain.utilities.duckduckgo_search import DuckDuckGoSearchAPIWrapper +import warnings - -class DuckDuckGoSearchTool(BaseTool): +class DuckDuckGoSearchRun(BaseTool): """Tool that adds the capability to query the DuckDuckGo search API.""" name = "DuckDuckGo Search" @@ -26,3 +26,34 @@ class DuckDuckGoSearchTool(BaseTool): async def _arun(self, query: str) -> str: """Use the tool asynchronously.""" raise NotImplementedError("DuckDuckGoSearch does not support async") + + +class DuckDuckGoSearchResults(BaseTool): + """Tool that has capability to query the Duck Duck Go Search API and get back json.""" + + name = "DuckDuckGo Results JSON" + description = ( + "A wrapper around Duck Duck Go Search. " + "Useful for when you need to answer questions about current events. " + "Input should be a search query. Output is a JSON array of the query results" + ) + num_results: int = 4 + api_wrapper: DuckDuckGoSearchAPIWrapper = Field( + default_factory=DuckDuckGoSearchAPIWrapper + ) + + def _run(self, query: str) -> str: + """Use the tool.""" + return str(self.api_wrapper.results(query, self.num_results)) + + async def _arun(self, query: str) -> str: + """Use the tool asynchronously.""" + raise NotImplementedError("DuckDuckGoSearchResults does not support async") + +def DuckDuckGoSearchTool(*args, **kwargs): + warnings.warn( + "DuckDuckGoSearchTool will be deprecated in the future. " + "Please use DuckDuckGoSearchRun instead.", + DeprecationWarning, + ) + return DuckDuckGoSearchRun(*args, **kwargs) \ No newline at end of file diff --git a/tests/integration_tests/utilities/test_duckduckdgo_search_api.py b/tests/integration_tests/utilities/test_duckduckdgo_search_api.py index a6397251aa7..8d228e573d6 100644 --- a/tests/integration_tests/utilities/test_duckduckdgo_search_api.py +++ b/tests/integration_tests/utilities/test_duckduckdgo_search_api.py @@ -1,6 +1,6 @@ import pytest -from langchain.tools.ddg_search.tool import DuckDuckGoSearchTool +from langchain.tools.ddg_search.tool import DuckDuckGoSearchRun def ddg_installed() -> bool: @@ -16,7 +16,7 @@ def ddg_installed() -> bool: @pytest.mark.skipif(not ddg_installed(), reason="requires duckduckgo-search package") def test_ddg_search_tool() -> None: keywords = "Bella Ciao" - tool = DuckDuckGoSearchTool() + tool = DuckDuckGoSearchRun() result = tool(keywords) print(result) assert len(result.split()) > 20