Thursday, April 23, 2026

Implement Calculator Tool using Autogen and Llama3.2(LLM in local machine)

LLMs are pre-trained on large datasets but do not have access to information beyond their training cut-off date.

They cannot retrieve real-time information

They lack enterprise-specific knowledge unless explicitly trained on it.


Example:

AI Assistant for Financial Services Problem: 

A banking chatbot is designed to assist customers with loan eligibility, interest rates, and payment schedules, but: It cannot perform real-time financial calculations. It lacks access to the latest interest rates. Users demand personalized financial advice. 

Solution with Tools: 

Calculator Tool: Computes EMI payments, interest accumulation, and loan eligibility. 

Finance API Tool: Fetches live interest rates and account balances. 

Memory Tool: Stores previous queries to offer personalized financial planning.



Autogen Output Implementing Calculator Tool

References

Shekhar Agrawal; Srinivasa Sunil Chippada; Rathish Mohan. Ultimate Agentic AI with AutoGen for Enterprise Automation: Design, Build, And Deploy Enterprise-Grade AI Agents Using LLMs and AutoGen To Power Intelligent, ... Enterprise Automation (English Edition). Orange Education Pvt Ltd, AVA™. Kindle Edition. 


Python Code

import autogen

import math


# =========================

# 1. Define LLM config

# =========================

config_list = [

    {

        "model": "llama3.2",

        "base_url": "http://localhost:11434/v1",  # Ollama endpoint

        "api_key": "ollama"  # dummy value

    }

]


llm_config = {

    "config_list": config_list,

    "temperature": 0,

}


# =========================

# 2. Create calculator tool

# =========================

def calculator(expression: str) -> str:

    """

    Evaluate a math expression safely.

    """

    try:

        # Allowed names (safe eval)

        allowed_names = {

            "sqrt": math.sqrt,

            "pow": pow,

            "sin": math.sin,

            "cos": math.cos,

            "tan": math.tan,

            "pi": math.pi,

            "e": math.e,

        }


        result = eval(expression, {"__builtins__": {}}, allowed_names)

        return str(result)

    except Exception as e:

        return f"Error: {str(e)}"


# =========================

# 3. Create Assistant Agent

# =========================

assistant = autogen.AssistantAgent(

    name="assistant",

    llm_config=llm_config,

)


# =========================

# 4. Create User Proxy Agent

# =========================

user_proxy = autogen.UserProxyAgent(

    name="user_proxy",

    human_input_mode="NEVER",

    code_execution_config=False,

)


# =========================

# 5. Register tool

# =========================

assistant.register_for_llm(

    name="calculator",

    description="Evaluate mathematical expressions"

)(calculator)


user_proxy.register_for_execution(

    name="calculator"

)(calculator)


# =========================

# 6. Start conversation

# =========================

user_proxy.initiate_chat(

    assistant,

    message="What is (5 + 3) * sqrt(16)?"

)

No comments:

Post a Comment