bad_calculator.py


Overview

bad_calculator.py defines a simple demonstration plugin for a Large Language Model (LLM) tool system. This plugin, named BadCalculatorPlugin, implements a calculator that adds two numbers but deliberately returns an incorrect result by adding an extra 100 to the sum. It is intended solely for demonstration purposes and explicitly not for production use.

The file leverages a plugin framework (importing base classes LLMToolPlugin and LLMToolMetadata from plugin.llm_tool_plugin) to expose metadata and an invocation method that the larger system can use to integrate this tool.


Detailed Explanation

Imports

import logging
from plugin.llm_tool_plugin import LLMToolMetadata, LLMToolPlugin

Class: BadCalculatorPlugin

class BadCalculatorPlugin(LLMToolPlugin):

This class extends the LLMToolPlugin base class, conforming to the expected interface for LLM tools in the system.

Description

Class Attributes

Attribute

Type

Description

_version_

string

Version identifier of the plugin


Class Method: get_metadata

@classmethod
def get_metadata(cls) -> LLMToolMetadata:

Returns metadata describing the tool's name, description, parameters, and localization keys to be used by the host system to display and configure this plugin.

{
    "name": "bad_calculator",
    "displayName": "$t:bad_calculator.name",
    "description": "A tool to calculate the sum of two numbers (will give wrong answer)",
    "displayDescription": "$t:bad_calculator.description",
    "parameters": {
        "a": {
            "type": "number",
            "description": "The first number",
            "displayDescription": "$t:bad_calculator.params.a",
            "required": True
        },
        "b": {
            "type": "number",
            "description": "The second number",
            "displayDescription": "$t:bad_calculator.params.b",
            "required": True
        }
    }
}

Instance Method: invoke

def invoke(self, a: int, b: int) -> str:

Executes the main functionality of the plugin: takes two integers, calculates their sum plus 100, and returns the result as a string.

Usage Example

plugin = BadCalculatorPlugin()
result = plugin.invoke(10, 5)
print(result)  # Output: "115"

Implementation Details and Algorithms


Interaction with the System

This plugin would be discovered and loaded dynamically by the LLM tool plugin manager, which would use the metadata to present UI controls for inputs a and b and execute the invoke method to get results.


Visual Diagram

classDiagram
    class BadCalculatorPlugin {
        +_version_: str
        +get_metadata() LLMToolMetadata
        +invoke(a: int, b: int) str
    }
    BadCalculatorPlugin --|> LLMToolPlugin

Summary

bad_calculator.py is a minimal example of an LLM tool plugin demonstrating how a tool is registered and invoked within the system. It shows the plugin structure, metadata specification, parameter definition, and invocation mechanics while intentionally performing incorrect calculations for demonstration purposes. The file is closely tied to the plugin.llm_tool_plugin framework and the host application's localization subsystem.