Update tool.py
Browse files
tool.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
-
from typing import Optional
|
| 4 |
-
import urllib.parse
|
| 5 |
|
| 6 |
class Tool:
|
| 7 |
"""Base class for tools."""
|
|
@@ -17,13 +15,11 @@ class Tool:
|
|
| 17 |
class SearchInformationTool(Tool):
|
| 18 |
name = "web_search"
|
| 19 |
description = "Perform a web search query and return the search results."
|
| 20 |
-
inputs = {
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"nullable": True,
|
| 26 |
-
},
|
| 27 |
}
|
| 28 |
output_type = "string"
|
| 29 |
|
|
@@ -31,7 +27,7 @@ class SearchInformationTool(Tool):
|
|
| 31 |
super().__init__()
|
| 32 |
self.browser = browser
|
| 33 |
|
| 34 |
-
def forward(self, query: str, filter_year:
|
| 35 |
return self.browser.search_web(query, filter_year, filter_year)
|
| 36 |
|
| 37 |
class VisitTool(Tool):
|
|
@@ -54,8 +50,7 @@ class Browser:
|
|
| 54 |
self.current_page = None
|
| 55 |
|
| 56 |
def search_web(self, query, start_year, end_year):
|
| 57 |
-
|
| 58 |
-
url = f"https://www.google.com/search?q={encoded_query}"
|
| 59 |
headers = {
|
| 60 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
|
| 61 |
}
|
|
@@ -80,8 +75,8 @@ class Browser:
|
|
| 80 |
response = requests.get(url)
|
| 81 |
response.raise_for_status()
|
| 82 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 83 |
-
for
|
| 84 |
-
|
| 85 |
text = soup.get_text(separator='\n', strip=True)
|
| 86 |
self.current_page = {
|
| 87 |
"url": url,
|
|
@@ -99,4 +94,4 @@ class Browser:
|
|
| 99 |
def _state(self):
|
| 100 |
if self.current_page:
|
| 101 |
return self.current_page["header"], self.current_page["content"]
|
| 102 |
-
|
|
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class Tool:
|
| 5 |
"""Base class for tools."""
|
|
|
|
| 15 |
class SearchInformationTool(Tool):
|
| 16 |
name = "web_search"
|
| 17 |
description = "Perform a web search query and return the search results."
|
| 18 |
+
inputs = {"query": {"type": "string", "description": "The web search query to perform."}}
|
| 19 |
+
inputs["filter_year"] = {
|
| 20 |
+
"type": "string",
|
| 21 |
+
"description": "[Optional parameter]: filter the search results to only include pages from a specific year.",
|
| 22 |
+
"nullable": True,
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
output_type = "string"
|
| 25 |
|
|
|
|
| 27 |
super().__init__()
|
| 28 |
self.browser = browser
|
| 29 |
|
| 30 |
+
def forward(self, query: str, filter_year: int | None = None) -> str:
|
| 31 |
return self.browser.search_web(query, filter_year, filter_year)
|
| 32 |
|
| 33 |
class VisitTool(Tool):
|
|
|
|
| 50 |
self.current_page = None
|
| 51 |
|
| 52 |
def search_web(self, query, start_year, end_year):
|
| 53 |
+
url = f"https://www.google.com/search?q={query}"
|
|
|
|
| 54 |
headers = {
|
| 55 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
|
| 56 |
}
|
|
|
|
| 75 |
response = requests.get(url)
|
| 76 |
response.raise_for_status()
|
| 77 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 78 |
+
for script in soup(["script", "style"]):
|
| 79 |
+
script.decompose()
|
| 80 |
text = soup.get_text(separator='\n', strip=True)
|
| 81 |
self.current_page = {
|
| 82 |
"url": url,
|
|
|
|
| 94 |
def _state(self):
|
| 95 |
if self.current_page:
|
| 96 |
return self.current_page["header"], self.current_page["content"]
|
| 97 |
+
|