From f539a64b104d9d14d8770839e66b69b91a488f0a Mon Sep 17 00:00:00 2001 From: mollymzli Date: Mon, 10 Mar 2025 15:36:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=EF=BC=9ABaiduSearch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/tool/baidu_search.py | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 app/tool/baidu_search.py diff --git a/app/tool/baidu_search.py b/app/tool/baidu_search.py new file mode 100644 index 000000000..5b4113d0a --- /dev/null +++ b/app/tool/baidu_search.py @@ -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) + From 7a34df9be2a9286f394bd390bfbd9fc84443cbab Mon Sep 17 00:00:00 2001 From: mollymzli Date: Mon, 10 Mar 2025 19:08:14 +0800 Subject: [PATCH 2/2] fix: baidu_search --- app/agent/manus.py | 4 ++-- app/prompt/manus.py | 4 +++- app/tool/baidu_search.py | 12 ++---------- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/app/agent/manus.py b/app/agent/manus.py index 7fa4b698a..cef6b7a51 100644 --- a/app/agent/manus.py +++ b/app/agent/manus.py @@ -7,7 +7,7 @@ from app.tool.file_saver import FileSaver from app.tool.google_search import GoogleSearch from app.tool.python_execute import PythonExecute - +from app.tool.baidu_search import BaiduSearch class Manus(ToolCallAgent): """ @@ -29,6 +29,6 @@ class Manus(ToolCallAgent): # Add general-purpose tools to the tool collection available_tools: ToolCollection = Field( default_factory=lambda: ToolCollection( - PythonExecute(), GoogleSearch(), BrowserUseTool(), FileSaver(), Terminate() + PythonExecute(), GoogleSearch(), BaiduSearch(), BrowserUseTool(), FileSaver(), Terminate() ) ) diff --git a/app/prompt/manus.py b/app/prompt/manus.py index 7c5b68693..0d3a872e8 100644 --- a/app/prompt/manus.py +++ b/app/prompt/manus.py @@ -8,7 +8,9 @@ BrowserUseTool: Open, browse, and use web browsers.If you open a local HTML file, you must provide the absolute path to the file. -GoogleSearch: Perform web information retrieval +GoogleSearch: Perform web information retrieval using Google search engine. + +BaiduSearch: Perform web information retrieval using Baidu search engine, especially effective for Chinese content. Based on user needs, proactively select the most appropriate tool or combination of tools. For complex tasks, you can break down the problem and use different tools step by step to solve it. After using each tool, clearly explain the execution results and suggest the next steps. """ diff --git a/app/tool/baidu_search.py b/app/tool/baidu_search.py index 5b4113d0a..1408b6af9 100644 --- a/app/tool/baidu_search.py +++ b/app/tool/baidu_search.py @@ -1,8 +1,7 @@ import asyncio from typing import List from baidusearch.baidusearch import search -# from app.tool.base import BaseTool -from base import BaseTool +from app.tool.base import BaseTool class BaiduSearch(BaseTool): name: str = "baidu_search" @@ -37,7 +36,6 @@ async def execute(self, query: str, num_results: int = 10) -> List[str]: 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( @@ -48,10 +46,4 @@ async def execute(self, query: str, num_results: int = 10) -> List[str]: 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) - + return links \ No newline at end of file