Upload 3 files
Browse files- tools/final_answer.py +14 -0
- tools/visit_webpage.py +45 -0
- tools/web_search.py +27 -0
tools/final_answer.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
|
4 |
+
class FinalAnswerTool(Tool):
|
5 |
+
name = "final_answer"
|
6 |
+
description = "Provides a final answer to the given problem."
|
7 |
+
inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
|
8 |
+
output_type = "any"
|
9 |
+
|
10 |
+
def forward(self, answer: Any) -> Any:
|
11 |
+
return answer
|
12 |
+
|
13 |
+
def __init__(self, *args, **kwargs):
|
14 |
+
self.is_initialized = False
|
tools/visit_webpage.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
import requests
|
4 |
+
import markdownify
|
5 |
+
import smolagents
|
6 |
+
|
7 |
+
class VisitWebpageTool(Tool):
|
8 |
+
name = "visit_webpage"
|
9 |
+
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
10 |
+
inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}
|
11 |
+
output_type = "string"
|
12 |
+
|
13 |
+
def forward(self, url: str) -> str:
|
14 |
+
try:
|
15 |
+
import requests
|
16 |
+
from markdownify import markdownify
|
17 |
+
from requests.exceptions import RequestException
|
18 |
+
|
19 |
+
from smolagents.utils import truncate_content
|
20 |
+
except ImportError as e:
|
21 |
+
raise ImportError(
|
22 |
+
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
|
23 |
+
) from e
|
24 |
+
try:
|
25 |
+
# Send a GET request to the URL with a 20-second timeout
|
26 |
+
response = requests.get(url, timeout=20)
|
27 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
28 |
+
|
29 |
+
# Convert the HTML content to Markdown
|
30 |
+
markdown_content = markdownify(response.text).strip()
|
31 |
+
|
32 |
+
# Remove multiple line breaks
|
33 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
34 |
+
|
35 |
+
return truncate_content(markdown_content, 10000)
|
36 |
+
|
37 |
+
except requests.exceptions.Timeout:
|
38 |
+
return "The request timed out. Please try again later or check the URL."
|
39 |
+
except RequestException as e:
|
40 |
+
return f"Error fetching the webpage: {str(e)}"
|
41 |
+
except Exception as e:
|
42 |
+
return f"An unexpected error occurred: {str(e)}"
|
43 |
+
|
44 |
+
def __init__(self, *args, **kwargs):
|
45 |
+
self.is_initialized = False
|
tools/web_search.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
import duckduckgo_search
|
4 |
+
|
5 |
+
class DuckDuckGoSearchTool(Tool):
|
6 |
+
name = "web_search"
|
7 |
+
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
8 |
+
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
9 |
+
output_type = "string"
|
10 |
+
|
11 |
+
def __init__(self, max_results=10, **kwargs):
|
12 |
+
super().__init__()
|
13 |
+
self.max_results = max_results
|
14 |
+
try:
|
15 |
+
from duckduckgo_search import DDGS
|
16 |
+
except ImportError as e:
|
17 |
+
raise ImportError(
|
18 |
+
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
19 |
+
) from e
|
20 |
+
self.ddgs = DDGS(**kwargs)
|
21 |
+
|
22 |
+
def forward(self, query: str) -> str:
|
23 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
24 |
+
if len(results) == 0:
|
25 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
26 |
+
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
27 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|