1. Added my first Tool: Calculator
Browse files
app.py
CHANGED
@@ -33,7 +33,20 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
@@ -42,7 +55,7 @@ final_answer = FinalAnswerTool()
|
|
42 |
model = HfApiModel(
|
43 |
max_tokens=2096,
|
44 |
temperature=0.5,
|
45 |
-
model_id='
|
46 |
custom_role_conversions=None,
|
47 |
)
|
48 |
|
@@ -55,8 +68,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
59 |
-
max_steps=
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
62 |
planning_interval=None,
|
|
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
+
@tool
|
37 |
+
def calculator(expression: str) -> str:
|
38 |
+
"""A tool that evaluates mathematical expressions.
|
39 |
+
Args:
|
40 |
+
expression: A string containing a mathematical expression (e.g., '2 + 2', '5 * 10', 'sqrt(16)').
|
41 |
+
"""
|
42 |
+
try:
|
43 |
+
# Use Python's built-in eval function for simple calculations
|
44 |
+
# This is safe as long as the agent is using it internally
|
45 |
+
result = eval(expression, {"__builtins__": {}}, {"sqrt": lambda x: x**0.5})
|
46 |
+
return f"Result of {expression} = {result}"
|
47 |
+
except Exception as e:
|
48 |
+
return f"Error calculating '{expression}': {str(e)}"
|
49 |
+
|
50 |
final_answer = FinalAnswerTool()
|
51 |
|
52 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
|
|
55 |
model = HfApiModel(
|
56 |
max_tokens=2096,
|
57 |
temperature=0.5,
|
58 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
|
59 |
custom_role_conversions=None,
|
60 |
)
|
61 |
|
|
|
68 |
|
69 |
agent = CodeAgent(
|
70 |
model=model,
|
71 |
+
tools=[final_answer, calculator], ## add your tools here (don't remove final answer)
|
72 |
+
max_steps=3,
|
73 |
verbosity_level=1,
|
74 |
grammar=None,
|
75 |
planning_interval=None,
|