Skip to content

Commit bb0ddd6

Browse files
committed
feat(tool): 添加百度和必应搜索引擎支持
- 新增 BaiduSearch 和 BingSearch 工具类 - 更新配置文件,增加搜索引擎配置选项 - 添加必要的依赖包
1 parent d847b55 commit bb0ddd6

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

app/tool/baidu_search.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import asyncio
2+
from typing import List
3+
4+
from baidusearch.baidusearch import search
5+
from app.tool.base import BaseTool
6+
7+
8+
class BaiduSearch(BaseTool):
9+
name: str = "baidu_search"
10+
description: str = """Perform a Baidu search and return a list of relevant links.
11+
Use this tool when you need to find information on the web, get up-to-date data, or research specific topics.
12+
The tool returns a list of URLs that match the search query.
13+
"""
14+
parameters: dict = {
15+
"type": "object",
16+
"properties": {
17+
"query": {
18+
"type": "string",
19+
"description": "(required) The search query to submit to Baidu.",
20+
},
21+
"num_results": {
22+
"type": "integer",
23+
"description": "(optional) The number of search results to return. Default is 10.",
24+
"default": 10,
25+
},
26+
},
27+
"required": ["query"],
28+
}
29+
30+
async def execute(self, query: str, num_results: int = 10) -> List[str]:
31+
"""
32+
Execute a Baidu search and return a list of URLs.
33+
34+
Args:
35+
query (str): The search query to submit to Baidu.
36+
num_results (int, optional): The number of search results to return. Default is 10.
37+
38+
Returns:
39+
List[str]: A list of URLs matching the search query.
40+
"""
41+
# Run the search in a thread pool to prevent blocking
42+
loop = asyncio.get_event_loop()
43+
links = await loop.run_in_executor(
44+
None, lambda: [result['url'] for result in search(query, num_results=num_results)]
45+
)
46+
47+
return links

app/tool/bing_search.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import aiohttp
2+
from bs4 import BeautifulSoup
3+
from typing import List
4+
5+
from app.tool.base import BaseTool
6+
7+
8+
class BingSearch(BaseTool):
9+
name: str = "bing_search"
10+
description: str = """使用必应搜索返回相关链接列表。
11+
当需要查找网络信息、获取最新数据或研究特定主题时使用此工具。
12+
该工具返回与搜索查询匹配的URL列表。
13+
"""
14+
parameters: dict = {
15+
"type": "object",
16+
"properties": {
17+
"query": {
18+
"type": "string",
19+
"description": "(必填) 提交给必应的搜索查询。",
20+
},
21+
"num_results": {
22+
"type": "integer",
23+
"description": "(可选) 要返回的搜索结果数量。默认为10。",
24+
"default": 10,
25+
},
26+
},
27+
"required": ["query"],
28+
}
29+
30+
async def execute(self, query: str, num_results: int = 10) -> List[str]:
31+
"""
32+
执行必应搜索并返回URL列表。
33+
34+
参数:
35+
query (str): 要提交给必应的搜索查询。
36+
num_results (int, optional): 要返回的搜索结果数量。默认为10。
37+
38+
返回:
39+
List[str]: 与搜索查询匹配的URL列表。
40+
"""
41+
headers = {
42+
"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"
43+
}
44+
search_url = f"https://www.bing.com/search?q={query}"
45+
46+
async with aiohttp.ClientSession() as session:
47+
try:
48+
async with session.get(search_url, headers=headers) as response:
49+
response.raise_for_status()
50+
html = await response.text()
51+
except Exception as e:
52+
raise RuntimeError(f"必应搜索请求失败: {str(e)}")
53+
54+
soup = BeautifulSoup(html, 'html.parser')
55+
links = []
56+
57+
# 必应搜索结果链接通常在类名为"b_algo"的div内,具体选择器可能需要根据实际页面结构调整
58+
for result in soup.select('.b_algo'):
59+
a_tag = result.select_one('a')
60+
if a_tag and 'href' in a_tag.attrs:
61+
link = a_tag['href']
62+
links.append(link)
63+
if len(links) >= num_results:
64+
break
65+
66+
return links[:num_results]

config/config.example.toml

+4
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ api_key = "sk-..."
2525
[server]
2626
host = "localhost"
2727
port = 5172
28+
29+
# Search engine configuration
30+
[search]
31+
default_engine = "google" # Available options: google, baidu, bing

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ uvicorn~=0.34.0
1515
unidiff~=0.7.5
1616
browser-use~=0.1.40
1717
googlesearch-python~=1.3.0
18+
python-baidu-api~=0.1.1
19+
py-bing-search~=0.2.7
1820

1921
aiofiles~=24.1.0
2022
pydantic_core~=2.27.2

0 commit comments

Comments
 (0)