calcworks commited on
Commit
0786c58
·
verified ·
1 Parent(s): 9a8c112

Update app.py

Browse files

Added custom tools for my demo agent exercise.

Files changed (1) hide show
  1. app.py +50 -6
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
 
3
  import requests
4
  import pytz
5
  import yaml
@@ -18,6 +19,49 @@ def my_cutom_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.
@@ -35,11 +79,11 @@ def get_current_time_in_timezone(timezone: str) -> str:
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
 
@@ -51,7 +95,7 @@ with open("prompts.yaml", 'r') as 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,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
+ import httpx
4
  import requests
5
  import pytz
6
  import yaml
 
19
  """
20
  return "What magic will you build ?"
21
 
22
+ ## -----
23
+ ## my custom tools
24
+ ## -----
25
+ @tool
26
+ def get_top_hackernews_stories(num_stories: int = 10) -> str:
27
+ """A tool that fetches a specified number of top news stories from Hacker News.
28
+ Args:
29
+ num_stories: int representing the number of stories to return.
30
+ """
31
+ response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
32
+ story_ids = response.json()
33
+ stories = []
34
+ for story_id in story_ids[:num_stories]:
35
+ story_response = httpx.get(f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json")
36
+ story = story_response.json()
37
+ story["username"] = story["by"]
38
+ stories.append(story)
39
+ return json.dumps(stories)
40
+
41
+ @tool
42
+ def get_user_details(username: str) -> str:
43
+ """A tool that fetches the details of a Hacker News user using their username.
44
+ Args:
45
+ username: A string representing the username of the user to get details for.
46
+ """
47
+
48
+ try:
49
+ logger.info(f"Getting details for user: {username}")
50
+ user = httpx.get(f"https://hacker-news.firebaseio.com/v0/user/{username}.json").json()
51
+ user_details = {
52
+ "id": user.get("user_id"),
53
+ "karma": user.get("karma"),
54
+ "about": user.get("about"),
55
+ "total_items_submitted": len(user.get("submitted", [])),
56
+ }
57
+ return json.dumps(user_details)
58
+ except Exception as e:
59
+ logger.exception(e)
60
+ return f"Error getting user details: {e}"
61
+ ## -----
62
+ ## end my custom tools
63
+ ## -----
64
+
65
  @tool
66
  def get_current_time_in_timezone(timezone: str) -> str:
67
  """A tool that fetches the current local time in a specified timezone.
 
79
 
80
 
81
  final_answer = FinalAnswerTool()
82
+ model = HfApiModel(
83
+ max_tokens=2096,
84
+ temperature=0.5,
85
+ model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
86
+ custom_role_conversions=None,
87
  )
88
 
89
 
 
95
 
96
  agent = CodeAgent(
97
  model=model,
98
+ tools=[get_top_hackernews_stories, get_user_details, final_answer], ## add your tools here (don't remove final answer)
99
  max_steps=6,
100
  verbosity_level=1,
101
  grammar=None,