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. A tool that sets cache_ttl has its results cached.

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]
exception smarter.apps.llmtool.extension.toolkit.ExtensionToolCallError[source]

Bases: Exception

A tool call failed.

ExtensionToolkit.call() returns its message to the LLM.

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.

Sends extension_tool_called, then extension_tool_responded or extension_tool_failed, and always extension_tool_completed last.

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.

execute(call)[source]

Run a resolved tool call with the tool runner (smarter.apps.llmtool.runner).

Parameters:

call (ExtensionToolCall) – The call, with its validated arguments, settings, switches and caller.

Return type:

TypeAliasType

Returns:

The tool’s result.

Raises:

ExtensionToolCallError – If the tool fails, times out, or returns a result that is not JSON. Raised, rather than returned, so that an error is never cached.

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.

Sends extension_tool_failed, and logs a warning.

Parameters:
Return type:

dict[str, Any]

Returns:

{"error": error}.

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.

run(function, arguments)[source]

Run one of this toolkit’s tools, for call().

Parameters:
Return type:

TypeAliasType

Returns:

The tool’s result.

Raises:

ExtensionToolCallError – If the arguments are invalid, or the tool fails, times out, or returns a result that is not JSON.

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