Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: add bing search (#1865)

Echoe55cf53d6a8c
diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.py
index 176cda8e4..daa8ab7da 100644
--- a/camel/toolkits/search_toolkit.py
+++ b/camel/toolkits/search_toolkit.py
@@ -835,6 +835,116 @@ class SearchToolkit(BaseToolkit):
except Exception as e:
return {"error": f"Baidu scraping error: {e!s}"}
+ def search_bing(self, query: str, max_results: int = 5) -> Dict[str, Any]:
+ r"""Use Bing search engine to search information for the given query.
+
+ This function queries the Chinese version of Bing search engine (cn.
+ bing.com) using web scraping to retrieve relevant search results. It
+ extracts search results including titles, snippets, and URLs. This
+ function is particularly useful when the query is in Chinese or when
+ Chinese search results are desired.
+
+ Args:
+ query (str): The search query string to submit to Bing. Works best
+ with Chinese queries or when Chinese results are preferred.
+ max_results (int): Maximum number of results to return.
+ (default: :obj:`5`)
+
+ Returns:
+ Dict ([str, Any]): A dictionary containing either:
+ - 'results': A list of dictionaries, each with:
+ - 'result_id': The index of the result.
+ - 'snippet': A brief description of the search result.
+ - 'title': The title of the search result.
+ - 'link': The URL of the search result.
+ - or 'error': An error message if something went wrong.
+ """
+ from typing import Any, Dict, List, cast
+ from urllib.parse import urlencode
+
+ from bs4 import BeautifulSoup, Tag
+
+ try:
+ query = urlencode({"q": query})
+ url = f'https://cn.bing.com/search?{query}'
+ headers = {
+ "User-Agent": (
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
+ "Chrome/120.0.0.0 Safari/537.36"
+ ),
+ }
+ # Add timeout to prevent hanging
+ response = requests.get(url, headers=headers, timeout=10)
+
+ # Check if the request was successful
+ if response.status_code != 200:
+ return {
+ "error": (
+ f"Bing returned status code: "
+ f"{response.status_code}"
+ )
+ }
+
+ response.encoding = 'utf-8'
+ soup = BeautifulSoup(response.text, 'html.parser')
+
+ b_results_element = soup.find("ol", id="b_results")
+ if b_results_element is None:
+ return {"results": []}
+
+ # Ensure b_results is a Tag and find all li elements
+ b_results_tag = cast(Tag, b_results_element)
+ result_items = b_results_tag.find_all("li")
+
+ results: List[Dict[str, Any]] = []
+ for i in range(min(len(result_items), max_results)):
+ row = result_items[i]
+ if not isinstance(row, Tag):
+ continue
+
+ h2_element = row.find("h2")
+ if h2_element is None:
+ continue
+ h2_tag = cast(Tag, h2_element)
+
+ title = h2_tag.get_text().strip()
+
+ link_tag_element = h2_tag.find("a")
+ if link_tag_element is None:
+ continue
+ link_tag = cast(Tag, link_tag_element)
+
+ link = link_tag.get("href")
+ if link is None:
+ continue
+
+ content_element = row.find("p", class_="b_algoSlug")
+ content_text = ""
+ if content_element is not None and isinstance(
+ content_element, Tag
+ ):
+ content_text = content_element.get_text()
+
+ row_data = {
+ "result_id": i + 1,
+ "snippet": content_text,
+ "title": title,
+ "link": link,
+ }
+ results.append(row_data)
+
+ if not results:
+ return {
+ "warning": "No results found. Check if "
+ "Bing HTML structure has changed."
+ }
+
+ return {"results": results}
+
+ except Exception as e:
+ return {"error": f"Bing scraping error: {e!s}"}
+
def get_tools(self) -> List[FunctionTool]:
r"""Returns a list of FunctionTool objects representing the
functions in the toolkit.
@@ -853,4 +963,5 @@ class SearchToolkit(BaseToolkit):
FunctionTool(self.search_brave),
FunctionTool(self.search_bocha),
FunctionTool(self.search_baidu),
+ FunctionTool(self.search_bing),
]
diff --git a/examples/toolkits/search_toolkit.py b/examples/toolkits/search_toolkit.py
index c3dd9d6a6..243ec4e15 100644
--- a/examples/toolkits/search_toolkit.py
+++ b/examples/toolkits/search_toolkit.py
@@ -17,39 +17,6 @@ from pydantic import BaseModel
from camel.agents import ChatAgent
from camel.toolkits import FunctionTool, SearchToolkit
-agent = ChatAgent(
- system_message="""You are a helpful assistant that can use baidu search
- engine to answer questions.""",
- tools=[FunctionTool(SearchToolkit().search_baidu)],
-)
-
-usr_msg = "今天北京的天气如何"
-
-response = agent.step(input_message=usr_msg, response_format=None)
-
-print(response.msgs[0].content)
-
-"""
-===============================================================================
-今天北京的天气信息可以通过以下链接查看:
-
-1. [中国天气网 - 北京天气预报](http://www.baidu.com/link?
-url=AJhE9PhEO3TmkJ70CUcRsR3NVB3m6wxN5Imdp0ZVsEBK1t8YhtM6YMxrQy3_vRN6dJv4FLHkBCe
-fZURnzHTm9gio-dS4-4MwGVgJe40m7prOoggce2eB0h-3DsllbKMm)
-2. [中国天气网 - 北京天气预报](http://www.baidu.com/link?
-url=1vhNOfl9tV65_104GMQbDnU_fdCZPXDV2BtTJelxdd6isdSZjAHvtoXqOWG3n7D1N-m9zAmOhQG
-c-jEGqiXe9K)
-3. [中国天气网 - 北京天气预报](http://www.baidu.com/link?
-url=Q0URfpodXDpUe1TKBPpToKIyIuCcjSGUR5jorx81g8Pni5XH-Tbc6AXMa7EwCWjBG3jysTZb43S
-6ZCsJOKvPw2EbIlQ_bMu42-5sCraqXlS)
-4. [中国天气网 - 北京天气预报一周](http://www.baidu.com/link?
-url=TtFe8QryJFuwX1kx50YF5WijRcd2TMJRhPudDQvqW7TG4siah68gUZd_frsVWPi1xkYvrxoYL87
-QMH0wSjDYOq)
-
-请点击链接查看详细的天气预报信息。
-===============================================================================
-"""
-
res_simple = SearchToolkit().query_wolfram_alpha(
query="solve 3x-7=11", is_detailed=False
)
@@ -284,3 +251,71 @@ dingFormat":None,"hostPageDisplayUrl":"https://m.sohu.com/a/796245119_121713887
dly":None},"videos":None}
===============================================================================
"""
+
+
+agent = ChatAgent(
+ system_message="""You are a helpful assistant that can use baidu search
+ engine to answer questions.""",
+ tools=[FunctionTool(SearchToolkit().search_baidu)],
+)
+
+usr_msg = "今天北京的天气如何"
+
+response = agent.step(input_message=usr_msg, response_format=None)
+
+print(response.msgs[0].content)
+
+"""
+===============================================================================
+今天北京的天气信息可以通过以下链接查看:
+
+1. [中国天气网 - 北京天气预报](http://www.baidu.com/link?
+url=AJhE9PhEO3TmkJ70CUcRsR3NVB3m6wxN5Imdp0ZVsEBK1t8YhtM6YMxrQy3_vRN6dJv4FLHkBCe
+fZURnzHTm9gio-dS4-4MwGVgJe40m7prOoggce2eB0h-3DsllbKMm)
+2. [中国天气网 - 北京天气预报](http://www.baidu.com/link?
+url=1vhNOfl9tV65_104GMQbDnU_fdCZPXDV2BtTJelxdd6isdSZjAHvtoXqOWG3n7D1N-m9zAmOhQG
+c-jEGqiXe9K)
+3. [中国天气网 - 北京天气预报](http://www.baidu.com/link?
+url=Q0URfpodXDpUe1TKBPpToKIyIuCcjSGUR5jorx81g8Pni5XH-Tbc6AXMa7EwCWjBG3jysTZb43S
+6ZCsJOKvPw2EbIlQ_bMu42-5sCraqXlS)
+4. [中国天气网 - 北京天气预报一周](http://www.baidu.com/link?
+url=TtFe8QryJFuwX1kx50YF5WijRcd2TMJRhPudDQvqW7TG4siah68gUZd_frsVWPi1xkYvrxoYL87
+QMH0wSjDYOq)
+
+请点击链接查看详细的天气预报信息。
+===============================================================================
+"""
+
+bing_call_agent = ChatAgent(
+ system_message="""You are a helpful assistant that can use baidu search
+ engine to answer questions.""",
+ tools=[FunctionTool(SearchToolkit().search_bing)],
+)
+
+bing_usr_msg = "帮忙查询巴黎圣母院最新修复进展"
+
+response = bing_call_agent.step(
+ input_message=bing_usr_msg, response_format=None
+)
+
+print(response.msgs[0].content)
+
+"""
+===============================================================================
+以下是关于巴黎圣母院最新修复进展的一些信息:
+
+1. **时隔4年,灾后余生的巴黎圣母院即将重生** -
+[知乎](https://zhuanlan.zhihu.com/p/619405504)
+
+2. **历时4年,耗资70亿,被烧塌的巴黎圣母院修好了!!** -
+[腾讯网](https://news.qq.com/rain/a/20231018A0329F00)
+
+3. **一票难求!巴黎圣母院重新开放!5年修复离不开来自东方的支持** -
+[新浪财经](https://finance.sina.com.cn/wm/2024-12-08/doc-incyumnp3384392.shtml)
+
+4. **巴黎圣母院浴火重生!建筑学者:勘探报告近3000页,修复工作复杂** -
+[腾讯网](https://news.qq.com/rain/a/20241208A05Q9K00)
+
+这些链接提供了关于巴黎圣母院修复的详细信息和最新进展。
+===============================================================================
+"""
diff --git a/test/toolkits/test_search_functions.py b/test/toolkits/test_search_functions.py
index c68d2dcc2..8d648ab58 100644
--- a/test/toolkits/test_search_functions.py
+++ b/test/toolkits/test_search_functions.py
@@ -248,6 +248,67 @@ def test_search_baidu(mock_get, search_toolkit):
)
+@patch('requests.get')
+def test_search_bing(mock_get, search_toolkit):
+ # Mock the response from Bing search
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.encoding = "utf-8"
+ mock_response.text = """
+ <html>
+ <head><title>Bing Search</title></head>
+ <body>
+ <ol id="b_results">
+ <li>
+ <h2><a href="https://example1.com">Test Title 1</a></h2>
+ <p class="b_algoSlug">Test Snippet 1</p>
+ </li>
+ <li>
+ <h2><a href="https://example2.com">Test Title 2</a></h2>
+ <p class="b_algoSlug">Test Snippet 2</p>
+ </li>
+ </ol>
+ </body>
+ </html>
+ """
+ mock_get.return_value = mock_response
+
+ # Call the function under test
+ result = search_toolkit.search_bing(query="test query", max_results=5)
+
+ # Expected output
+ expected_output = {
+ "results": [
+ {
+ "result_id": 1,
+ "title": "Test Title 1",
+ "snippet": "Test Snippet 1",
+ "link": "https://example1.com",
+ },
+ {
+ "result_id": 2,
+ "title": "Test Title 2",
+ "snippet": "Test Snippet 2",
+ "link": "https://example2.com",
+ },
+ ]
+ }
+
+ # Assertions
+ assert result == expected_output
+ mock_get.assert_called_once_with(
+ "https://cn.bing.com/search?q=test+query",
+ headers={
+ "User-Agent": (
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
+ "Chrome/120.0.0.0 Safari/537.36"
+ ),
+ },
+ timeout=10,
+ )
+
+
@patch('requests.get')
@patch('wolframalpha.Client')
@patch('os.environ.get')