odrec commited on
Commit
7491053
·
verified ·
1 Parent(s): 809ef31

Add visit website tool

Browse files
Files changed (1) hide show
  1. app.py +28 -1
app.py CHANGED
@@ -1,6 +1,9 @@
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
 
 
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
@@ -18,6 +21,30 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
22
  @tool
23
  def web_search(search_query:str)-> str:
@@ -65,7 +92,7 @@ with open("prompts.yaml", 'r') as stream:
65
 
66
  agent = CodeAgent(
67
  model=model,
68
- tools=[final_answer, web_search, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
69
  max_steps=6,
70
  verbosity_level=1,
71
  grammar=None,
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
+ from requests.exceptions import RequestException
5
+ import re
6
+ from markdownify import markdownify
7
  import pytz
8
  import yaml
9
  from tools.final_answer import FinalAnswerTool
 
21
  """
22
  return "What magic will you build ?"
23
 
24
+ @tool
25
+ def visit_website(url:str)-> str:
26
+ """A tool that visits a website, gathers its content and returns the markdown
27
+ Args:
28
+ url: url address of the website to visit
29
+ """
30
+ try:
31
+ # Send a GET request to the URL
32
+ response = requests.get(url)
33
+ response.raise_for_status() # Raise an exception for bad status codes
34
+
35
+ # Convert the HTML content to Markdown
36
+ markdown_content = markdownify(response.text).strip()
37
+
38
+ # Remove multiple line breaks
39
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
40
+
41
+ return markdown_content
42
+
43
+ except RequestException as e:
44
+ return f"Error fetching the webpage: {str(e)}"
45
+ except Exception as e:
46
+ return f"An unexpected error occurred: {str(e)}"
47
+
48
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
49
  @tool
50
  def web_search(search_query:str)-> str:
 
92
 
93
  agent = CodeAgent(
94
  model=model,
95
+ tools=[final_answer, web_search, get_current_time_in_timezone, visit_website], ## add your tools here (don't remove final answer)
96
  max_steps=6,
97
  verbosity_level=1,
98
  grammar=None,