mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 15:48:29 +00:00
Add SerpApi tools - google search, google shopping
This commit is contained in:
@@ -43,4 +43,6 @@ from .tools import (
|
||||
YoutubeChannelSearchTool,
|
||||
YoutubeVideoSearchTool,
|
||||
WeaviateVectorSearchTool,
|
||||
SerpApiGoogleSearchTool,
|
||||
SerpApiGoogleShoppingTool,
|
||||
)
|
||||
|
||||
@@ -52,3 +52,5 @@ from .youtube_channel_search_tool.youtube_channel_search_tool import (
|
||||
)
|
||||
from .youtube_video_search_tool.youtube_video_search_tool import YoutubeVideoSearchTool
|
||||
from .weaviate_tool.vector_search import WeaviateVectorSearchTool
|
||||
from .serpapi_tool.serpapi_google_search_tool import SerpApiGoogleSearchTool
|
||||
from .serpapi_tool.serpapi_google_shopping_tool import SerpApiGoogleShoppingTool
|
||||
37
src/crewai_tools/tools/serpapi_tool/serpapi_base_tool.py
Normal file
37
src/crewai_tools/tools/serpapi_tool/serpapi_base_tool.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
import re
|
||||
from typing import Optional, Any
|
||||
|
||||
from crewai.tools import BaseTool
|
||||
|
||||
class SerpApiBaseTool(BaseTool):
|
||||
client: Optional[Any] = None
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
try:
|
||||
from serpapi import Client
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"`serpapi` package not found"
|
||||
)
|
||||
api_key = os.getenv("SERPAPI_API_KEY")
|
||||
if not api_key:
|
||||
raise ValueError(
|
||||
"Missing API key, you can get the key from https://serpapi.com/manage-api-key"
|
||||
)
|
||||
self.client = Client(api_key=api_key)
|
||||
|
||||
def _omit_fields(self, data, omit_patterns):
|
||||
if isinstance(data, dict):
|
||||
for field in list(data.keys()):
|
||||
if any(re.compile(p).match(field) for p in omit_patterns):
|
||||
data.pop(field, None)
|
||||
else:
|
||||
if isinstance(data[field], (dict, list)):
|
||||
self._omit_fields(data[field], omit_patterns)
|
||||
elif isinstance(data, list):
|
||||
for item in data:
|
||||
self._omit_fields(item, omit_patterns)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
from typing import Any, Type, Optional
|
||||
|
||||
import re
|
||||
from pydantic import BaseModel, Field
|
||||
from .serpapi_base_tool import SerpApiBaseTool
|
||||
from serpapi import HTTPError
|
||||
|
||||
class SerpApiGoogleSearchToolSchema(BaseModel):
|
||||
"""Input for Google Search."""
|
||||
search_query: str = Field(..., description="Mandatory search query you want to use to Google search.")
|
||||
location: Optional[str] = Field(None, description="Location you want the search to be performed in.")
|
||||
|
||||
class SerpApiGoogleSearchTool(SerpApiBaseTool):
|
||||
name: str = "Google Search"
|
||||
description: str = (
|
||||
"A tool to perform to perform a Google search with a search_query."
|
||||
)
|
||||
args_schema: Type[BaseModel] = SerpApiGoogleSearchToolSchema
|
||||
|
||||
def _run(
|
||||
self,
|
||||
**kwargs: Any,
|
||||
) -> Any:
|
||||
try:
|
||||
results = self.client.search({
|
||||
"q": kwargs.get("search_query"),
|
||||
"location": kwargs.get("location"),
|
||||
}).as_dict()
|
||||
|
||||
self._omit_fields(
|
||||
results,
|
||||
[r"search_metadata", r"search_parameters", r"serpapi_.+", r".+_token", r"displayed_link", r"pagination"]
|
||||
)
|
||||
|
||||
return results
|
||||
except HTTPError as e:
|
||||
return f"An error occurred: {str(e)}. Some parameters may be invalid."
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
from typing import Any, Type, Optional
|
||||
|
||||
import re
|
||||
from pydantic import BaseModel, Field
|
||||
from .serpapi_base_tool import SerpApiBaseTool
|
||||
from serpapi import HTTPError
|
||||
|
||||
class SerpApiGoogleShoppingToolSchema(BaseModel):
|
||||
"""Input for Google Shopping."""
|
||||
search_query: str = Field(..., description="Mandatory search query you want to use to Google shopping.")
|
||||
location: Optional[str] = Field(None, description="Location you want the search to be performed in.")
|
||||
|
||||
|
||||
class SerpApiGoogleShoppingTool(SerpApiBaseTool):
|
||||
name: str = "Google Shopping"
|
||||
description: str = (
|
||||
"A tool to perform search on Google shopping with a search_query."
|
||||
)
|
||||
args_schema: Type[BaseModel] = SerpApiGoogleShoppingToolSchema
|
||||
|
||||
def _run(
|
||||
self,
|
||||
**kwargs: Any,
|
||||
) -> Any:
|
||||
try:
|
||||
results = self.client.search({
|
||||
"engine": "google_shopping",
|
||||
"q": kwargs.get("search_query"),
|
||||
"location": kwargs.get("location")
|
||||
}).as_dict()
|
||||
|
||||
self._omit_fields(
|
||||
results,
|
||||
[r"search_metadata", r"search_parameters", r"serpapi_.+", r"filters", r"pagination"]
|
||||
)
|
||||
|
||||
return results
|
||||
except HTTPError as e:
|
||||
return f"An error occurred: {str(e)}. Some parameters may be invalid."
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user