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

enhance: add metaso.cn API search PR2986 (#2991)

Wendong-Fande438432bddf
diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.py
index 587ffd003..b8e270792 100644
--- a/camel/toolkits/search_toolkit.py
+++ b/camel/toolkits/search_toolkit.py
@@ -1328,42 +1328,47 @@ class SearchToolkit(BaseToolkit):
scope: Literal[
"webpage", "document", "scholar", "image", "video", "podcast"
] = "webpage",
- ) -> dict[str, Any]:
+ ) -> 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.
+ page (int): Page number. (default: :obj:`1`)
include_summary (bool): Whether to include summary in the result.
- include_raw_content (bool): Whether to include raw content in the result.
+ (default: :obj:`False`)
+ include_raw_content (bool): Whether to include raw content in the
+ result. (default: :obj:`False`)
concise_snippet (bool): Whether to return concise snippet.
- scope (Literal[
- "webpage", "document", "scholar", "image", "video", "podcast"
- ]): Search scope, default is 'webpage'.
+ (default: :obj:`False`)
+ scope (Literal["webpage", "document", "scholar", "image", "video",
+ "podcast"]): Search scope. (default: :obj:`"webpage"`)
Returns:
- dict: Search results or error information.
+ Dict[str, Any]: Search results or error information.
"""
import http.client
import json
- # It is recommended to put the token in environment variable for security
- import os
+ # It is recommended to put the token in environment variable for
+ # security
+
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
- })
+ 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'
+ 'Content-Type': 'application/json',
}
try:
conn.request("POST", "/api/v1/search", payload, headers)
@@ -1373,11 +1378,12 @@ class SearchToolkit(BaseToolkit):
try:
return json.loads(result)
except Exception:
- return {"error": f"Metaso returned content could not be parsed: {result}"}
+ 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 the
functions in the toolkit.
diff --git a/examples/toolkits/search_toolkit.py b/examples/toolkits/search_toolkit.py
index b59a370e7..0aaf64489 100644
--- a/examples/toolkits/search_toolkit.py
+++ b/examples/toolkits/search_toolkit.py
@@ -382,7 +382,7 @@ print(search_alibaba_tongxiao_response)
metaso_response = SearchToolkit().search_metaso(
query="What is CAMEL-AI?",
scope="webpage",
- page= 1,
+ page=1,
)
print(metaso_response)
@@ -416,4 +416,4 @@ print(metaso_response)
\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 file
+""" # noqa: E501
diff --git a/test/toolkits/test_search_functions.py b/test/toolkits/test_search_functions.py
index 5ce215511..1ff21e9fb 100644
--- a/test/toolkits/test_search_functions.py
+++ b/test/toolkits/test_search_functions.py
@@ -834,6 +834,7 @@ 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
@@ -841,7 +842,11 @@ def test_search_metaso_success(mock_https_connection, search_toolkit):
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_response.read.return_value = (
+ b'{"results": [{"title": "Test Title", '
+ b'"url": "https://example.com", "snippet": "Test snippet"}], '
+ b'"total": 1}'
+ )
mock_conn.getresponse.return_value = mock_response
# Mock environment variable
@@ -854,10 +859,10 @@ def test_search_metaso_success(mock_https_connection, search_toolkit):
{
"title": "Test Title",
"url": "https://example.com",
- "snippet": "Test snippet"
+ "snippet": "Test snippet",
}
],
- "total": 1
+ "total": 1,
}
assert result == expected_result
@@ -894,13 +899,14 @@ def test_search_metaso_with_parameters(mock_https_connection, search_toolkit):
include_summary=True,
include_raw_content=True,
concise_snippet=True,
- scope="document"
+ 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"