Tool Protocol
The protocol that a Smarter Extension’s tools implement.
A tool is a subclass of SmarterExtensionTool. It declares its name, the Pydantic
model of its arguments, and a call() method:
from pydantic import BaseModel, Field
from smarter.apps.llmtool.extension import ExtensionContext, ExtensionToolError, SmarterExtensionTool
class WeatherArguments(BaseModel):
location: str = Field(..., description="City and state, e.g. San Francisco, CA")
class GetWeather(SmarterExtensionTool):
"""Get the current weather and a 24-hour forecast for a location."""
name = "get_weather"
arguments_model = WeatherArguments
def call(self, arguments: WeatherArguments, context: ExtensionContext):
api_key = context.settings.google_maps_api_key.get_secret_value()
...
return {"location": arguments.location, "forecast": [...]}
The platform does everything else:
the OpenAI tool definition: the class docstring is its description, and the arguments model’s JSON schema is its parameters;
parsing and validating the LLM’s arguments, and returning validation errors to the LLM;
the
llm_tool_presented,llm_tool_requestedandllm_tool_respondedsignals;turning an exception into an
{"error": ...}result, so that the LLM can recover;a timeout, from the manifest’s
[tool.smarter] timeout.
Raise ExtensionToolError to return a message to the LLM. Any other exception is
logged with its traceback, and the LLM is told that the tool failed.
- class smarter.apps.llmtool.extension.protocol.ExtensionContext(slug, version, settings, account=None, user_profile=None, llmclient=None, logger=None)[source]
Bases:
objectWhat a tool call knows about where it runs.
- Parameters:
slug (
str) – The extension’s slug.version (
str) – The extension’s installed version.settings (
BaseModel) – The extension’s settings, one attribute per setting in its manifest. Asecretsetting is apydantic.SecretStr; an unset setting is None.account (
Account|None) – The account of the LLMClient that called the tool, if any.user_profile (
UserProfile|None) – The user whose prompt called the tool, if any.llmclient (
object|None) – The LLMClient that called the tool, if any.
- __init__(slug, version, settings, account=None, user_profile=None, llmclient=None, logger=None)
- logger: Logger | WaffleSwitchedLoggerWrapper = None
- settings: BaseModel
- user_profile: UserProfile | None = None
- smarter.apps.llmtool.extension.protocol.ExtensionLogger = logging.Logger | smarter.lib.logging.waffle_switched_logger.WaffleSwitchedLoggerWrapper
The logger of an extension’s code.
- exception smarter.apps.llmtool.extension.protocol.ExtensionToolError[source]
Bases:
ExceptionRaised by a tool to tell the LLM why it could not do what was asked.
The message is returned to the LLM as
{"error": "<message>"}, without a traceback.
- class smarter.apps.llmtool.extension.protocol.SmarterExtensionTool[source]
Bases:
objectThe base class of a Smarter Extension’s tools.
Subclasses set
nameandarguments_model, document the tool in the class docstring (or setdescription), and implementcall(). A new instance is created for each tool call.- arguments_model: ClassVar[type[BaseModel]]
The Pydantic model of the tool’s arguments.
Its JSON schema is the tool’s parameters.
- call(arguments, context)[source]
Do what the LLM asked.
- Parameters:
arguments (
BaseModel) – The LLM’s arguments, validated byarguments_model.context (
ExtensionContext) – The extension’s settings, and who called the tool.
- Return type:
TypeAliasType- Returns:
The result for the LLM: any JSON value.
- Raises:
ExtensionToolError – To tell the LLM why the tool could not do what was asked.
- description: ClassVar[str | None] = None
The tool’s description for the LLM.
Defaults to the class docstring.
- classmethod parameters()[source]
The tool’s parameters for the LLM: the JSON schema of
arguments_model.
- classmethod tool_description()[source]
The tool’s description for the LLM:
description, or the class docstring.- Return type:
- smarter.apps.llmtool.extension.protocol.extension_logger(slug)[source]
Return the logger of an extension’s code, switched by
SmarterWaffleSwitches.EXTENSION_LOGGING.- Parameters:
slug (
str) – The extension’s slug.- Return type: