Skip to content

Commit 9df96f1

Browse files
committed
minor update
1 parent 7f1ace3 commit 9df96f1

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

camel/toolkits/search_toolkit.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import warnings
1616
from typing import Any, Dict, List, Literal, Optional, TypeAlias, Union, cast
1717

18-
18+
import requests
1919

2020
from camel.logger import get_logger
2121
from camel.toolkits.base import BaseToolkit
@@ -54,7 +54,7 @@ def __init__(
5454
"""
5555
super().__init__(timeout=timeout)
5656
self.exclude_domains = exclude_domains
57-
57+
5858
@api_keys_required(
5959
[
6060
(None, "SERPER_API_KEY"),
@@ -80,8 +80,6 @@ def search_serper(
8080
"""
8181
import json
8282

83-
import requests
84-
8583
SERPER_API_KEY = os.getenv("SERPER_API_KEY")
8684

8785
url = "https://google.serper.dev/search"
@@ -427,8 +425,6 @@ def search_brave(
427425
Dict[str, Any]: A dictionary representing a search result.
428426
"""
429427

430-
import requests
431-
432428
BRAVE_API_KEY = os.getenv("BRAVE_API_KEY")
433429

434430
url = "https://api.search.brave.com/res/v1/web/search"
@@ -570,8 +566,6 @@ def search_google(
570566
"""
571567
from urllib.parse import quote
572568

573-
import requests
574-
575569
# Validate input parameters
576570
if not isinstance(start_page, int) or start_page < 1:
577571
raise ValueError("start_page must be a positive integer")
@@ -821,8 +815,6 @@ def search_bocha(
821815
"""
822816
import json
823817

824-
import requests
825-
826818
BOCHA_API_KEY = os.getenv("BOCHA_API_KEY")
827819

828820
url = "https://api.bochaai.com/v1/web-search"
@@ -874,8 +866,6 @@ def search_baidu(
874866
"""
875867
from bs4 import BeautifulSoup
876868

877-
import requests
878-
879869
try:
880870
url = "https://www.baidu.com/s"
881871
headers = {
@@ -960,8 +950,6 @@ def search_bing(
960950
from typing import Any, Dict, List, cast
961951
from urllib.parse import urlencode
962952

963-
import requests
964-
965953
from bs4 import BeautifulSoup, Tag
966954

967955
try:
@@ -1228,7 +1216,6 @@ def search_alibaba_tongxiao(
12281216
message. Each result contains title, snippet, url and other
12291217
metadata.
12301218
"""
1231-
import requests
12321219

12331220
TONGXIAO_API_KEY = os.getenv("TONGXIAO_API_KEY")
12341221

@@ -1412,7 +1399,7 @@ def get_tools(self) -> List[FunctionTool]:
14121399
representing the functions in the toolkit.
14131400
"""
14141401
return [
1415-
FunctionTool(self.search_serper),
1402+
FunctionTool(self.search_serper),
14161403
FunctionTool(self.search_wiki),
14171404
FunctionTool(self.search_linkup),
14181405
FunctionTool(self.search_google),

examples/toolkits/search_toolkit.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ class PersonInfo(BaseModel):
421421
""" # noqa: E501
422422

423423

424-
425424
# Example using Serper search
426425
if os.getenv("SERPER_API_KEY"):
427426
serper_response = SearchToolkit().search_serper(
@@ -480,4 +479,4 @@ class PersonInfo(BaseModel):
480479
Apple Account or create a new account to start using Apple services
481480
...', 'position': 10}], 'credits': 1}
482481
===============================================================================
483-
"""
482+
"""

test/toolkits/test_search_functions.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ def test_search_metaso_invalid_json(mock_https_connection, search_toolkit):
900900
mock_https_connection.assert_called_once_with("metaso.cn")
901901
mock_conn.request.assert_called_once()
902902

903+
903904
@patch('requests.post')
904905
def test_search_serper_success(mock_post, search_toolkit):
905906
"""Test successful Serper search."""
@@ -913,24 +914,24 @@ def test_search_serper_success(mock_post, search_toolkit):
913914
"gl": "us",
914915
"hl": "en",
915916
"num": 10,
916-
"type": "search"
917+
"type": "search",
917918
},
918919
"organic": [
919920
{
920921
"title": "Apple",
921922
"link": "https://www.apple.com/",
922923
"snippet": "Discover the innovative world of Apple...",
923-
"position": 1
924+
"position": 1,
924925
}
925-
]
926+
],
926927
}
927928
mock_post.return_value = mock_response
928929

929930
with patch.dict(os.environ, {'SERPER_API_KEY': 'test_key'}):
930931
result = search_toolkit.search_serper(query="apple inc")
931932

932933
assert result == mock_response.json.return_value
933-
934+
934935
# Verify request
935936
mock_post.assert_called_once()
936937
args, kwargs = mock_post.call_args
@@ -944,7 +945,7 @@ def test_search_serper_success(mock_post, search_toolkit):
944945
assert json.loads(kwargs['data']) == {
945946
"q": "apple inc",
946947
"location": "United States",
947-
"page": 1
948+
"page": 1,
948949
}
949950

950951

@@ -956,5 +957,5 @@ def test_search_serper_error(mock_post, search_toolkit):
956957
with patch.dict(os.environ, {'SERPER_API_KEY': 'test_key'}):
957958
with pytest.raises(RuntimeError) as excinfo:
958959
search_toolkit.search_serper(query="test")
959-
960+
960961
assert "Error making request to Serper" in str(excinfo.value)

0 commit comments

Comments
 (0)