-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: web scraping in tools + fix metrics data use in tools (#393)
* all changes * lint * code review changes * error msg * update tool descriptions * fix
- Loading branch information
1 parent
ffc711d
commit e0cf5ff
Showing
12 changed files
with
158 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Any, Dict, List | ||
|
||
from bs4 import BeautifulSoup | ||
from requests import get | ||
|
||
from backend.tools.base import BaseTool | ||
|
||
|
||
class WebScrapeTool(BaseTool): | ||
NAME = "web_scrape" | ||
|
||
@classmethod | ||
def is_available(cls) -> bool: | ||
return True | ||
|
||
async def call(self, parameters: dict, **kwargs: Any) -> List[Dict[str, Any]]: | ||
url = parameters.get("url") | ||
|
||
response = get(url) | ||
if not response.ok: | ||
error_message = f"HTTP {response.status_code} {response.reason}" | ||
return [ | ||
( | ||
{ | ||
"text": f"Cannot open and scrape URL {url}, Error: {error_message}", | ||
"url": url, | ||
} | ||
) | ||
] | ||
|
||
soup = BeautifulSoup(response.text, "html.parser") | ||
text = soup.get_text(separator="\n") | ||
|
||
return [ | ||
( | ||
{ | ||
"text": text, | ||
"url": url, | ||
} | ||
) | ||
] |