mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-18 16:16:33 +00:00
google-serper: async + full json results + support for Google Images, Places and News (#4078)
* implemented arun, results, and aresults. Reuses aiosession if available. * helper tools GoogleSerperRun and GoogleSerperResults * support for Google Images, Places and News (examples given) and filtering based on time (e.g. past hour) * updated docs
This commit is contained in:
@@ -12,6 +12,7 @@ from langchain.tools.file_management.read import ReadFileTool
|
||||
from langchain.tools.file_management.write import WriteFileTool
|
||||
from langchain.tools.google_places.tool import GooglePlacesTool
|
||||
from langchain.tools.google_search.tool import GoogleSearchResults, GoogleSearchRun
|
||||
from langchain.tools.google_serper.tool import GoogleSerperResults, GoogleSerperRun
|
||||
from langchain.tools.human.tool import HumanInputRun
|
||||
from langchain.tools.ifttt import IFTTTWebhook
|
||||
from langchain.tools.openapi.utils.api_models import APIOperation
|
||||
@@ -57,6 +58,8 @@ __all__ = [
|
||||
"GooglePlacesTool",
|
||||
"GoogleSearchResults",
|
||||
"GoogleSearchRun",
|
||||
"GoogleSerperResults",
|
||||
"GoogleSerperRun",
|
||||
"HumanInputRun",
|
||||
"IFTTTWebhook",
|
||||
"ListDirectoryTool",
|
||||
|
6
langchain/tools/google_serper/__init__.py
Normal file
6
langchain/tools/google_serper/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from langchain.tools.google_serper.tool import GoogleSerperResults, GoogleSerperRun
|
||||
|
||||
"""Google Serper API Toolkit."""
|
||||
"""Tool for the Serer.dev Google Search API."""
|
||||
|
||||
__all__ = ["GoogleSerperRun", "GoogleSerperResults"]
|
70
langchain/tools/google_serper/tool.py
Normal file
70
langchain/tools/google_serper/tool.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""Tool for the Serper.dev Google Search API."""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from pydantic.fields import Field
|
||||
|
||||
from langchain.callbacks.manager import (
|
||||
AsyncCallbackManagerForToolRun,
|
||||
CallbackManagerForToolRun,
|
||||
)
|
||||
from langchain.tools.base import BaseTool
|
||||
from langchain.utilities.google_serper import GoogleSerperAPIWrapper
|
||||
|
||||
|
||||
class GoogleSerperRun(BaseTool):
|
||||
"""Tool that adds the capability to query the Serper.dev Google search API."""
|
||||
|
||||
name = "Google Serper"
|
||||
description = (
|
||||
"A low-cost Google Search API."
|
||||
"Useful for when you need to answer questions about current events."
|
||||
"Input should be a search query."
|
||||
)
|
||||
api_wrapper: GoogleSerperAPIWrapper
|
||||
|
||||
def _run(
|
||||
self,
|
||||
query: str,
|
||||
run_manager: Optional[CallbackManagerForToolRun] = None,
|
||||
) -> str:
|
||||
"""Use the tool."""
|
||||
return str(self.api_wrapper.run(query))
|
||||
|
||||
async def _arun(
|
||||
self,
|
||||
query: str,
|
||||
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
||||
) -> str:
|
||||
"""Use the tool asynchronously."""
|
||||
return (await self.api_wrapper.arun(query)).__str__()
|
||||
|
||||
|
||||
class GoogleSerperResults(BaseTool):
|
||||
"""Tool that has capability to query the Serper.dev Google Search API
|
||||
and get back json."""
|
||||
|
||||
name = "Google Serrper Results JSON"
|
||||
description = (
|
||||
"A low-cost Google Search API."
|
||||
"Useful for when you need to answer questions about current events."
|
||||
"Input should be a search query. Output is a JSON object of the query results"
|
||||
)
|
||||
api_wrapper: GoogleSerperAPIWrapper = Field(default_factory=GoogleSerperAPIWrapper)
|
||||
|
||||
def _run(
|
||||
self,
|
||||
query: str,
|
||||
run_manager: Optional[CallbackManagerForToolRun] = None,
|
||||
) -> str:
|
||||
"""Use the tool."""
|
||||
return str(self.api_wrapper.results(query))
|
||||
|
||||
async def _arun(
|
||||
self,
|
||||
query: str,
|
||||
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
||||
) -> str:
|
||||
"""Use the tool asynchronously."""
|
||||
|
||||
return (await self.api_wrapper.aresults(query)).__str__()
|
Reference in New Issue
Block a user