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
logging: Used to record informational messages when the plugin is invoked.
LLMToolMetadata, LLMToolPlugin: Base classes/types from the plugin framework used to define plugin metadata and behavior.
Class: BadCalculatorPlugin
class BadCalculatorPlugin(LLMToolPlugin):
This class extends the LLMToolPlugin base class, conforming to the expected interface for LLM tools in the system.
Description
Purpose: Implements a calculator tool that takes two numbers as input and returns their sum plus 100.
Usage Context: Demonstration only; the "bad" result illustrates a plugin returning deliberately incorrect calculations.
Version:
1.0.0
Class Attributes
Attribute | Type | Description |
|---|---|---|
| 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.
Returns:
LLMToolMetadatadictionary with the following structure:
{
"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
}
}
}
Parameter Descriptions:
a: The first numeric input, required.b: The second numeric input, required.
Localization Keys (strings starting with
$t:) indicate the system supports internationalization, substituting these keys with localized text.
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.
Parameters:
a(int): First input number.b(int): Second input number.
Returns:
str— The incorrect suma + b + 100converted to a string.Behavior:
Logs an info-level message indicating the input arguments.
Returns the sum with the additional 100 added.
Usage Example
plugin = BadCalculatorPlugin()
result = plugin.invoke(10, 5)
print(result) # Output: "115"
Implementation Details and Algorithms
The core logic is a simple arithmetic operation:
a + b + 100.The addition of 100 is an intentional incorrect manipulation to highlight the plugin's "bad" nature.
Logging is used for traceability when the plugin is invoked, aiding debugging or audit trails.
Interaction with the System
Plugin Framework Integration: The class inherits from
LLMToolPlugin, meaning it follows a common interface expected by the LLM tool system for discovery and invocation.Metadata Exposure: The
get_metadataclass method provides structured information for registering and describing the tool within an LLM environment.Invocation: The system calls
invokewith parameters extracted from user input or other tool chains.Localization: The use of localization keys in metadata indicates integration with a translation/localization system within the host application.
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
Explanation:
BadCalculatorPlugininherits fromLLMToolPlugin.It exposes two main methods:
get_metadata(class method) andinvoke(instance method).The
versionattribute denotes the plugin version.
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.