DTStudios commited on
Commit
4e051ec
·
verified ·
1 Parent(s): 59cca98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -25
app.py CHANGED
@@ -9,29 +9,43 @@ from tools.final_answer import FinalAnswerTool
9
  from smolagents import tools
10
 
11
  @tool
12
- def calculator(a: int, b: int, operation: str = "multiply") -> float:
13
- """
14
- Perform basic arithmetic operations on two numbers.
 
 
15
 
16
- Args:
17
- a (int): First number
18
- b (int): Second number
19
- operation (str): Operation to perform ("add", "subtract", "multiply", "divide")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- Returns:
22
- float: Result of the operation
23
- """
24
- if operation == "add":
25
- return a + b
26
- elif operation == "subtract":
27
- return a - b
28
- elif operation == "multiply":
29
- return a * b
30
- elif operation == "divide":
31
- return a / b if b != 0 else float("inf")
32
- else:
33
- raise ValueError(f"Unsupported operation: {operation}")
34
-
35
 
36
  @tool
37
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -74,9 +88,9 @@ from tools.final_answer import FinalAnswerTool
74
  from smolagents import load_tool
75
 
76
  tool_registry = {
77
- "calculator": {
78
- "tool": calculator,
79
- "schema": calculator.tool_schema(),
80
  "enabled": True
81
  },
82
  "get_current_time_in_timezone": {
@@ -97,7 +111,7 @@ agent_tools = [entry["tool"] for entry in tool_registry.values() if entry["enabl
97
  ]
98
  agent = CodeAgent(
99
  model=model,
100
- tools=[final_answer, calculator, get_current_time_in_timezone],
101
  max_steps=6,
102
  verbosity_level=1,
103
  grammar=None,
 
9
  from smolagents import tools
10
 
11
  @tool
12
+ class VisitWebpageTool(Tool):
13
+ name = "visit_webpage"
14
+ description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
15
+ inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}
16
+ output_type = "string"
17
 
18
+ def forward(self, url: str) -> str:
19
+ try:
20
+ import requests
21
+ from markdownify import markdownify
22
+ from requests.exceptions import RequestException
23
+
24
+ from smolagents.utils import truncate_content
25
+ except ImportError as e:
26
+ raise ImportError(
27
+ "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
28
+ ) from e
29
+ try:
30
+ # Send a GET request to the URL with a 20-second timeout
31
+ response = requests.get(url, timeout=20)
32
+ response.raise_for_status() # Raise an exception for bad status codes
33
+
34
+ # Convert the HTML content to Markdown
35
+ markdown_content = markdownify(response.text).strip()
36
+
37
+ # Remove multiple line breaks
38
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
39
+
40
+ return truncate_content(markdown_content, 10000)
41
+
42
+ except requests.exceptions.Timeout:
43
+ return "The request timed out. Please try again later or check the URL."
44
+ except RequestException as e:
45
+ return f"Error fetching the webpage: {str(e)}"
46
+ except Exception as e:
47
+ return f"An unexpected error occurred: {str(e)}"
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  @tool
51
  def get_current_time_in_timezone(timezone: str) -> str:
 
88
  from smolagents import load_tool
89
 
90
  tool_registry = {
91
+ "VisitWebPageTool": {
92
+ "tool": VisitWebpageTool,
93
+ "schema": VisitWebpageTool(),
94
  "enabled": True
95
  },
96
  "get_current_time_in_timezone": {
 
111
  ]
112
  agent = CodeAgent(
113
  model=model,
114
+ tools=[final_answer, VisitWebpageTool, get_current_time_in_timezone],
115
  max_steps=6,
116
  verbosity_level=1,
117
  grammar=None,