🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
enhance: Enhance PR #2901, excluding domains from SearchToolkit definition rather than search_google function (#2905)
Boer-Zhang905160258fb4
diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.pyindex f7c9d7e88..0a0d44842 100644--- a/camel/toolkits/search_toolkit.py+++ b/camel/toolkits/search_toolkit.py@@ -36,6 +36,7 @@ class SearchToolkit(BaseToolkit):self,timeout: Optional[float] = None,number_of_result_pages: int = 10,+ exclude_domains: Optional[List[str]] = None,):r"""Initializes the RedditToolkit with the specified number of retriesand delay.@@ -45,9 +46,14 @@ class SearchToolkit(BaseToolkit):(default: :obj:`None`)number_of_result_pages (int): The number of result pages toretrieve. (default: :obj:`10`)+ exclude_domains (Optional[List[str]]): List of domains to+ exclude from search results. Currently only supported+ by the `search_google` function.+ (default: :obj:`None`)"""super().__init__(timeout=timeout)self.number_of_result_pages = number_of_result_pages+ self.exclude_domains = exclude_domains@dependencies_required("wikipedia")def search_wiki(self, entity: str) -> str:@@ -438,7 +444,6 @@ class SearchToolkit(BaseToolkit):self,query: str,search_type: str = "web",- exclude_domains: Optional[List[str]] = None,) -> List[Dict[str, Any]]:r"""Use Google search engine to search information for the given query.@@ -446,9 +451,6 @@ class SearchToolkit(BaseToolkit):query (str): The query to be searched.search_type (str): The type of search to perform. Either "web" forweb pages or "image" for image search. (default: "web")- exclude_domains (Optional[List[str]]): A list of domains to exclude- from the search results. If provided, the query will be modified.- (default: :obj:`None`)Returns:List[Dict[str, Any]]: A list of dictionaries where each dictionary@@ -507,10 +509,10 @@ class SearchToolkit(BaseToolkit):search_language = "en"modified_query = query- if exclude_domains:+ if self.exclude_domains:# Use Google's -site: operator to exclude domainsexclusion_terms = " ".join(- [f"-site:{domain}" for domain in exclude_domains]+ [f"-site:{domain}" for domain in self.exclude_domains])modified_query = f"{query} {exclusion_terms}"logger.debug(f"Excluded domains, modified query: {modified_query}")diff --git a/examples/toolkits/search_google_with_exclusion.py b/examples/toolkits/search_google_with_exclusion.pyindex 8c6bb6cd2..fa21878a9 100644--- a/examples/toolkits/search_google_with_exclusion.py+++ b/examples/toolkits/search_google_with_exclusion.py@@ -26,14 +26,27 @@ search_result = toolkit.search_google(query="Python Wikipedia")print("Search results for 'Python Wikipedia':\n")for result in search_result:print(result['url'])-search_result = toolkit.search_google(- query="Python Wikipedia",+toolkit = SearchToolkit(+ number_of_result_pages=3,exclude_domains=["wikipedia.org",- "wikipedia-api.readthedocs.io",- "wikipedia.readthedocs.io",],)+search_result = toolkit.search_google(query="Python Wikipedia")print("\nExcluding specified domains related to wikipedia:\n")for result in search_result:print(result['url'])++"""+Search results for 'Python Wikipedia':+https://en.wikipedia.org/wiki/Python_(programming_language)+https://wiki.python.org/moin/FrontPage+https://pypi.org/project/wikipedia/++Excluding specified domains related to wikipedia:+++https://pypi.org/project/wikipedia/+https://wiki.python.org/moin/FrontPage+https://www.reddit.com/r/Python/comments/3tn216/scraping_wikipedia/+"""