Calculator enables an Agent to perform mathematical calculations.

Example

The following agent will calculate the result of 10*5 and then raise it to the power of 2:

cookbook/tools/calculator_tools.py
from phi.agent import Agent
from phi.tools.calculator import Calculator

agent = Agent(
    tools=[
        Calculator(
            add=True,
            subtract=True,
            multiply=True,
            divide=True,
            exponentiate=True,
            factorial=True,
            is_prime=True,
            square_root=True,
        )
    ],
    show_tool_calls=True,
    markdown=True,
)
agent.print_response("What is 10*5 then to the power of 2, do it step by step")

Toolkit Params

ParameterTypeDefaultDescription
addboolTrueEnables the functionality to perform addition.
subtractboolTrueEnables the functionality to perform subtraction.
multiplyboolTrueEnables the functionality to perform multiplication.
divideboolTrueEnables the functionality to perform division.
exponentiateboolFalseEnables the functionality to perform exponentiation.
factorialboolFalseEnables the functionality to calculate the factorial of a number.
is_primeboolFalseEnables the functionality to check if a number is prime.
square_rootboolFalseEnables the functionality to calculate the square root of a number.

Toolkit Functions

FunctionDescription
addAdds two numbers and returns the result.
subtractSubtracts the second number from the first and returns the result.
multiplyMultiplies two numbers and returns the result.
divideDivides the first number by the second and returns the result. Handles division by zero.
exponentiateRaises the first number to the power of the second number and returns the result.
factorialCalculates the factorial of a number and returns the result. Handles negative numbers.
is_primeChecks if a number is prime and returns the result.
square_rootCalculates the square root of a number and returns the result. Handles negative numbers.