codekingpro/portable-devtools
114k
1"""Tool for the Google search API."""2 3from typing import Optional4 5from langchain_core._api.deprecation import deprecated6from langchain_core.callbacks import CallbackManagerForToolRun7from langchain_core.tools import BaseTool8 9from langchain_community.utilities.google_search import GoogleSearchAPIWrapper10 11 12@deprecated(13 since="0.0.33",14 removal="1.0",15 alternative_import="langchain_google_community.GoogleSearchRun",16)17class GoogleSearchRun(BaseTool):18 """Tool that queries the Google search API."""19 20 name: str = "google_search"21 description: str = (22 "A wrapper around Google Search. "23 "Useful for when you need to answer questions about current events. "24 "Input should be a search query."25 )26 api_wrapper: GoogleSearchAPIWrapper27 28 def _run(29 self,30 query: str,31 run_manager: Optional[CallbackManagerForToolRun] = None,32 ) -> str:33 """Use the tool."""34 return self.api_wrapper.run(query)35 36 37@deprecated(38 since="0.0.33",39 removal="1.0",40 alternative_import="langchain_google_community.GoogleSearchResults",41)42class GoogleSearchResults(BaseTool):43 """Tool that queries the Google Search API and gets back json."""44 45 name: str = "google_search_results_json"46 description: str = (47 "A wrapper around Google Search. "48 "Useful for when you need to answer questions about current events. "49 "Input should be a search query. Output is a JSON array of the query results"50 )51 num_results: int = 452 api_wrapper: GoogleSearchAPIWrapper53 54 def _run(55 self,56 query: str,57 run_manager: Optional[CallbackManagerForToolRun] = None,58 ) -> str:59 """Use the tool."""60 return str(self.api_wrapper.results(query, self.num_results))61 