opt: abstract web search interface, code cleanup
This commit is contained in:
parent
f9ce06adb8
commit
b7774b18ef
@ -7,8 +7,7 @@ from app.prompt.manus import NEXT_STEP_PROMPT, SYSTEM_PROMPT
|
|||||||
from app.tool import Terminate, ToolCollection
|
from app.tool import Terminate, ToolCollection
|
||||||
from app.tool.browser_use_tool import BrowserUseTool
|
from app.tool.browser_use_tool import BrowserUseTool
|
||||||
from app.tool.file_saver import FileSaver
|
from app.tool.file_saver import FileSaver
|
||||||
from app.tool.google_search import GoogleSearch
|
from app.tool.web_search import WebSearch
|
||||||
from app.tool.baidu_search import BaiduSearch
|
|
||||||
from app.tool.python_execute import PythonExecute
|
from app.tool.python_execute import PythonExecute
|
||||||
from app.config import config
|
from app.config import config
|
||||||
|
|
||||||
@ -36,22 +35,10 @@ class Manus(ToolCallAgent):
|
|||||||
# Add general-purpose tools to the tool collection
|
# Add general-purpose tools to the tool collection
|
||||||
available_tools: ToolCollection = Field(
|
available_tools: ToolCollection = Field(
|
||||||
default_factory=lambda: ToolCollection(
|
default_factory=lambda: ToolCollection(
|
||||||
PythonExecute(), Manus.get_search_tool(), BrowserUseTool(), FileSaver(), Terminate()
|
PythonExecute(), WebSearch(), BrowserUseTool(), FileSaver(), Terminate()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_search_tool():
|
|
||||||
"""Determines the search tool to use based on the configuration."""
|
|
||||||
if config.search_config is None:
|
|
||||||
return GoogleSearch()
|
|
||||||
else:
|
|
||||||
# Check search engine
|
|
||||||
engine = config.search_config.engine.lower()
|
|
||||||
if engine == "baidu":
|
|
||||||
return BaiduSearch()
|
|
||||||
return GoogleSearch()
|
|
||||||
|
|
||||||
async def _handle_special_tool(self, name: str, result: Any, **kwargs):
|
async def _handle_special_tool(self, name: str, result: Any, **kwargs):
|
||||||
await self.available_tools.get_tool(BrowserUseTool().name).cleanup()
|
await self.available_tools.get_tool(BrowserUseTool().name).cleanup()
|
||||||
await super()._handle_special_tool(name, result, **kwargs)
|
await super()._handle_special_tool(name, result, **kwargs)
|
||||||
|
@ -158,7 +158,6 @@ class Config:
|
|||||||
search_settings = None
|
search_settings = None
|
||||||
if search_config:
|
if search_config:
|
||||||
search_settings = SearchSettings(**search_config)
|
search_settings = SearchSettings(**search_config)
|
||||||
print("search setting", search_settings)
|
|
||||||
|
|
||||||
config_dict = {
|
config_dict = {
|
||||||
"llm": {
|
"llm": {
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
from googlesearch import search
|
|
||||||
|
|
||||||
from app.tool.base import BaseTool
|
|
||||||
|
|
||||||
|
|
||||||
class GoogleSearch(BaseTool):
|
|
||||||
name: str = "google_search"
|
|
||||||
description: str = """Perform a Google 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 Google.",
|
|
||||||
},
|
|
||||||
"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 Google search and return a list of URLs.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
query (str): The search query to submit to Google.
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
# Run the search in a thread pool to prevent blocking
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
links = await loop.run_in_executor(
|
|
||||||
None, lambda: list(search(query, num_results=num_results))
|
|
||||||
)
|
|
||||||
|
|
||||||
return links
|
|
@ -1,14 +1,16 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from baidusearch.baidusearch import search
|
from googlesearch import search as google_search
|
||||||
|
from baidusearch.baidusearch import search as baidu_search
|
||||||
|
|
||||||
from app.tool.base import BaseTool
|
from app.tool.base import BaseTool
|
||||||
|
from app.config import config
|
||||||
|
|
||||||
|
|
||||||
class BaiduSearch(BaseTool):
|
class WebSearch(BaseTool):
|
||||||
name: str = "baidu_search"
|
name: str = "web_search"
|
||||||
description: str = """Perform a Baidu search and return a list of relevant links.
|
description: str = """Perform a web 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.
|
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.
|
The tool returns a list of URLs that match the search query.
|
||||||
"""
|
"""
|
||||||
@ -17,7 +19,7 @@ The tool returns a list of URLs that match the search query.
|
|||||||
"properties": {
|
"properties": {
|
||||||
"query": {
|
"query": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "(required) The search query to submit to Baidu.",
|
"description": "(required) The search query to submit to the search engine.",
|
||||||
},
|
},
|
||||||
"num_results": {
|
"num_results": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
@ -27,13 +29,17 @@ The tool returns a list of URLs that match the search query.
|
|||||||
},
|
},
|
||||||
"required": ["query"],
|
"required": ["query"],
|
||||||
}
|
}
|
||||||
|
_search_engine: dict = {
|
||||||
|
"google": google_search,
|
||||||
|
"baidu": baidu_search,
|
||||||
|
}
|
||||||
|
|
||||||
async def execute(self, query: str, num_results: int = 10) -> List[str]:
|
async def execute(self, query: str, num_results: int = 10) -> List[str]:
|
||||||
"""
|
"""
|
||||||
Execute a Baidu search and return a list of URLs.
|
Execute a Web search and return a list of URLs.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
query (str): The search query to submit to Baidu.
|
query (str): The search query to submit to the search engine.
|
||||||
num_results (int, optional): The number of search results to return. Default is 10.
|
num_results (int, optional): The number of search results to return. Default is 10.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -41,8 +47,17 @@ The tool returns a list of URLs that match the search query.
|
|||||||
"""
|
"""
|
||||||
# Run the search in a thread pool to prevent blocking
|
# Run the search in a thread pool to prevent blocking
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
search_engine = self.get_search_engine()
|
||||||
links = await loop.run_in_executor(
|
links = await loop.run_in_executor(
|
||||||
None, lambda: list(search(query, num_results=num_results))
|
None, lambda: list(search_engine(query, num_results=num_results))
|
||||||
)
|
)
|
||||||
|
|
||||||
return links
|
return links
|
||||||
|
|
||||||
|
def get_search_engine(self):
|
||||||
|
"""Determines the search engine to use based on the configuration."""
|
||||||
|
if config.search_config is None:
|
||||||
|
return google_search
|
||||||
|
else:
|
||||||
|
engine = config.search_config.engine.lower()
|
||||||
|
return self._search_engine.get(engine, google_search)
|
Loading…
x
Reference in New Issue
Block a user