🐫 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
feat: add metaso.cn API search (#2986)
@hu-mengliangce56cf0e4e6f
diff --git a/.env b/.envindex 3238f48d4..1e2b5de6d 100644--- a/.env+++ b/.env@@ -124,6 +124,9 @@#LANGFUSE_SECRET_KEY="Fill your API key here"#LANGFUSE_PUBLIC_KEY="Fill your API key here"+#METASO API(https://metaso.cn/search-api/api-keys)+#METASO_API_KEY="Fill your API key here"+# E2B# E2B_API_KEY="Fill your e2b or e2b-compatible sandbox provider API Key here"# E2B_DOMAIN="Fill your custom e2b domain here"diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.pyindex 4e3c1585d..587ffd003 100644--- a/camel/toolkits/search_toolkit.py+++ b/camel/toolkits/search_toolkit.py@@ -1317,6 +1317,67 @@ class SearchToolkit(BaseToolkit):f"search: {e!s}"}+ @api_keys_required([(None, 'METASO_API_KEY')])+ def search_metaso(+ self,+ query: str,+ page: int = 1,+ include_summary: bool = False,+ include_raw_content: bool = False,+ concise_snippet: bool = False,+ scope: Literal[+ "webpage", "document", "scholar", "image", "video", "podcast"+ ] = "webpage",+ ) -> dict[str, Any]:+ r"""Perform a web search using the metaso.cn API.++ Args:+ query (str): The search query string.+ page (int): Page number, default is 1.+ include_summary (bool): Whether to include summary in the result.+ include_raw_content (bool): Whether to include raw content in the result.+ concise_snippet (bool): Whether to return concise snippet.+ scope (Literal[+ "webpage", "document", "scholar", "image", "video", "podcast"+ ]): Search scope, default is 'webpage'.++ Returns:+ dict: Search results or error information.+ """+ import http.client+ import json++ # It is recommended to put the token in environment variable for security+ import os+ METASO_API_KEY = os.getenv("METASO_API_KEY")++ conn = http.client.HTTPSConnection("metaso.cn")+ payload = json.dumps({+ "q": query,+ "scope": scope,+ "includeSummary": include_summary,+ "page": str(page),+ "includeRawContent": include_raw_content,+ "conciseSnippet": concise_snippet+ })+ headers = {+ 'Authorization': f'Bearer {METASO_API_KEY}',+ 'Accept': 'application/json',+ 'Content-Type': 'application/json'+ }+ try:+ conn.request("POST", "/api/v1/search", payload, headers)+ res = conn.getresponse()+ data = res.read()+ result = data.decode("utf-8")+ try:+ return json.loads(result)+ except Exception:+ return {"error": f"Metaso returned content could not be parsed: {result}"}+ except Exception as e:+ return {"error": f"Metaso search failed: {e}"}++def get_tools(self) -> List[FunctionTool]:r"""Returns a list of FunctionTool objects representing thefunctions in the toolkit.@@ -1337,4 +1398,5 @@ class SearchToolkit(BaseToolkit):FunctionTool(self.search_bing),FunctionTool(self.search_exa),FunctionTool(self.search_alibaba_tongxiao),+ FunctionTool(self.search_metaso),]diff --git a/examples/toolkits/search_toolkit.py b/examples/toolkits/search_toolkit.pyindex 692d00ceb..b59a370e7 100644--- a/examples/toolkits/search_toolkit.py+++ b/examples/toolkits/search_toolkit.py@@ -378,3 +378,42 @@ print(search_alibaba_tongxiao_response)]}==============================================================================="""+# Example using metaso response+metaso_response = SearchToolkit().search_metaso(+ query="What is CAMEL-AI?",+ scope="webpage",+ page= 1,+)++print(metaso_response)+"""+===============================================================================+{'credits': 10, 'searchParameters': {'q': 'What is CAMEL-AI?', 'scope': 'webpage',+ 'page': 1, 'includeSummary': False, 'includeRawContent': False, 'conciseSnippet':+ False, 'format': 'chat_completions'}, 'webpages': [{'title': '使用CAMEL框架和Qwen+ 模型自动进行数据获取及报告与知识 ...', 'link': 'https://developer.aliyun.com/articl+ e/1647631', 'score': 'high', 'snippet': '**关于CAMEL AI**\n* * *\n**"Finding the+ Scaling Law of Agents"**\nCAMEL AI (https://www.camel-ai.org) 是一个致力于构建基+ 于大语言模型(LLMs)的多智能体系统的开源平台,旨在推动人工智能的发展。就像AI领域的“沙+ 漠之舟”,CAMEL不仅在信息的广袤领域里稳步前行,更为实现AGI探索出一条前沿之路。CAMEL框架+ 是最早基于ChatGPT的autonomous agents的知名项目,已被NeurlPS 2023录用。', 'position'+ : 1, 'authors': ['阿里云开发者社区'], 'date': '2024年12月31日'}, {'title':+ 'CAMEL-AI团队参与发表Nature子刊啦!聚焦LLM如何重塑 ...', 'link': 'https://cloud.+ tencent.com/developer/article/2475624', 'score': 'high', 'snippet': 'Finding+ the Scaling Law of Agents"\nCAMEL AI (https://www.camel-ai.org) 是一个致力于构建+ 基于大语言模型)LLMs)的多智能体系统的开源平台,旨在推动人工智能的发展。\n就像AI领域的+ “沙漠之舟”,CAMEL不仅在信息的广袤领域里稳步前行,更为实现AGI探索出一条前沿之路。\n+ CAMEL框架是最早基于ChatGPT的autonomous agents的知名项目,已被NeurlPS 2023录用。\n+ CAMEL AI框架设计上极具灵活性与模块化,包含多种智能体实现、专业领域提示示例以及AI数据探+ 索功能等模块,适应各种高效决策与复杂数据处理需求。\n通过协作运作的智能体队列,CAMEL AI+ 不仅在金融、零售、医疗等领域展示出强大的适用性和创新性,还大大提升了自适应能力......+ 'position': 9, 'authors': ['KubeNine'], 'date': '2025年04月15日'}, {'title': '+ RAG应用的评估-CSDN博客', 'link': 'https://blog.csdn.net/weixin_44245188/article/+ details/148157069', 'score': 'high', 'snippet': "Query: 什么是CAMEL AI?\n+ Expected Answers: ['CAMEL AI 是一个开源的、社区驱动的AI框架。']\nRetrieved+ Results: ['CAMEL AI 介绍\\n\\nCAMEL AI 是一个开源的、社区驱动的AI框架,旨在简化AI应+ 用的开发和部署。该框架提供了多种功能模块,包括数据加载、特征工程、模型训练和部署等。']+ \nPrecision: 1.0000\nRecall: 1.0000\nF1 Score: 1.0000\nAverage+ Similarity: 0.9584", 'position': 10, 'date': '2025年05月23日'}], 'total': 38}+===============================================================================+"""\ No newline at end of filediff --git a/test/toolkits/test_search_functions.py b/test/toolkits/test_search_functions.pyindex 861c13d63..5ce215511 100644--- a/test/toolkits/test_search_functions.py+++ b/test/toolkits/test_search_functions.py@@ -833,3 +833,118 @@ def test_search_alibaba_tongxiao(mock_get, search_toolkit):}],}++@patch('http.client.HTTPSConnection')+def test_search_metaso_success(mock_https_connection, search_toolkit):+ # Mock the connection and response+ mock_conn = MagicMock()+ mock_https_connection.return_value = mock_conn++ mock_response = MagicMock()+ mock_response.read.return_value = b'{"results": [{"title": "Test Title", "url": "https://example.com", "snippet": "Test snippet"}], "total": 1}'+ mock_conn.getresponse.return_value = mock_response++ # Mock environment variable+ with patch.dict(os.environ, {'METASO_API_KEY': 'test_api_key'}):+ result = search_toolkit.search_metaso(query="test query")++ # Verify the result+ expected_result = {+ "results": [+ {+ "title": "Test Title",+ "url": "https://example.com",+ "snippet": "Test snippet"+ }+ ],+ "total": 1+ }+ assert result == expected_result++ # Verify the API call+ mock_https_connection.assert_called_once_with("metaso.cn")+ mock_conn.request.assert_called_once()++ # Verify request parameters+ call_args = mock_conn.request.call_args+ assert call_args[0][0] == "POST"+ assert call_args[0][1] == "/api/v1/search"++ # Verify headers+ headers = call_args[0][3]+ assert headers['Authorization'] == 'Bearer test_api_key'+ assert headers['Accept'] == 'application/json'+ assert headers['Content-Type'] == 'application/json'+++@patch('http.client.HTTPSConnection')+def test_search_metaso_with_parameters(mock_https_connection, search_toolkit):+ # Mock the connection and response+ mock_conn = MagicMock()+ mock_https_connection.return_value = mock_conn++ mock_response = MagicMock()+ mock_response.read.return_value = b'{"results": [], "total": 0}'+ mock_conn.getresponse.return_value = mock_response++ with patch.dict(os.environ, {'METASO_API_KEY': 'test_api_key'}):+ search_toolkit.search_metaso(+ query="test query",+ page=2,+ include_summary=True,+ include_raw_content=True,+ concise_snippet=True,+ scope="document"+ )++ # Verify the payload contains correct parameters+ call_args = mock_conn.request.call_args+ payload = call_args[0][2]+ import json+ payload_data = json.loads(payload)++ assert payload_data["q"] == "test query"+ assert payload_data["page"] == "2"+ assert payload_data["includeSummary"] is True+ assert payload_data["includeRawContent"] is True+ assert payload_data["conciseSnippet"] is True+ assert payload_data["scope"] == "document"+++@patch('http.client.HTTPSConnection')+def test_search_metaso_connection_error(mock_https_connection, search_toolkit):+ # Mock connection error+ mock_conn = MagicMock()+ mock_https_connection.return_value = mock_conn+ mock_conn.request.side_effect = Exception("Connection failed")++ with patch.dict(os.environ, {'METASO_API_KEY': 'test_api_key'}):+ result = search_toolkit.search_metaso(query="test query")++ assert "error" in result+ assert "Metaso search failed: Connection failed" in result["error"]++ # Verify connection was attempted+ mock_https_connection.assert_called_once_with("metaso.cn")+ mock_conn.request.assert_called_once()+++@patch('http.client.HTTPSConnection')+def test_search_metaso_invalid_json(mock_https_connection, search_toolkit):+ # Mock invalid JSON response+ mock_conn = MagicMock()+ mock_https_connection.return_value = mock_conn++ mock_response = MagicMock()+ mock_response.read.return_value = b'invalid json response'+ mock_conn.getresponse.return_value = mock_response++ with patch.dict(os.environ, {'METASO_API_KEY': 'test_api_key'}):+ result = search_toolkit.search_metaso(query="test query")++ assert "error" in result+ assert "Metaso returned content could not be parsed" in result["error"]++ # Verify connection was attempted+ mock_https_connection.assert_called_once_with("metaso.cn")+ mock_conn.request.assert_called_once()