llm_tool_plugin.py

Overview

The llm_tool_plugin.py file defines the foundational structure and interfaces for implementing LLM (Large Language Model) Tool Plugins within a plugin framework. It provides:

This file serves as a contract and helper utility for integrating custom tools that interact with or extend LLM capabilities in a standardized way, enabling seamless plugin management and interoperability within a larger system.


Classes and Functions

TypedDict Classes

LLMToolParameter

A typed dictionary representing a single parameter's specification for an LLM tool.

Field

Type

Description

type

str

The data type of the parameter (e.g., "string", "integer").

description

str

A detailed description of the parameter for internal use.

displayDescription

str

A user-friendly description for display purposes.

required

bool

Whether this parameter is mandatory when invoking the tool.

Example usage:

param_example = LLMToolParameter(
    type="string",
    description="The user's email address.",
    displayDescription="User Email",
    required=True
)

LLMToolMetadata

A typed dictionary representing the metadata of an LLM tool plugin.

Field

Type

Description

name

str

Internal identifier of the tool.

displayName

str

User-friendly name for the tool.

description

str

Internal descriptive text explaining the tool's purpose.

displayDescription

str

User-facing description of the tool.

parameters

dict[str, LLMToolParameter]

Dictionary mapping parameter names to their specifications.

Example usage:

metadata_example = LLMToolMetadata(
    name="translate_text",
    displayName="Text Translator",
    description="Translates text from one language to another.",
    displayDescription="Translate text easily.",
    parameters={
        "source_lang": LLMToolParameter(
            type="string",
            description="Language code to translate from",
            displayDescription="Source Language",
            required=True
        ),
        "target_lang": LLMToolParameter(
            type="string",
            description="Language code to translate to",
            displayDescription="Target Language",
            required=True
        ),
        "text": LLMToolParameter(
            type="string",
            description="Text to be translated",
            displayDescription="Text",
            required=True
        )
    }
)

Class: LLMToolPlugin

This is an abstract base class for all LLM tool plugins. It is decorated using @pluginlib.Parent with a plugin type identifier PLUGIN_TYPE_LLM_TOOLS, indicating that it serves as a parent class within the plugin management system.

Purpose

Methods


Function: llm_tool_metadata_to_openai_tool

Converts LLMToolMetadata into a dictionary formatted as an OpenAI tool function definition, suitable for use in OpenAI API calls that support function calling.

Signature

def llm_tool_metadata_to_openai_tool(llm_tool_metadata: LLMToolMetadata) -> dict[str, Any]

Parameters

Returns

Description

This utility function maps the internally used metadata structure to the OpenAI function calling interface, enabling the plugin's tool to be registered and called as a function by OpenAI-based LLMs.

Implementation Details

Example

openai_tool = llm_tool_metadata_to_openai_tool(metadata_example)

# Output:
# {
#   "type": "function",
#   "function": {
#       "name": "translate_text",
#       "description": "Translates text from one language to another.",
#       "parameters": {
#           "type": "object",
#           "properties": {
#               "source_lang": {"type": "string", "description": "Language code to translate from"},
#               "target_lang": {"type": "string", "description": "Language code to translate to"},
#               "text": {"type": "string", "description": "Text to be translated"}
#           },
#           "required": ["source_lang", "target_lang", "text"]
#       }
#   }
# }

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Diagram: Class Structure of llm_tool_plugin.py

classDiagram
    class LLMToolParameter {
        +type: str
        +description: str
        +displayDescription: str
        +required: bool
    }

    class LLMToolMetadata {
        +name: str
        +displayName: str
        +description: str
        +displayDescription: str
        +parameters: dict[str, LLMToolParameter]
    }

    class LLMToolPlugin {
        <<abstract>>
        +get_metadata(): LLMToolMetadata
        +invoke(**kwargs): str
    }

    LLMToolMetadata o-- LLMToolParameter : parameters

Summary

llm_tool_plugin.py defines the core abstractions and helpers for LLM tool plugins, providing typed metadata structures, an abstract plugin base class, and a conversion utility to adapt plugin metadata to OpenAI's function calling format. It is a foundational piece enabling developers to build and integrate custom LLM tools consistently within a plugin-enabled application environment.