Spaces:
Sleeping
Sleeping
Create web_search_tool.py
Browse files- tools/web_search_tool.py +18 -0
tools/web_search_tool.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from crewai.tools import BaseTool
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class WebSearchTool(BaseTool):
|
| 7 |
+
name: str = "Web Search Tool"
|
| 8 |
+
description: str = "Fetches web content for debugging help"
|
| 9 |
+
|
| 10 |
+
def _run(self, query: str):
|
| 11 |
+
url = f"https://duckduckgo.com/html/?q={query}"
|
| 12 |
+
|
| 13 |
+
response = requests.get(url)
|
| 14 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 15 |
+
|
| 16 |
+
results = soup.find_all("a", class_="result__a", limit=5)
|
| 17 |
+
|
| 18 |
+
return "\n".join([result.get_text() for result in results])
|