47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
TOOL_DEFINITION = {
|
|
"name": "calculator",
|
|
"description": "Performs basic math calculations. Can add, subtract, multiply, or divide two numbers.",
|
|
"input_schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"operation": {
|
|
"type": "string",
|
|
"enum": ["add", "subtract", "multiply", "divide"],
|
|
"description": "The math operation to perform"
|
|
},
|
|
"a": {
|
|
"type": "number",
|
|
"description": "First number"
|
|
},
|
|
"b": {
|
|
"type": "number",
|
|
"description": "Second number"
|
|
}
|
|
},
|
|
"required": ["operation", "a", "b"]
|
|
}
|
|
}
|
|
|
|
|
|
def run(operation: str, a: float, b: float) -> dict:
|
|
"""
|
|
Execute the calculation.
|
|
"""
|
|
operations = {
|
|
"add": lambda x, y: x + y,
|
|
"subtract": lambda x, y: x - y,
|
|
"multiply": lambda x, y: x * y,
|
|
"divide": lambda x, y: x / y if y != 0 else "Error: Division by zero"
|
|
}
|
|
|
|
if operation not in operations:
|
|
return {"error": f"Unknown operation: {operation}"}
|
|
|
|
result = operations[operation](a, b)
|
|
|
|
return {
|
|
"operation": operation,
|
|
"a": a,
|
|
"b": b,
|
|
"result": result
|
|
} |