2023-12-11 21:53:30 +00:00
|
|
|
"""Tool for the Google search API."""
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
|
|
from langchain_core.tools import BaseTool
|
|
|
|
|
|
|
|
from langchain_community.utilities.google_search import GoogleSearchAPIWrapper
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleSearchRun(BaseTool):
|
|
|
|
"""Tool that queries the Google search API."""
|
|
|
|
|
|
|
|
name: str = "google_search"
|
|
|
|
description: str = (
|
|
|
|
"A wrapper around Google Search. "
|
|
|
|
"Useful for when you need to answer questions about current events. "
|
|
|
|
"Input should be a search query."
|
|
|
|
)
|
|
|
|
api_wrapper: GoogleSearchAPIWrapper
|
|
|
|
|
|
|
|
def _run(
|
|
|
|
self,
|
|
|
|
query: str,
|
|
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
|
|
) -> str:
|
|
|
|
"""Use the tool."""
|
|
|
|
return self.api_wrapper.run(query)
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleSearchResults(BaseTool):
|
|
|
|
"""Tool that queries the Google Search API and gets back json."""
|
|
|
|
|
2024-01-25 23:24:19 +00:00
|
|
|
name: str = "google_search_results_json"
|
2023-12-11 21:53:30 +00:00
|
|
|
description: str = (
|
|
|
|
"A wrapper around Google 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: GoogleSearchAPIWrapper
|
|
|
|
|
|
|
|
def _run(
|
|
|
|
self,
|
|
|
|
query: str,
|
|
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
|
|
) -> str:
|
|
|
|
"""Use the tool."""
|
|
|
|
return str(self.api_wrapper.results(query, self.num_results))
|