fixed serpapi

This commit is contained in:
Lorenze Jay
2025-01-10 18:24:07 -08:00
parent e26667ea40
commit d882818d6c
2 changed files with 65 additions and 26 deletions

View File

@@ -1,16 +1,30 @@
from typing import Any, Type, Optional from typing import Any, Type, Optional
import re import re
from pydantic import BaseModel, Field from pydantic import BaseModel, Field, ConfigDict
from .serpapi_base_tool import SerpApiBaseTool from .serpapi_base_tool import SerpApiBaseTool
from serpapi import HTTPError
try:
from serpapi import HTTPError
except ImportError:
HTTPError = Any
class SerpApiGoogleSearchToolSchema(BaseModel): class SerpApiGoogleSearchToolSchema(BaseModel):
"""Input for Google Search.""" """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.") 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): class SerpApiGoogleSearchTool(SerpApiBaseTool):
model_config = ConfigDict(
arbitrary_types_allowed=True, validate_assignment=True, frozen=False
)
name: str = "Google Search" name: str = "Google Search"
description: str = ( description: str = (
"A tool to perform to perform a Google search with a search_query." "A tool to perform to perform a Google search with a search_query."
@@ -22,19 +36,25 @@ class SerpApiGoogleSearchTool(SerpApiBaseTool):
**kwargs: Any, **kwargs: Any,
) -> Any: ) -> Any:
try: try:
results = self.client.search({ results = self.client.search(
"q": kwargs.get("search_query"), {
"location": kwargs.get("location"), "q": kwargs.get("search_query"),
}).as_dict() "location": kwargs.get("location"),
}
).as_dict()
self._omit_fields( self._omit_fields(
results, results,
[r"search_metadata", r"search_parameters", r"serpapi_.+", r".+_token", r"displayed_link", r"pagination"] [
r"search_metadata",
r"search_parameters",
r"serpapi_.+",
r".+_token",
r"displayed_link",
r"pagination",
],
) )
return results return results
except HTTPError as e: except HTTPError as e:
return f"An error occurred: {str(e)}. Some parameters may be invalid." return f"An error occurred: {str(e)}. Some parameters may be invalid."

View File

@@ -3,15 +3,29 @@ from typing import Any, Type, Optional
import re import re
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from .serpapi_base_tool import SerpApiBaseTool from .serpapi_base_tool import SerpApiBaseTool
from serpapi import HTTPError from pydantic import ConfigDict
try:
from serpapi import HTTPError
except ImportError:
HTTPError = Any
class SerpApiGoogleShoppingToolSchema(BaseModel): class SerpApiGoogleShoppingToolSchema(BaseModel):
"""Input for Google Shopping.""" """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.") 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): class SerpApiGoogleShoppingTool(SerpApiBaseTool):
model_config = ConfigDict(
arbitrary_types_allowed=True, validate_assignment=True, frozen=False
)
name: str = "Google Shopping" name: str = "Google Shopping"
description: str = ( description: str = (
"A tool to perform search on Google shopping with a search_query." "A tool to perform search on Google shopping with a search_query."
@@ -23,20 +37,25 @@ class SerpApiGoogleShoppingTool(SerpApiBaseTool):
**kwargs: Any, **kwargs: Any,
) -> Any: ) -> Any:
try: try:
results = self.client.search({ results = self.client.search(
"engine": "google_shopping", {
"q": kwargs.get("search_query"), "engine": "google_shopping",
"location": kwargs.get("location") "q": kwargs.get("search_query"),
}).as_dict() "location": kwargs.get("location"),
}
).as_dict()
self._omit_fields( self._omit_fields(
results, results,
[r"search_metadata", r"search_parameters", r"serpapi_.+", r"filters", r"pagination"] [
r"search_metadata",
r"search_parameters",
r"serpapi_.+",
r"filters",
r"pagination",
],
) )
return results return results
except HTTPError as e: except HTTPError as e:
return f"An error occurred: {str(e)}. Some parameters may be invalid." return f"An error occurred: {str(e)}. Some parameters may be invalid."