Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/app/tool: A new tool for Baidu search #386

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions app/tool/baidu_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import asyncio
from typing import List
from baidusearch.baidusearch import search
# from app.tool.base import BaseTool
from base import BaseTool

class BaiduSearch(BaseTool):
name: str = "baidu_search"
description: str = """Perform a Baidu search and return a list of relevant links.
Use this tool when you need to find information on the web, get up-to-date data, or research specific topics.
The tool returns a list of URLs that match the search query.
"""
parameters: dict = {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "(required) The search query to submit to Baidu.",
},
"num_results": {
"type": "integer",
"description": "(optional) The number of search results to return. Default is 10.",
"default": 10,
},
},
"required": ["query"],
}

async def execute(self, query: str, num_results: int = 10) -> List[str]:
"""
Execute a Baidu search and return a list of URLs.

Args:
query (str): The search query to submit to Baidu.
num_results (int, optional): The number of search results to return. Default is 10.

Returns:
List[str]: A list of URLs matching the search query.
"""
print('baidu search query:', query)
# Run the search in a thread pool to prevent blocking
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(
None, lambda: search(query, num_results=num_results)
)
links = []
for item in response:
url = item['url']
if url.startswith('http'):
links.append(url)
# print('baidu search query links:', links)
return links

if __name__ == '__main__':
links = asyncio.run(BaiduSearch().execute("Manux", 5))
print('baidu search query links:', links)