Calculator Agent

Mathematical calculation tools for AI agents.

python
from praisonaiagents import Agent
from praisonai_tools import evaluate

agent = Agent(name="MathAssistant", tools=[evaluate])
agent.start("What is the area of a circle with radius 5?")

The user asks a maths question; the agent evaluates expressions and returns a precise answer.

Diagram

Input

Agent

Tool

Output

Diagram
Calculator AgentAgentUserCalculator AgentAgentUserRequestProcess requestResultResponse

Calculator Tools

Use Calculator Tools to perform mathematical calculations with AI agents.

  1. Install Dependencies

    First, install the required packages:

    bash
    pip install praisonaiagents praisonai-tools sympy pint
  2. Import Components

    Import the necessary components:

    python
    from praisonaiagents import Agent, Task, AgentTeam
    from praisonai_tools import (
        evaluate, solve_equation, convert_units,
        calculate_statistics, calculate_financial
    )
  3. Create Agent

    Create a calculator agent:

    python
    calculator_agent = Agent(
        name="MathAssistant",
        role="Mathematical Computation Specialist",
        goal="Perform accurate mathematical calculations and solve equations.",
        backstory="Expert in mathematical computations and problem-solving.",
        tools=[
            evaluate, solve_equation, convert_units,
            calculate_statistics, calculate_financial
        ],
        reflection=False
    )
  4. Define Task

    Define the calculation task:

    python
    calc_task = Task(
        description="Calculate the area of a circle with radius 5",
        expected_output="The area of the circle in square units",
        agent=calculator_agent,
        name="circle_area"
    )
  5. Run Agent

    Initialize and run the agent:

    python
    agents = AgentTeam(
        agents=[calculator_agent],
        tasks=[calc_task],
        process="sequential"
    )
    agents.start()

Understanding Calculator Tools

What are Calculator Tools?

Calculator Tools provide mathematical computation capabilities for AI agents: – Expression evaluation – Equation solving – Unit conversions – Support for mathematical functions – Variable substitution

Key Components

Available Functions

python
from praisonai_tools import evaluate
from praisonai_tools import solve_equation
from praisonai_tools import convert_units
from praisonai_tools import calculate_statistics
from praisonai_tools import calculate_financial

Advanced Usage

Expression Evaluation

Evaluate complex mathematical expressions with variables:

python
from praisonai_tools import evaluate

# Basic arithmetic
result = evaluate("2 + 2 * 3")

# With mathematical functions
result = evaluate("sin(pi/2) + sqrt(16)")

# With variables
result = evaluate(
    "x^2 + y",
    variables={"x": 3, "y": 4}
)
Equation Solving

Solve different types of equations:

python
from praisonai_tools import solve_equation

# Linear equation
result = solve_equation("2*x + 3 = 7")

# With custom variable
result = solve_equation("y^2 = 4", variable="y")
Unit Conversions

Convert between different units:

python
from praisonai_tools import convert_units

# Length conversion
result = convert_units(100, "meters", "kilometers")

# Temperature conversion
result = convert_units(32, "fahrenheit", "celsius")

# Time conversion
result = convert_units(2.5, "hours", "minutes")
Statistical Calculations

Calculate statistical metrics:

python
from praisonai_tools import calculate_statistics

result = calculate_statistics([1, 2, 3, 4, 5])
Financial Calculations

Calculate financial metrics:

python
from praisonai_tools import calculate_financial

result = calculate_financial(1000, 5, 2)

Error Handling

Error Management

The tools include comprehensive error handling:

python
# Returns success result
{"result": 8.0}

# Returns error message
{"error": "Invalid expression"}

Best Practices

Precision Control

Control calculation precision:

python
evaluate(
    "pi",
    precision=5
)
Safe Execution

Uses safe evaluation environment with: – Restricted builtins – Mathematical functions only – Variable validation

Examples

Basic Calculator Agent

python
from praisonaiagents import Agent, Task, AgentTeam
from praisonai_tools import evaluate, solve_equation, convert_units

# Create calculator agent
calc_agent = Agent(
    name="MathExpert",
    role="Mathematical Specialist",
    goal="Solve mathematical problems accurately.",
    backstory="Expert in mathematics and calculations.",
    tools=[evaluate, solve_equation, convert_units],
    reflection=False
)

# Define task
calc_task = Task(
    description="Solve the equation 2x + 5 = 15",
    expected_output="Value of x",
    agent=calc_agent,
    name="equation_solver"
)

# Run agent
agents = AgentTeam(
    agents=[calc_agent],
    tasks=[calc_task],
    process="sequential"
)
agents.start()

Related