Create tool.py
Browse files
tool.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
|
| 4 |
+
class Tool:
|
| 5 |
+
"""Base class for tools."""
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.name = None
|
| 8 |
+
self.description = None
|
| 9 |
+
self.inputs = {}
|
| 10 |
+
self.output_type = None
|
| 11 |
+
|
| 12 |
+
def forward(self, *args, **kwargs):
|
| 13 |
+
raise NotImplementedError("Subclasses must implement this method.")
|
| 14 |
+
|
| 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 |
+
|
| 26 |
+
def __init__(self, browser):
|
| 27 |
+
super().__init__()
|
| 28 |
+
self.browser = browser
|
| 29 |
+
|
| 30 |
+
def forward(self, query: str, filter_year: int | None = None) -> str:
|
| 31 |
+
self.browser.visit_page(f"https://www.google.com/search?q={query}", filter_year=filter_year)
|
| 32 |
+
header, content = self.browser._state()
|
| 33 |
+
return header.strip() + "\n=======================\n" + content
|
| 34 |
+
|
| 35 |
+
class VisitTool(Tool):
|
| 36 |
+
name = "visit_page"
|
| 37 |
+
description = "Visit a webpage at a given URL and return its text."
|
| 38 |
+
inputs = {"url": {"type": "string", "description": "The relative or absolute URL of the webpage to visit."}}
|
| 39 |
+
output_type = "string"
|
| 40 |
+
|
| 41 |
+
def __init__(self, browser=None):
|
| 42 |
+
super().__init__()
|
| 43 |
+
self.browser = browser
|
| 44 |
+
|
| 45 |
+
def forward(self, url: str) -> str:
|
| 46 |
+
self.browser.visit_page(url)
|
| 47 |
+
header, content = self.browser._state()
|
| 48 |
+
return header.strip() + "\n=======================\n" + content
|
| 49 |
+
|
| 50 |
+
class Browser:
|
| 51 |
+
def __init__(self):
|
| 52 |
+
self.current_page = None
|
| 53 |
+
|
| 54 |
+
def visit_page(self, url: str, filter_year: int = None):
|
| 55 |
+
try:
|
| 56 |
+
response = requests.get(url)
|
| 57 |
+
response.raise_for_status()
|
| 58 |
+
|
| 59 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 60 |
+
|
| 61 |
+
for script in soup(["script", "style"]):
|
| 62 |
+
script.decompose()
|
| 63 |
+
|
| 64 |
+
text = soup.get_text(separator='\n', strip=True)
|
| 65 |
+
|
| 66 |
+
self.current_page = {
|
| 67 |
+
"url": url,
|
| 68 |
+
"filter_year": filter_year,
|
| 69 |
+
"header": f"Header for {url}",
|
| 70 |
+
"content": text
|
| 71 |
+
}
|
| 72 |
+
except requests.RequestException as e:
|
| 73 |
+
print(f"An error occurred: {e}")
|
| 74 |
+
self.current_page = {
|
| 75 |
+
"url": url,
|
| 76 |
+
"filter_year": filter_year,
|
| 77 |
+
"header": "Error",
|
| 78 |
+
"content": f"Failed to retrieve the page: {e}"
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
def _state(self):
|
| 82 |
+
if self.current_page:
|
| 83 |
+
return self.current_page["header"], self.current_page["content"]
|
| 84 |
+
return "", ""
|
| 85 |
+
|
| 86 |
+
# Example usage:
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
browser = Browser()
|
| 89 |
+
search_tool = SearchInformationTool(browser)
|
| 90 |
+
visit_tool = VisitTool(browser)
|
| 91 |
+
|
| 92 |
+
search_result = search_tool.forward("example query", 2023)
|
| 93 |
+
print(search_result)
|
| 94 |
+
|
| 95 |
+
visit_result = visit_tool.forward("https://example.com")
|
| 96 |
+
print(visit_result)
|