Prerequisites

  • Python 3.10 or higher
  • PraisonAI Agents package installed
  • sympy and pint packages installed

Calculator Tools

Use Calculator Tools to perform mathematical calculations with AI agents.

1

Install Dependencies

First, install the required packages:

pip install praisonaiagents sympy pint
2

Import Components

Import the necessary components:

from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import (
    evaluate, solve_equation, convert_units,
    calculate_statistics, calculate_financial
)
3

Create Agent

Create a calculator agent:

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
    ],
    self_reflect=False
)
4

Define Task

Define the calculation task:

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:

agents = PraisonAIAgents(
    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

Expression Evaluation

Evaluate mathematical expressions:

result = evaluate("2 + 2 * 3")

Equation Solving

Solve mathematical equations:

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

Unit Conversions

Convert between units:

result = convert_units(100, "meters", "kilometers")

Statistical Calculations

Calculate statistical metrics:

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

Financial Calculations

Calculate financial metrics:

result = calculate_financial(1000, 5, 2)

Advanced Usage

Error Handling

Error Management

The tools include comprehensive error handling:

# Returns success result
{"result": 8.0}

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

Best Practices

Precision Control

Control calculation precision:

evaluate(
    "pi",
    precision=5
)

Safe Execution

Uses safe evaluation environment with:

  • Restricted builtins
  • Mathematical functions only
  • Variable validation

Was this page helpful?