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 Alibaba Tongxiao Search API Support PR #2127 (#2255)

Wendong-Fan4d75c5496403
diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.py
index bf397834b..67977f15d 100644
--- a/camel/toolkits/search_toolkit.py
+++ b/camel/toolkits/search_toolkit.py
@@ -1065,11 +1065,23 @@ class SearchToolkit(BaseToolkit):
return {"error": f"Exa search failed: {e!s}"}
@api_keys_required([(None, 'TONGXIAO_API_KEY')])
- def search_ali(
+ def search_alibaba_tongxiao(
self,
query: str,
- time_range: Literal["OneDay", "OneWeek", "OneMonth", "OneYear", "NoLimit"] = "NoLimit",
- industry: Optional[Literal["finance", "law", "medical", "internet", "tax", "news_province", "news_center"]] = None,
+ time_range: Literal[
+ "OneDay", "OneWeek", "OneMonth", "OneYear", "NoLimit"
+ ] = "NoLimit",
+ industry: Optional[
+ Literal[
+ "finance",
+ "law",
+ "medical",
+ "internet",
+ "tax",
+ "news_province",
+ "news_center",
+ ]
+ ] = None,
page: int = 1,
return_main_text: bool = False,
return_markdown_text: bool = True,
@@ -1077,41 +1089,48 @@ class SearchToolkit(BaseToolkit):
) -> Dict[str, Any]:
r"""Query the Alibaba Tongxiao search API and return search results.
- This function queries the Alibaba Tongxiao search API, a powerful
- real-time search API that provides structured data from various search
- engines and knowledge base collections. The API is particularly
- effective for Chinese language queries.
+ A powerful search API optimized for Chinese language queries with
+ features:
+ - Enhanced Chinese language understanding
+ - Industry-specific filtering (finance, law, medical, etc.)
+ - Structured data with markdown formatting
+ - Result reranking for relevance
+ - Time-based filtering
Args:
query (str): The search query string (length >= 1 and <= 100).
- time_range (Literal["OneDay", "OneWeek", "OneMonth", "OneYear", "NoLimit"]):
- Time frame filter for search results.
+ time_range (Literal["OneDay", "OneWeek", "OneMonth", "OneYear",
+ "NoLimit"]): Time frame filter for search results.
(default: :obj:`"NoLimit"`)
- industry (Optional[Literal["finance", "law", "medical", "internet", "tax",
- "news_province", "news_center"]]): Industry-specific search filter. When
- specified, only returns results from sites in the specified
- industries. Multiple industries can be comma-separated.
+ industry (Optional[Literal["finance", "law", "medical",
+ "internet", "tax", "news_province", "news_center"]]):
+ Industry-specific search filter. When specified, only returns
+ results from sites in the specified industries. Multiple
+ industries can be comma-separated.
(default: :obj:`None`)
- page (int): Page number for results pagination.
+ page (int): Page number for results pagination.
(default: :obj:`1`)
return_main_text (bool): Whether to include the main text of the
webpage in results. (default: :obj:`True`)
return_markdown_text (bool): Whether to include markdown formatted
content in results. (default: :obj:`True`)
- enable_rerank (bool): Whether to enable result reranking. If
- response time is critical, setting this to False can reduce
+ enable_rerank (bool): Whether to enable result reranking. If
+ response time is critical, setting this to False can reduce
response time by approximately 140ms. (default: :obj:`True`)
Returns:
Dict[str, Any]: A dictionary containing either search results with
- 'requestId' and 'results' keys, or an 'error' key with error message.
- Each result contains title, snippet, url and other metadata.
+ 'requestId' and 'results' keys, or an 'error' key with error
+ message. Each result contains title, snippet, url and other
+ metadata.
"""
TONGXIAO_API_KEY = os.getenv("TONGXIAO_API_KEY")
-
+
# Validate query length
if not query or len(query) > 100:
- return {"error": "Query length must be between 1 and 100 characters"}
+ return {
+ "error": "Query length must be between 1 and 100 characters"
+ }
# API endpoint and parameters
base_url = "https://cloud-iqs.aliyuncs.com/search/genericSearch"
@@ -1164,16 +1183,29 @@ class SearchToolkit(BaseToolkit):
"hostname": item.get("hostname", ""),
}
- # Only include additional fields if they exist and are requested
+ # Only include additional fields if they exist and are
+ # requested
if "summary" in item and item.get("summary"):
result["summary"] = item["summary"]
- elif return_main_text and "mainText" in item and item.get("mainText"):
+ elif (
+ return_main_text
+ and "mainText" in item
+ and item.get("mainText")
+ ):
result["summary"] = item["mainText"]
- if return_main_text and "mainText" in item and item.get("mainText"):
+ if (
+ return_main_text
+ and "mainText" in item
+ and item.get("mainText")
+ ):
result["main_text"] = item["mainText"]
- if return_markdown_text and "markdownText" in item and item.get("markdownText"):
+ if (
+ return_markdown_text
+ and "markdownText" in item
+ and item.get("markdownText")
+ ):
result["markdown_text"] = item["markdownText"]
if "score" in item:
@@ -1193,7 +1225,10 @@ class SearchToolkit(BaseToolkit):
except requests.exceptions.RequestException as e:
return {"error": f"Alibaba Tongxiao search request failed: {e!s}"}
except Exception as e:
- return {"error": f"Unexpected error during Alibaba Tongxiao search: {e!s}"}
+ return {
+ "error": f"Unexpected error during Alibaba Tongxiao "
+ f"search: {e!s}"
+ }
def get_tools(self) -> List[FunctionTool]:
r"""Returns a list of FunctionTool objects representing the
@@ -1215,5 +1250,5 @@ class SearchToolkit(BaseToolkit):
FunctionTool(self.search_baidu),
FunctionTool(self.search_bing),
FunctionTool(self.search_exa),
- FunctionTool(self.search_ali),
+ FunctionTool(self.search_alibaba_tongxiao),
]
diff --git a/examples/toolkits/search_toolkit.py b/examples/toolkits/search_toolkit.py
index 223d79bc4..c1ad8d536 100644
--- a/examples/toolkits/search_toolkit.py
+++ b/examples/toolkits/search_toolkit.py
@@ -395,11 +395,11 @@ on improving efficiency, fault tolerance, and minimizing resource overheads.
===============================================================================
"""
-search_ali_response = SearchToolkit().search_ali(
- query="Alibaba's chip investment for 2025",
- time_range="OneMonth"
+search_alibaba_tongxiao_response = SearchToolkit().search_alibaba_tongxiao(
+ query="Alibaba's chip investment for 2025", time_range="OneMonth"
)
-print(search_ali_response)
+print(search_alibaba_tongxiao_response)
+# ruff: noqa: RUF001
"""
===============================================================================
{'request_id': '78a77a7e004dd97bc18bd907b90d152b', 'results': [
@@ -419,4 +419,4 @@ print(search_ali_response)
{'result_id': 2, 'title': 'technode'.....}
]}
===============================================================================
-"""
\ No newline at end of file
+"""
diff --git a/test/toolkits/test_jina_reranker_toolkit.py b/test/toolkits/test_jina_reranker_toolkit.py
index 418fd43f7..cf9549ba1 100644
--- a/test/toolkits/test_jina_reranker_toolkit.py
+++ b/test/toolkits/test_jina_reranker_toolkit.py
@@ -67,6 +67,9 @@ def test_init_with_custom_device():
model_mock.eval.assert_called_once()
+ reason="Skipping due to Hugging Face authentication issues in CI/CD"
+)
def test_sort_documents():
r"""Test the _sort_documents method."""
toolkit = JinaRerankerToolkit()
@@ -79,6 +82,9 @@ def test_sort_documents():
assert result == [("doc2", 0.9), ("doc1", 0.5), ("doc3", 0.1)]
+ reason="Skipping due to Hugging Face authentication issues in CI/CD"
+)
def test_sort_documents_with_mismatched_lengths():
r"""Test _sort_documents with mismatched document and score lists."""
toolkit = JinaRerankerToolkit()
@@ -129,6 +135,9 @@ def test_rerank_text_documents_with_custom_max_length(reranker_toolkit):
assert result == [("doc2", 0.8), ("doc1", 0.6)]
+ reason="Skipping due to Hugging Face authentication issues in CI/CD"
+)
def test_rerank_text_documents_model_not_initialized():
r"""Test reranking text documents when model is not initialized."""
toolkit = JinaRerankerToolkit()
diff --git a/test/toolkits/test_search_functions.py b/test/toolkits/test_search_functions.py
index 8d648ab58..8009f6602 100644
--- a/test/toolkits/test_search_functions.py
+++ b/test/toolkits/test_search_functions.py
@@ -589,3 +589,59 @@ def test_search_bocha_error(search_toolkit):
assert "error" in result
assert "Connection error" in result["error"]
+
+
+@patch('requests.get')
+def test_search_alibaba_tongxiao(mock_get, search_toolkit):
+ # Mock the response from Alibaba Tongxiao search
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.json.return_value = {
+ "requestId": "test-request-id",
+ "pageItems": [
+ {
+ "title": "Test Title 1",
+ "snippet": "Test Snippet 1",
+ "link": "https://example1.com",
+ "hostname": "example1.com",
+ "markdownText": "# Test Markdown 1",
+ }
+ ],
+ }
+ mock_get.return_value = mock_response
+
+ # Mock environment variables and call the function
+ with patch.dict(os.environ, {'TONGXIAO_API_KEY': 'fake_api_key'}):
+ result = search_toolkit.search_alibaba_tongxiao(
+ query="test query", return_markdown_text=True
+ )
+
+ # Verify the request was made correctly
+ mock_get.assert_called_once_with(
+ "https://cloud-iqs.aliyuncs.com/search/genericSearch",
+ headers={"X-API-Key": "fake_api_key"},
+ params={
+ "query": "test query",
+ "timeRange": "NoLimit",
+ "page": 1,
+ "returnMainText": "false",
+ "returnMarkdownText": "true",
+ "enableRerank": "true",
+ },
+ timeout=10,
+ )
+
+ # Check if the result is as expected
+ assert result == {
+ "request_id": "test-request-id",
+ "results": [
+ {
+ "result_id": 1,
+ "title": "Test Title 1",
+ "snippet": "Test Snippet 1",
+ "url": "https://example1.com",
+ "hostname": "example1.com",
+ "markdown_text": "# Test Markdown 1",
+ }
+ ],
+ }