eeenn commited on
Commit
37947df
·
verified ·
1 Parent(s): aec8eb8

Upload 6 files

Browse files
Files changed (4) hide show
  1. app.py +16 -36
  2. tools/party_menu.py +1 -8
  3. tools/party_song.py +11 -14
  4. tools/party_time.py +19 -14
app.py CHANGED
@@ -1,36 +1,16 @@
1
- from smolagents import CodeAgent, InferenceClientModel, FinalAnswerTool
2
- from tools.party_song import suggest_party_music
3
- from tools.party_menu import suggest_menu
4
- from tools.party_time import estimate_preparation_time
5
- from smolagents import FinalAnswerTool
6
-
7
- #引入 yaml 库,用来加载系统提示(prompts.yaml)文件。
8
- import yaml
9
-
10
- # 定义终极回答工具 FinalAnswerTool , smolagents 自带的工具,
11
- # 作用是告诉代理 “这是最后一步,把结果返回给用户”。
12
- # 没有这个工具,Agent 可能会一直循环思考,不知道什么时候结束。
13
- final_answer = FinalAnswerTool()
14
-
15
- #初始化模型
16
- model = InferenceClientModel(
17
- model_id="Qwen/Qwen2.5-Coder-7B-Instruct",
18
- max_tokens=1024,
19
- temperature=0.5,
20
- custom_role_conversions=None,
21
- )
22
-
23
- #加载系统提示
24
- with open("prompts.yaml", "r") as f:
25
- prompt_templates = yaml.safe_load(f)
26
-
27
- #构建代理
28
- agent = CodeAgent(
29
- model=model,
30
- tools=[final_answer, suggest_party_music, suggest_menu, estimate_preparation_time],
31
- prompt_templates=prompt_templates
32
- )
33
-
34
- if __name__ == "__main__":
35
- from Gradio_UI import GradioUI
36
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, InferenceClientModel
2
+ from tools.party_menu import suggest_menu
3
+ from tools.party_song import suggest_party_music
4
+ from tools.party_time import estimate_preparation_time
5
+ from tools.final_answer import FinalAnswerTool
6
+
7
+ model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-7B-Instruct")
8
+
9
+ agent = CodeAgent(
10
+ model=model,
11
+ tools=[FinalAnswerTool(), suggest_menu, suggest_party_music, estimate_preparation_time],
12
+ )
13
+
14
+ if __name__ == "__main__":
15
+ from Gradio_UI import GradioUI
16
+ GradioUI(agent).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tools/party_menu.py CHANGED
@@ -1,6 +1,5 @@
1
- from smolagents import CodeAgent, tool, InferenceClientModel
2
 
3
- # Tool to suggest a menu based on the occasion
4
  @tool
5
  def suggest_menu(occasion: str) -> str:
6
  """
@@ -20,9 +19,3 @@ def suggest_menu(occasion: str) -> str:
20
  return "Buffet with high-energy and healthy food."
21
  else:
22
  return "Custom menu for the butler."
23
-
24
- # Alfred, the butler, preparing the menu for the party
25
- agent = CodeAgent(tools=[suggest_menu], model=InferenceClientModel())
26
-
27
- # Preparing the menu for the party
28
- agent.run("Prepare a casual menu for the party.")
 
1
+ from smolagents import tool
2
 
 
3
  @tool
4
  def suggest_menu(occasion: str) -> str:
5
  """
 
19
  return "Buffet with high-energy and healthy food."
20
  else:
21
  return "Custom menu for the butler."
 
 
 
 
 
 
tools/party_song.py CHANGED
@@ -1,14 +1,11 @@
1
- import os
2
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
3
-
4
- HF_TOKEN = os.getenv("HF_TOKEN")
5
-
6
- agent = CodeAgent(
7
- tools=[DuckDuckGoSearchTool()],
8
- model=InferenceClientModel(
9
- model_id="Qwen/Qwen2.5-Coder-7B-Instruct", # 或者你要用的模型
10
- api_key=HF_TOKEN
11
- )
12
- )
13
-
14
- agent.run("Search for the best music recommendations for a party at the Wayne's mansion.")
 
1
+ from smolagents import tool, DuckDuckGoSearchTool
2
+
3
+ @tool
4
+ def suggest_party_music(query: str = "best music for a party") -> str:
5
+ """
6
+ Suggests music playlists or songs for a party by searching online.
7
+ Args:
8
+ query (str): Search query for music.
9
+ """
10
+ search_tool = DuckDuckGoSearchTool()
11
+ return search_tool(query)
 
 
 
tools/party_time.py CHANGED
@@ -1,18 +1,23 @@
1
- from smolagents import CodeAgent, InferenceClientModel
2
- import numpy as np
3
- import time
4
  import datetime
5
 
6
- agent = CodeAgent(tools=[], model=InferenceClientModel(), additional_authorized_imports=['datetime'])
7
-
8
- agent.run(
9
  """
10
- Alfred needs to prepare for the party. Here are the tasks:
11
- 1. Prepare the drinks - 30 minutes
12
- 2. Decorate the mansion - 60 minutes
13
- 3. Set up the menu - 45 minutes
14
- 4. Prepare the music and playlist - 45 minutes
15
-
16
- If we start right now, at what time will the party be ready?
17
  """
18
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
 
 
2
  import datetime
3
 
4
+ @tool
5
+ def estimate_preparation_time(start_time: str = None) -> str:
 
6
  """
7
+ Estimates total preparation time for a party and returns when everything will be ready.
8
+ Args:
9
+ start_time (str, optional): Start time in format HH:MM. Defaults to current time.
 
 
 
 
10
  """
11
+ tasks = {
12
+ "Prepare the drinks": 30,
13
+ "Decorate the mansion": 60,
14
+ "Set up the menu": 45,
15
+ "Prepare the music and playlist": 45,
16
+ }
17
+ total_minutes = sum(tasks.values())
18
+ if start_time:
19
+ now = datetime.datetime.strptime(start_time, "%H:%M")
20
+ else:
21
+ now = datetime.datetime.now()
22
+ ready_time = now + datetime.timedelta(minutes=total_minutes)
23
+ return f"Total prep time: {total_minutes} minutes. The party will be ready at {ready_time.strftime('%H:%M')}."