Abraham E. Tavarez commited on
Commit
d4804be
·
1 Parent(s): eb8b538

visit website tool

Browse files
Files changed (5) hide show
  1. agent.py +3 -1
  2. app.py +2 -2
  3. requirements.txt +2 -1
  4. tools.py +0 -12
  5. tools/visit_website.py +45 -0
agent.py CHANGED
@@ -3,6 +3,7 @@ from smolagents import HfApiModel, CodeAgent, ToolCallingAgent, DuckDuckGoSearch
3
  from huggingface_hub import login
4
  from dotenv import load_dotenv
5
  from sample_questions import QUESTIONS
 
6
 
7
  load_dotenv()
8
  login(os.environ["HF_API_KEY"])
@@ -10,6 +11,7 @@ login(os.environ["HF_API_KEY"])
10
  # Tools
11
  tools = [
12
  DuckDuckGoSearchTool(),
 
13
  ]
14
 
15
  question = QUESTIONS[0];
@@ -34,7 +36,7 @@ codeAgent = CodeAgent(
34
 
35
  codeAgent.logger.console.width=66
36
 
37
- res = codeAgent.run("whats the current weather in Orlando?")
38
  print(res)
39
 
40
 
 
3
  from huggingface_hub import login
4
  from dotenv import load_dotenv
5
  from sample_questions import QUESTIONS
6
+ from tools.visit_website import VisitWebpageTool
7
 
8
  load_dotenv()
9
  login(os.environ["HF_API_KEY"])
 
11
  # Tools
12
  tools = [
13
  DuckDuckGoSearchTool(),
14
+ VisitWebpageTool()
15
  ]
16
 
17
  question = QUESTIONS[0];
 
36
 
37
  codeAgent.logger.console.width=66
38
 
39
+ res = codeAgent.run(question)
40
  print(res)
41
 
42
 
app.py CHANGED
@@ -15,8 +15,8 @@ class BasicAgent:
15
  def __init__(self):
16
  print("BasicAgent initialized.")
17
  def __call__(self, question: str) -> str:
18
- # return codeAgent.run(question)
19
- return toolCallingAgent.run(question)
20
  # print(f"Agent received question (first 50 chars): {question[:50]}...")
21
  # fixed_answer = "This is a default answer."
22
  # print(f"Agent returning fixed answer: {fixed_answer}")
 
15
  def __init__(self):
16
  print("BasicAgent initialized.")
17
  def __call__(self, question: str) -> str:
18
+ return codeAgent.run(question)
19
+ # return toolCallingAgent.run(question)
20
  # print(f"Agent received question (first 50 chars): {question[:50]}...")
21
  # fixed_answer = "This is a default answer."
22
  # print(f"Agent returning fixed answer: {fixed_answer}")
requirements.txt CHANGED
@@ -2,4 +2,5 @@ gradio
2
  requests
3
  smolagents
4
  dotenv
5
- duckduckgo-search
 
 
2
  requests
3
  smolagents
4
  dotenv
5
+ duckduckgo-search
6
+ markdownify
tools.py DELETED
@@ -1,12 +0,0 @@
1
- from smolagents import tool
2
-
3
- @tool
4
- def get_weather_data(city: str) -> str:
5
- """
6
- Gets weather data by the city name.
7
- Args:
8
- city: city name
9
- """
10
- pass
11
-
12
- print(get_weather_data.description)
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/visit_website.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