Skip to content

Commit

Permalink
fixes bing_search
Browse files Browse the repository at this point in the history
  • Loading branch information
via committed Mar 10, 2025
1 parent 637d647 commit 3fbed0f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/agent/manus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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.bing_search import BingSearch


class Manus(ToolCallAgent):
Expand All @@ -29,6 +30,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(), BrowserUseTool(), FileSaver(), Terminate(), BingSearch()
)
)
30 changes: 17 additions & 13 deletions app/tool/bing_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
from typing import List
import requests
from app.logger import logger
from bs4 import BeautifulSoup
from app.tool.base import BaseTool

Expand Down Expand Up @@ -86,8 +87,21 @@ def _search_sync(self, query: str, num_results: int = 10) -> List[str]:

def _parse_html(self, url: str, rank_start: int = 0, first: int = 1) -> tuple:
"""
Parse Bing search result HTML synchronously.
Returns a tuple of (list of results, next_url).
Parse Bing search result HTML synchronously to extract search results and the next page URL.
Args:
url (str): The URL of the Bing search results page to parse.
rank_start (int, optional): The starting rank for numbering the search results. Defaults to 0.
first (int, optional): Unused parameter (possibly legacy). Defaults to 1.
Returns:
tuple: A tuple containing:
- list: A list of dictionaries with keys 'title', 'abstract', 'url', and 'rank' for each result.
- str or None: The URL of the next results page, or None if there is no next page.
Example:
This function is called by `execute` in the following way:
```python
results, next_url = self._parse_html(url, rank_start=0)
```
"""
try:
res = self.session.get(url=url)
Expand Down Expand Up @@ -128,7 +142,7 @@ def _parse_html(self, url: str, rank_start: int = 0, first: int = 1) -> tuple:
next_url = BING_HOST_URL + next_btn["href"]
return list_data, next_url
except Exception as e:
print(f"Error parsing HTML: {e}")
logger.warning(f"Error parsing HTML: {e}")
return [], None

async def execute(self, query: str, num_results: int = 10) -> List[str]:
Expand All @@ -147,13 +161,3 @@ async def execute(self, query: str, num_results: int = 10) -> List[str]:
None, lambda: self._search_sync(query, num_results=num_results)
)
return links


# if __name__ == "__main__":
# async def test():
# tool = BingSearch()
# results = await tool.execute(query="python", num_results=3)
# for i, url in enumerate(results, 1):
# print(f"{i}. {url}")
#
# asyncio.run(test())

0 comments on commit 3fbed0f

Please sign in to comment.