Scott Cogan commited on
Commit
fb0a09c
·
1 Parent(s): 18ec0f7

fix: convert tools to proper Tool subclasses with name and description attributes

Browse files
Files changed (1) hide show
  1. app.py +41 -5
app.py CHANGED
@@ -154,12 +154,48 @@ with open("prompts.yaml", 'r') as stream:
154
  raise
155
 
156
  # Create the agent with the templates
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  tools = [
158
- Tool(name="final_answer", description="Submit the final answer to the question", func=final_answer),
159
- Tool(name="search", description="Search the web for information", func=DuckDuckGoSearchTool()),
160
- Tool(name="calculate_min_price", description="Calculate the minimum price from a list of prices", func=calculate_min_price),
161
- Tool(name="extract_price_from_snippet", description="Extract prices from a text snippet", func=extract_price_from_snippet),
162
- Tool(name="get_current_time_in_timezone", description="Get the current time in a specified timezone", func=get_current_time_in_timezone)
163
  ]
164
 
165
  agent = CodeAgent(
 
154
  raise
155
 
156
  # Create the agent with the templates
157
+ class CalculateMinPriceTool(Tool):
158
+ name = "calculate_min_price"
159
+ description = "Calculate the minimum price from a list of prices"
160
+ inputs = {'prices': {'type': 'list[float]', 'description': 'List of product prices'}}
161
+ output_type = "string"
162
+
163
+ def forward(self, prices: list[float]) -> str:
164
+ min_price = min(prices)
165
+ return f"The minimum price is {min_price}"
166
+
167
+ class ExtractPriceFromSnippetTool(Tool):
168
+ name = "extract_price_from_snippet"
169
+ description = "Extract prices from a text snippet"
170
+ inputs = {'snippet': {'type': 'string', 'description': 'Text containing prices'}}
171
+ output_type = "list[string]"
172
+
173
+ def forward(self, snippet: str) -> list[str]:
174
+ price_pattern = r'\$\d+(?:,\d{3})*(?:\.\d{2})?|\d+(?:,\d{3})*(?:\.\d{2})?\s*(USD|EUR|GBP|INR|AUD|CAD)?'
175
+ matches = re.findall(price_pattern, snippet)
176
+ matches = [str(x) for x in matches]
177
+ return matches
178
+
179
+ class GetCurrentTimeInTimezoneTool(Tool):
180
+ name = "get_current_time_in_timezone"
181
+ description = "Get the current time in a specified timezone"
182
+ inputs = {'timezone': {'type': 'string', 'description': 'A valid timezone (e.g., America/New_York)'}}
183
+ output_type = "string"
184
+
185
+ def forward(self, timezone: str) -> str:
186
+ try:
187
+ tz = pytz.timezone(timezone)
188
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
189
+ return f"The current local time in {timezone} is: {local_time}"
190
+ except Exception as e:
191
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
192
+
193
  tools = [
194
+ final_answer,
195
+ DuckDuckGoSearchTool(),
196
+ CalculateMinPriceTool(),
197
+ ExtractPriceFromSnippetTool(),
198
+ GetCurrentTimeInTimezoneTool()
199
  ]
200
 
201
  agent = CodeAgent(