Skip to content

[Utilities] LLM Calculator Tool Function #57

@umar-anzar

Description

@umar-anzar
{
    "name": "calculator",
    "description": "Call this function whenever complex mathematical expressions are involved. Evaluates a single symbolic expression using SymPy. Supports constants (pi, E, I), algebra (+, -, *, /, **, sqrt, factor, simplify), trigonometry (sin, cos, tan, asin, acos, atan), logarithms (log with base), combinatorics (factorial, comb, perm), and calculus (diff, integrate, limit). Expressions are symbolic; no substitution.",
    "parameters": {
        "type": "object",
        "properties": {
            "expression": {
                "type": "string",
                "description": "A single symbolic expression as a string, e.g. 'sin(pi/4) + sqrt(2)'"
            }
        },
        "required": ["expression"]
    }
}
def calculator(expression: str) -> str:
    """
    Evaluate a single symbolic expression using SymPy and return numeric result
    rounded to 5 decimal places.
    """
    try:
        # Parse string into SymPy expression
        expr = sympify(expression)
        # Evaluate numerically and round to 5 decimal places
        return str(round(float(expr.evalf()), 5))
    except Exception as e:
        return f"Error: {e}"

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationenhancementNew feature or request

Type

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions