mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-16 11:38:31 +00:00
Merge pull request #197 from beowolx/bugfix/serperdev-missing-country
fix(serper-dev): restore search localization parameters
This commit is contained in:
@@ -26,7 +26,10 @@ from crewai_tools import SerperDevTool
|
||||
tool = SerperDevTool(
|
||||
n_results=10, # Optional: Number of results to return (default: 10)
|
||||
save_file=False, # Optional: Save results to file (default: False)
|
||||
search_type="search" # Optional: Type of search - "search" or "news" (default: "search")
|
||||
search_type="search", # Optional: Type of search - "search" or "news" (default: "search")
|
||||
country="us", # Optional: Country for search (default: "")
|
||||
location="New York", # Optional: Location for search (default: "")
|
||||
locale="en-US" # Optional: Locale for search (default: "")
|
||||
)
|
||||
|
||||
# Execute a search
|
||||
|
||||
@@ -2,7 +2,7 @@ import datetime
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Type
|
||||
from typing import Any, Type, Optional
|
||||
|
||||
import requests
|
||||
from crewai.tools import BaseTool
|
||||
@@ -45,6 +45,9 @@ class SerperDevTool(BaseTool):
|
||||
n_results: int = 10
|
||||
save_file: bool = False
|
||||
search_type: str = "search"
|
||||
country: Optional[str] = ""
|
||||
location: Optional[str] = ""
|
||||
locale: Optional[str] = ""
|
||||
|
||||
def _get_search_url(self, search_type: str) -> str:
|
||||
"""Get the appropriate endpoint URL based on search type."""
|
||||
@@ -146,11 +149,20 @@ class SerperDevTool(BaseTool):
|
||||
def _make_api_request(self, search_query: str, search_type: str) -> dict:
|
||||
"""Make API request to Serper."""
|
||||
search_url = self._get_search_url(search_type)
|
||||
payload = json.dumps({"q": search_query, "num": self.n_results})
|
||||
payload = {"q": search_query, "num": self.n_results}
|
||||
|
||||
if self.country != "":
|
||||
payload["gl"] = self.country
|
||||
if self.location != "":
|
||||
payload["location"] = self.location
|
||||
if self.locale != "":
|
||||
payload["hl"] = self.locale
|
||||
|
||||
headers = {
|
||||
"X-API-KEY": os.environ["SERPER_API_KEY"],
|
||||
"content-type": "application/json",
|
||||
}
|
||||
payload = json.dumps(payload)
|
||||
|
||||
response = None
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user