Calculator Tool¶
The CalculatorTool provides mathematical calculation capabilities for agents in the Bedrock Swarm framework. It supports basic arithmetic operations, mathematical functions, and expression evaluation.
Class Documentation¶
Bases: BaseTool
Simple calculator tool for basic arithmetic.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Name of the tool
TYPE:
|
description
|
Description of the tool
TYPE:
|
Source code in src/bedrock_swarm/tools/calculator.py
Attributes¶
name: str
property
¶
Get tool name.
description: str
property
¶
Get tool description.
Functions¶
get_schema() -> Dict[str, Any]
¶
Get JSON schema for the calculator tool.
Source code in src/bedrock_swarm/tools/calculator.py
_execute_impl(*, expression: str, **kwargs: Any) -> str
¶
Execute the calculator.
| PARAMETER | DESCRIPTION |
|---|---|
expression
|
Arithmetic expression to evaluate
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Result of the calculation |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If expression is invalid |
Source code in src/bedrock_swarm/tools/calculator.py
Features¶
The calculator tool supports:
- Basic arithmetic operations:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Exponentiation (**)
-
Modulo (%)
-
Mathematical functions:
- Square root (sqrt)
- Absolute value (abs)
- Trigonometric functions (sin, cos, tan)
- Logarithms (log, ln)
Usage Examples¶
from bedrock_swarm.tools import CalculatorTool
calculator = CalculatorTool()
# Basic arithmetic
result = calculator.evaluate("2 + 2")
print(result) # Output: 4
# Complex expressions
result = calculator.evaluate("sin(45) * sqrt(16)")
print(result) # Output: 2.8284...
# Multiple operations
result = calculator.evaluate("(10 + 5) * 2 / 3")
print(result) # Output: 10.0
Error Handling¶
The calculator handles various error cases:
- Invalid expressions
- Division by zero
- Invalid function calls
- Type mismatches
- Overflow/underflow
Implementation Details¶
The calculator tool:
- Parses mathematical expressions
- Validates input safety
- Handles numeric precision
- Provides detailed error messages
- Supports operator precedence