phanerozoic commited on
Commit
596d8ad
·
verified ·
1 Parent(s): 5ca0b03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -24
app.py CHANGED
@@ -1,17 +1,26 @@
1
- from smolagents import CodeAgent, HfApiModel, load_tool, tool
 
 
 
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
 
7
- # Import our custom tools from the tools folder
 
 
 
 
8
  from tools.final_answer import FinalAnswerTool
9
  from tools.visit_webpage import VisitWebpageTool
10
  from tools.web_search import DuckDuckGoSearchTool
11
 
 
12
  from Gradio_UI import GradioUI
13
 
14
- # Example Tool (non-functioning): provided purely as a template.
 
15
  @tool
16
  def my_custom_tool(arg1: str, arg2: int) -> str:
17
  """Example Tool: A non-functional tool provided as a template.
@@ -22,7 +31,8 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
22
  """
23
  return "What magic will you build ?"
24
 
25
- # Working Tool: Fetches the current local time in a specified timezone.
 
26
  @tool
27
  def get_current_time_in_timezone(timezone: str) -> str:
28
  """Working Tool: Fetches the current local time in a specified timezone.
@@ -37,37 +47,56 @@ def get_current_time_in_timezone(timezone: str) -> str:
37
  except Exception as e:
38
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
39
 
40
- # Instantiate the FinalAnswerTool (required for returning final answers)
 
41
  final_answer = FinalAnswerTool()
42
 
43
- # Instantiate working local tools:
44
- # Web Search Tool: Uses DuckDuckGo to perform a web search.
45
- search_tool = DuckDuckGoSearchTool() # Defined in tools/web_search.py
46
 
47
- # Webpage Visit Tool: Visits a URL and converts its content to Markdown.
48
- visit_tool = VisitWebpageTool() # Defined in tools/visit_webpage.py
49
-
50
- # Define the model configuration using HfApiModel
51
  model = HfApiModel(
52
  max_tokens=2096,
53
  temperature=0.5,
54
- model_id='Qwen/Qwen3-32B', # Adjust if this model is overloaded.
55
  custom_role_conversions=None,
56
  )
57
 
58
- # Load prompt templates from prompts.yaml
59
- with open("prompts.yaml", 'r') as stream:
60
- prompt_templates = yaml.safe_load(stream)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- # Create the CodeAgent, including our working tools and the example tool.
 
63
  agent = CodeAgent(
64
  model=model,
65
  tools=[
66
- final_answer, # Must remain included.
67
- search_tool, # Enables web search.
68
- visit_tool, # Enables webpage visits.
69
- get_current_time_in_timezone, # Working tool for time queries.
70
- my_custom_tool, # Example (non-functioning) tool.
71
  ],
72
  max_steps=6,
73
  verbosity_level=1,
@@ -75,8 +104,10 @@ agent = CodeAgent(
75
  planning_interval=None,
76
  name=None,
77
  description=None,
 
78
  prompt_templates=prompt_templates
79
  )
80
 
81
- # Launch the Gradio UI for interactive use
82
- GradioUI(agent).launch()
 
 
1
+ #!/usr/bin/env python
2
+ # coding=utf-8
3
+
4
+ import copy
5
  import datetime
6
  import requests
7
  import pytz
8
  import yaml
9
 
10
+ # smolagents imports:
11
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
12
+ from smolagents.prompts import DEFAULT_PROMPT_TEMPLATES
13
+
14
+ # Tools from your local folder
15
  from tools.final_answer import FinalAnswerTool
16
  from tools.visit_webpage import VisitWebpageTool
17
  from tools.web_search import DuckDuckGoSearchTool
18
 
19
+ # Gradio UI from your local file
20
  from Gradio_UI import GradioUI
21
 
22
+ ###############################################################
23
+ # Example custom tool: a non-functional template
24
  @tool
25
  def my_custom_tool(arg1: str, arg2: int) -> str:
26
  """Example Tool: A non-functional tool provided as a template.
 
31
  """
32
  return "What magic will you build ?"
33
 
34
+ ###############################################################
35
+ # A working tool: get the current time in a given timezone
36
  @tool
37
  def get_current_time_in_timezone(timezone: str) -> str:
38
  """Working Tool: Fetches the current local time in a specified timezone.
 
47
  except Exception as e:
48
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
49
 
50
+ ###############################################################
51
+ # Instantiate final_answer (required for returning final answers)
52
  final_answer = FinalAnswerTool()
53
 
54
+ # Other local tools: web search & visit page
55
+ search_tool = DuckDuckGoSearchTool()
56
+ visit_tool = VisitWebpageTool()
57
 
58
+ ###############################################################
59
+ # Define model configuration using HfApiModel
 
 
60
  model = HfApiModel(
61
  max_tokens=2096,
62
  temperature=0.5,
63
+ model_id='Qwen/Qwen3-32B', # Adjust if overloaded or unavailable
64
  custom_role_conversions=None,
65
  )
66
 
67
+ ###############################################################
68
+ # 1) Read your custom prompts.yaml
69
+ try:
70
+ with open("prompts.yaml", 'r') as stream:
71
+ custom_prompts = yaml.safe_load(stream)
72
+ except Exception as e:
73
+ print("Error loading prompts.yaml:", e)
74
+ custom_prompts = {}
75
+
76
+ # 2) Merge with the built-in defaults so no keys or subkeys are missing
77
+ prompt_templates = copy.deepcopy(DEFAULT_PROMPT_TEMPLATES)
78
+
79
+ def deep_merge(base_dict, override_dict):
80
+ """Recursively merge override_dict into base_dict."""
81
+ for k, v in override_dict.items():
82
+ if isinstance(v, dict) and isinstance(base_dict.get(k), dict):
83
+ deep_merge(base_dict[k], v)
84
+ else:
85
+ base_dict[k] = v
86
+
87
+ # Perform the merge
88
+ deep_merge(prompt_templates, custom_prompts)
89
 
90
+ ###############################################################
91
+ # Create the CodeAgent with the merged prompt templates
92
  agent = CodeAgent(
93
  model=model,
94
  tools=[
95
+ final_answer,
96
+ search_tool,
97
+ visit_tool,
98
+ get_current_time_in_timezone,
99
+ my_custom_tool, # Example tool
100
  ],
101
  max_steps=6,
102
  verbosity_level=1,
 
104
  planning_interval=None,
105
  name=None,
106
  description=None,
107
+ # The merged dictionary—ensures all required keys & structure are present
108
  prompt_templates=prompt_templates
109
  )
110
 
111
+ ###############################################################
112
+ # Launch Gradio UI
113
+ GradioUI(agent).launch()