-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Add Serper.dev search support to SearchToolkit #3539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
71e6c85
feat: add support for Serper.dev in SearchToolkit
gaivrt c6f155c
style: strict adherence to contribution guidelines (docstrings, examp…
gaivrt 6eacfc4
style: fix minimal dependency check (move requests import) and fix ex…
gaivrt b9b7662
style: fix serper example formatting to dense wrapped string for lint…
gaivrt 7f1ace3
Merge branch 'master' into master
fengju0213 9df96f1
minor update
fengju0213 06e6a9c
Merge branch 'master' into master
fengju0213 d953edc
Update search_toolkit.py
fengju0213 7c58745
Merge branch 'master' into master
fengju0213 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,55 @@ def __init__( | |
| super().__init__(timeout=timeout) | ||
| self.exclude_domains = exclude_domains | ||
|
|
||
| @api_keys_required( | ||
| [ | ||
| (None, "SERPER_API_KEY"), | ||
| ] | ||
| ) | ||
| def search_serper( | ||
| self, | ||
| query: str, | ||
| page: int = 1, | ||
| location: str = "United States", | ||
| ) -> Dict[str, Any]: | ||
| r"""Use Serper.dev API to perform Google search. | ||
|
|
||
| Args: | ||
| query (str): The search query. | ||
| page (int): The page number of results to retrieve. (default: :obj:`1`) | ||
| location (str): The location for the search results. | ||
| (default: :obj:`"United States"`) | ||
|
|
||
| Returns: | ||
| Dict[str, Any]: The search result dictionary containing 'organic', | ||
| 'peopleAlsoAsk', etc. | ||
| """ | ||
| import json | ||
fengju0213 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| SERPER_API_KEY = os.getenv("SERPER_API_KEY") | ||
|
|
||
| url = "https://google.serper.dev/search" | ||
|
|
||
| payload = json.dumps( | ||
| { | ||
| "q": query, | ||
| "location": location, | ||
| "page": page, | ||
| } | ||
| ) | ||
|
|
||
| headers = { | ||
| "X-API-KEY": SERPER_API_KEY, | ||
| "Content-Type": "application/json", | ||
| } | ||
|
|
||
| try: | ||
| response = requests.post(url, headers=headers, data=payload) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. timeout setting was not used in |
||
| response.raise_for_status() | ||
| return response.json() | ||
| except requests.exceptions.RequestException as e: | ||
| raise RuntimeError(f"Error making request to Serper: {e}") | ||
|
Comment on lines
+104
to
+105
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of raise error msg, return error information would be more agent-friendly |
||
|
|
||
| @dependencies_required("wikipedia") | ||
| def search_wiki(self, entity: str) -> str: | ||
| r"""Search the entity in WikiPedia and return the summary of the | ||
|
|
@@ -376,8 +425,6 @@ def search_brave( | |
| Dict[str, Any]: A dictionary representing a search result. | ||
| """ | ||
|
|
||
| import requests | ||
|
|
||
| BRAVE_API_KEY = os.getenv("BRAVE_API_KEY") | ||
|
|
||
| url = "https://api.search.brave.com/res/v1/web/search" | ||
|
|
@@ -519,8 +566,6 @@ def search_google( | |
| """ | ||
| from urllib.parse import quote | ||
|
|
||
| import requests | ||
|
|
||
| # Validate input parameters | ||
| if not isinstance(start_page, int) or start_page < 1: | ||
| raise ValueError("start_page must be a positive integer") | ||
|
|
@@ -1171,6 +1216,7 @@ def search_alibaba_tongxiao( | |
| message. Each result contains title, snippet, url and other | ||
| metadata. | ||
| """ | ||
|
|
||
| TONGXIAO_API_KEY = os.getenv("TONGXIAO_API_KEY") | ||
|
|
||
| # Validate query length | ||
|
|
@@ -1452,6 +1498,7 @@ def get_tools(self) -> List[FunctionTool]: | |
| representing the functions in the toolkit. | ||
| """ | ||
| return [ | ||
| FunctionTool(self.search_serper), | ||
| FunctionTool(self.search_wiki), | ||
| FunctionTool(self.search_linkup), | ||
| FunctionTool(self.search_google), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better align with other search engine's default value into 10