Spaces:
Sleeping
Sleeping
Update tools/web_search_tool.py
Browse files- tools/web_search_tool.py +18 -6
tools/web_search_tool.py
CHANGED
|
@@ -1,18 +1,30 @@
|
|
| 1 |
-
from
|
|
|
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
|
| 5 |
|
| 6 |
class WebSearchTool(BaseTool):
|
| 7 |
-
name
|
| 8 |
-
description
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
return "\n".join(
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.tools import BaseTool
|
| 2 |
+
|
| 3 |
import requests
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
|
| 6 |
|
| 7 |
class WebSearchTool(BaseTool):
|
| 8 |
+
name = "Web Search Tool"
|
| 9 |
+
description = "Searches web content"
|
| 10 |
|
| 11 |
def _run(self, query: str):
|
| 12 |
+
|
| 13 |
url = f"https://duckduckgo.com/html/?q={query}"
|
| 14 |
|
| 15 |
response = requests.get(url)
|
|
|
|
| 16 |
|
| 17 |
+
soup = BeautifulSoup(
|
| 18 |
+
response.text,
|
| 19 |
+
"html.parser"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
results = soup.find_all(
|
| 23 |
+
"a",
|
| 24 |
+
class_="result__a",
|
| 25 |
+
limit=5
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
return "\n".join(
|
| 29 |
+
[result.get_text() for result in results]
|
| 30 |
+
)
|