DragonProgrammer commited on
Commit
bcbd180
·
verified ·
1 Parent(s): f70d92b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -147,6 +147,7 @@ def safe_calculator(expression: str) -> str:
147
 
148
  # --- Basic Agent Definition ---
149
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
150
  class HfAgentWrapper:
151
  def __init__(self):
152
  print("Initializing HfAgentWrapper...")
@@ -154,31 +155,49 @@ class HfAgentWrapper:
154
 
155
  print(f"Loading HfAgent with model: {model_repo}")
156
  try:
157
- # Your original tool functions
158
- # get_current_time_in_timezone, web_search, safe_calculator
159
-
160
- # Manually wrap your functions into Tool objects
161
- # Tool.from_function(func) uses func.__name__ for the tool name
162
- # and func.__doc__ for the tool description.
163
- time_tool_obj = Tool.from_function(get_current_time_in_timezone)
164
- search_tool_obj = Tool.from_function(web_search)
165
- calculator_tool_obj = Tool.from_function(safe_calculator)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  # This is the list of Tool objects that HfAgent will receive
168
  self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
169
-
170
- # For printing/logging purposes, let's see their names
171
  tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
172
- print(f"Prepared Tool objects with names: {tool_names_for_log}")
173
 
174
  self.agent = HfAgent(model_repo, additional_tools=self.actual_tools_for_agent)
175
-
176
  print(f"HfAgent loaded successfully with model {model_repo}.")
177
- print(f"HfAgent initialized with tools: {tool_names_for_log}") # Use the names from the Tool objects
 
 
178
 
179
  except Exception as e:
180
  print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
181
- # Ensure the full traceback of the original error 'e' is visible in logs
182
  import traceback
183
  print("Full traceback of HfAgent initialization error:")
184
  traceback.print_exc()
 
147
 
148
  # --- Basic Agent Definition ---
149
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
150
+ # --- Agent Definition using HfAgent ---
151
  class HfAgentWrapper:
152
  def __init__(self):
153
  print("Initializing HfAgentWrapper...")
 
155
 
156
  print(f"Loading HfAgent with model: {model_repo}")
157
  try:
158
+ # Ensure your tool functions have docstrings, as they are used for the description.
159
+ # (Your current tool functions look like they do, which is good)
160
+ if not get_current_time_in_timezone.__doc__:
161
+ raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
162
+ if not web_search.__doc__:
163
+ raise ValueError("Tool 'web_search' is missing a docstring.")
164
+ if not safe_calculator.__doc__:
165
+ raise ValueError("Tool 'safe_calculator' is missing a docstring.")
166
+
167
+ # Manually wrap your functions into Tool objects using the Tool constructor
168
+ # The constructor for Tool in this version is typically:
169
+ # Tool(name: str, func: Callable, description: str)
170
+ time_tool_obj = Tool(
171
+ name=get_current_time_in_timezone.__name__,
172
+ func=get_current_time_in_timezone,
173
+ description=get_current_time_in_timezone.__doc__
174
+ )
175
+ search_tool_obj = Tool(
176
+ name=web_search.__name__,
177
+ func=web_search,
178
+ description=web_search.__doc__
179
+ )
180
+ calculator_tool_obj = Tool(
181
+ name=safe_calculator.__name__,
182
+ func=safe_calculator,
183
+ description=safe_calculator.__doc__
184
+ )
185
 
186
  # This is the list of Tool objects that HfAgent will receive
187
  self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
188
+
 
189
  tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
190
+ print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
191
 
192
  self.agent = HfAgent(model_repo, additional_tools=self.actual_tools_for_agent)
193
+
194
  print(f"HfAgent loaded successfully with model {model_repo}.")
195
+ # HfAgent's toolbox will also have its own representation of these tools
196
+ print(f"HfAgent internal toolbox tool names: {[tool.name for tool in self.agent.toolbox.tools()]}")
197
+
198
 
199
  except Exception as e:
200
  print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
 
201
  import traceback
202
  print("Full traceback of HfAgent initialization error:")
203
  traceback.print_exc()