efthygeo commited on
Commit
84435ae
·
verified ·
1 Parent(s): ae7a494

updated tool arsenal

Browse files
Files changed (1) hide show
  1. app.py +65 -2
app.py CHANGED
@@ -18,6 +18,63 @@ 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
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
@@ -55,8 +112,14 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
- max_steps=6,
 
 
 
 
 
 
60
  verbosity_level=1,
61
  grammar=None,
62
  planning_interval=None,
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ # code from: https://github.com/huggingface/smolagents/blob/main/examples/multiple_tools.py
22
+ @tool
23
+ def search_wikipedia(query: str) -> str:
24
+ """
25
+ Fetches a summary of a Wikipedia page for a given query.
26
+ Args:
27
+ query: The search term to look up on Wikipedia.
28
+ Returns:
29
+ str: A summary of the Wikipedia page if successful, or an error message if the request fails.
30
+ Raises:
31
+ requests.exceptions.RequestException: If there is an issue with the HTTP request.
32
+ """
33
+ url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
34
+
35
+ try:
36
+ response = requests.get(url)
37
+ response.raise_for_status()
38
+
39
+ data = response.json()
40
+ title = data["title"]
41
+ extract = data["extract"]
42
+
43
+ return f"Summary for {title}: {extract}"
44
+
45
+ except requests.exceptions.RequestException as e:
46
+ return f"Error fetching Wikipedia data: {str(e)}"
47
+
48
+ @tool
49
+ def get_news_headlines() -> str:
50
+ """
51
+ Fetches the top news headlines from the News API for the United States.
52
+ This function makes a GET request to the News API to retrieve the top news headlines
53
+ for the United States. It returns the titles and sources of the top 5 articles as a
54
+ formatted string. If no articles are available, it returns a message indicating that
55
+ no news is available. In case of a request error, it returns an error message.
56
+ Returns:
57
+ str: A string containing the top 5 news headlines and their sources, or an error message.
58
+ """
59
+ api_key = "995553d64cc54505859b26e81d7065a2" # Replace with your actual API key from https://newsapi.org/
60
+ url = f"https://newsapi.org/v2/top-headlines?country=us&apiKey={api_key}"
61
+
62
+ try:
63
+ response = requests.get(url)
64
+ response.raise_for_status()
65
+
66
+ data = response.json()
67
+ articles = data["articles"]
68
+
69
+ if not articles:
70
+ return "No news available at the moment."
71
+
72
+ headlines = [f"{article['title']} - {article['source']['name']}" for article in articles[:5]]
73
+ return "\n".join(headlines)
74
+
75
+ except requests.exceptions.RequestException as e:
76
+ return f"Error fetching news data: {str(e)}"
77
+
78
  @tool
79
  def get_current_time_in_timezone(timezone: str) -> str:
80
  """A tool that fetches the current local time in a specified timezone.
 
112
 
113
  agent = CodeAgent(
114
  model=model,
115
+ tools=[
116
+ final_answer,
117
+ get_current_time_in_timezone,
118
+ get_news_headlines,
119
+ image_generation_tool,
120
+ search_wikipedia,
121
+ ], ## add your tools here (don't remove final answer)
122
+ max_steps=10,
123
  verbosity_level=1,
124
  grammar=None,
125
  planning_interval=None,