Toolkit

The extension tools that a prompt offers the LLM.

ExtensionToolkit turns an LLMClient’s extensions into what an OpenAI-compatible chat completion needs:

  • tools: one function tool per extension tool, named ext_<slug>_<tool name>, with the tool’s description and its arguments model’s JSON schema;

  • tool calls: ExtensionToolkit.call() validates the LLM’s arguments, runs the tool with a timeout, and returns its result. Errors are returned to the LLM as {"error": ...}, so that it can recover, rather than failing the prompt.

An extension that is disabled, not available to the LLMClient’s account, or whose code does not load, is skipped and reported in ExtensionToolkit.errors, so that one broken extension does not fail the prompt.

class smarter.apps.llmtool.extension.toolkit.ExtensionFunction(name, extension, loaded, tool)[source]

Bases: object

A function tool offered to the LLM, and the extension tool it calls.

__init__(name, extension, loaded, tool)
extension: Extension
loaded: LoadedExtension
name: str
tool: type[SmarterExtensionTool]
class smarter.apps.llmtool.extension.toolkit.ExtensionToolkit(extensions, account=None, user_profile=None, llmclient=None)[source]

Bases: object

The extension tools and tool call dispatch of one prompt.

Parameters:
  • extensions (list[Extension]) – The LLMClient’s extensions.

  • account (Account | None) – The LLMClient’s account, which an extension must be available to.

  • user_profile (UserProfile | None) – The user whose prompt this is.

  • llmclient (object | None) – The LLMClient.

Example usage:

toolkit = ExtensionToolkit(LLMClientExtensions.extensions_for(llmclient), account=account).load()
tools.extend(toolkit.tools)
...
if toolkit.is_extension_function(tool_call.function.name):
    result = toolkit.call(tool_call.function.name, arguments)
__init__(extensions, account=None, user_profile=None, llmclient=None)[source]
call(function_name, arguments=None)[source]

Run a tool call, and return its result for the LLM.

Errors, including invalid arguments, an exception and a timeout, are returned as {"error": "..."}, so that the LLM can recover.

Parameters:
  • function_name (str) – The function name that the LLM called.

  • arguments (dict[str, Any] | str | None) – The function’s arguments, as a dict or a JSON string.

Return type:

TypeAliasType

Returns:

The tool’s result.

extension_for(function_name)[source]

Return the extension of one of this toolkit’s tools.

Return type:

Extension | None

failed(function, error)[source]

Report a failed tool call, and return the error for the LLM.

Return type:

dict[str, Any]

property formatted_class_name: str

The class name, for logging.

function_name(slug, tool_name)[source]

Return a unique OpenAI function name for one of an extension’s tools.

The name is ext_<slug>_<tool name>, with the slug’s hyphens replaced by underscores, and at most 64 characters long. If two tools would have the same name, a short hash is appended.

Parameters:
  • slug (str) – The extension’s slug.

  • tool_name (str) – The tool’s name.

Return type:

str

Returns:

The function name.

is_extension_function(function_name)[source]

Return whether a function name is one of this toolkit’s tools.

Return type:

bool

load()[source]

Load each enabled extension’s code, if this process has not already, and build its tools.

Return type:

ExtensionToolkit

Returns:

This toolkit.

tool_name_for(function_name)[source]

Return the extension tool’s own name, e.g. get_weather, of one of this toolkit’s functions.

Return type:

str | None