Mauricio Mercado commited on
Commit
fe15a85
·
unverified ·
1 Parent(s): 9a8c112

Finished first agent

Browse files
Files changed (4) hide show
  1. .gitignore +5 -0
  2. agent.json +1 -1
  3. app.py +53 -18
  4. tools/visit_webpage.py +1 -1
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ .venv
2
+ .smolagents
3
+ .smolagents-cache
4
+ __pycache__
5
+ .gradio
agent.json CHANGED
@@ -50,4 +50,4 @@
50
  "collections",
51
  "re"
52
  ]
53
- }
 
50
  "collections",
51
  "re"
52
  ]
53
+ }
app.py CHANGED
@@ -1,22 +1,12 @@
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
7
-
8
  from Gradio_UI import GradioUI
9
-
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
- @tool
12
- def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
- Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
- """
19
- return "What magic will you build ?"
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -33,25 +23,70 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
 
 
 
38
  model = HfApiModel(
39
  max_tokens=2096,
40
  temperature=0.5,
41
- model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
42
  custom_role_conversions=None,
43
  )
44
 
45
-
46
  # Import tool from Hub
47
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
48
 
49
  with open("prompts.yaml", 'r') as stream:
50
  prompt_templates = yaml.safe_load(stream)
51
-
52
  agent = CodeAgent(
53
  model=model,
54
- tools=[final_answer], ## add your tools here (don't remove final answer)
55
  max_steps=6,
56
  verbosity_level=1,
57
  grammar=None,
@@ -62,4 +97,4 @@ agent = CodeAgent(
62
  )
63
 
64
 
65
- GradioUI(agent).launch()
 
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
7
+ from tools.visit_webpage import VisitWebpageTool
8
  from Gradio_UI import GradioUI
9
+ import re
 
 
 
 
 
 
 
 
 
 
10
 
11
  @tool
12
  def get_current_time_in_timezone(timezone: str) -> str:
 
23
  except Exception as e:
24
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
25
 
26
+ @tool
27
+ def web_research(query: str) -> str:
28
+ """A tool that searches the web for news of any kind depending on the query, visits the top 5 results, and provides the content found for each link.
29
+ Args:
30
+ query: The search query to research
31
+ """
32
+ try:
33
+ # Initialize tools
34
+ search_tool = DuckDuckGoSearchTool(max_results=5)
35
+ visit_tool = VisitWebpageTool()
36
+
37
+ # Perform search
38
+ search_results = search_tool.forward(query)
39
+
40
+ # Extract URLs using regex
41
+ urls = re.findall(r'\[(.*?)\]\((.*?)\)', search_results)
42
+
43
+ # Visit each webpage and collect content
44
+ all_content = []
45
+ for _, url in urls[:5]: # Take first 5 URLs
46
+ content = visit_tool.forward(url)
47
+ all_content.append(content)
48
+
49
+ # Combine content
50
+ combined_content = "\n\n".join(all_content)
51
+
52
+ # Create summarization prompt
53
+ summarization_prompt = f"""Please provide a concise summary of the following content related to '{query}'.
54
+ Focus on the main points and key insights:\n\n{combined_content}"""
55
+
56
+ # Get summary using the model
57
+ summary_messages = [{"role": "user", "content": summarization_prompt}]
58
+ summary = model(summary_messages, max_tokens=5000)
59
+
60
+ # Format output
61
+ final_output = f"""## Content of Research on: {query}
62
+
63
+ {summary}
64
+ """
65
+ return final_output
66
+
67
+ except Exception as e:
68
+ return f"An error occurred: {str(e)}"
69
 
70
  final_answer = FinalAnswerTool()
71
+ # web_search = DuckDuckGoSearchTool()
72
+ visit_webpage = VisitWebpageTool()
73
+
74
  model = HfApiModel(
75
  max_tokens=2096,
76
  temperature=0.5,
77
+ model_id='https://jc26mwg228mkj8dw.us-east-1.aws.endpoints.huggingface.cloud/',# it is possible that this model may be overloaded
78
  custom_role_conversions=None,
79
  )
80
 
 
81
  # Import tool from Hub
82
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
83
 
84
  with open("prompts.yaml", 'r') as stream:
85
  prompt_templates = yaml.safe_load(stream)
86
+
87
  agent = CodeAgent(
88
  model=model,
89
+ tools=[final_answer, web_research], ## add your tools here (don't remove final answer)
90
  max_steps=6,
91
  verbosity_level=1,
92
  grammar=None,
 
97
  )
98
 
99
 
100
+ GradioUI(agent).launch()
tools/visit_webpage.py CHANGED
@@ -2,7 +2,7 @@ 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"
 
2
  from smolagents.tools import Tool
3
  import requests
4
  import markdownify
5
+ import re
6
 
7
  class VisitWebpageTool(Tool):
8
  name = "visit_webpage"