"""
The protocol that a Smarter Extension's tools implement.
A tool is a subclass of :class:`SmarterExtensionTool`. It declares its name, the Pydantic
model of its arguments, and a :meth:`~SmarterExtensionTool.call` method:
.. code-block:: python
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_requested`` and ``llm_tool_responded`` signals;
- turning an exception into an ``{"error": ...}`` result, so that the LLM can recover;
- a timeout, from the manifest's ``[tool.smarter] timeout``.
Raise :class:`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.
"""
import inspect
import logging as python_logging
import re
from dataclasses import dataclass, field
from typing import ClassVar, Optional, Union
from pydantic import BaseModel, JsonValue
from smarter.lib import logging
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.typing import SmarterDictType
TOOL_NAME_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9_]{0,47}$")
"""A tool's name: letters, digits and underscores, starting with a letter, at most 48 characters."""
MAX_DESCRIPTION_LENGTH = 1024
"""The maximum length of a tool description offered to the LLM."""
ExtensionLogger = Union[python_logging.Logger, logging.WaffleSwitchedLoggerWrapper]
"""The logger of an extension's code."""
[docs]
def switch_name(slug: str, name: str) -> str:
"""
Return the waffle switch name of one of an extension's switches.
:param slug: The extension's slug, e.g. ``date-calculator``.
:param name: The switch's name in the manifest, e.g. ``logging``.
:returns: e.g. ``ext_date_calculator_logging``.
"""
return f"ext_{slug.replace('-', '_')}_{name}"
[docs]
def extension_logger(slug: str) -> ExtensionLogger:
"""
Return the logger of an extension's code, switched by ``SmarterWaffleSwitches.EXTENSION_LOGGING``.
:param slug: The extension's slug.
"""
return logging.getSmarterLogger(
f"smarter_extensions.{slug}", any_switches=[SmarterWaffleSwitches.EXTENSION_LOGGING]
)
[docs]
@dataclass(frozen=True)
class ExtensionCaller:
"""
Who called a tool: plain identifiers, not the platform's database objects.
An extension's code is a third party's. It gets what identifies the caller, and nothing that would
let it read or change the platform's data, or reach a user's personal details.
:param account_number: The account of the LLMClient that called the tool, e.g. ``1234-5678-9012``.
:param user_id: The id of the user whose prompt called the tool.
:param llmclient_name: The name of the LLMClient that called the tool.
"""
account_number: Optional[str] = None
user_id: Optional[int] = None
llmclient_name: Optional[str] = None
[docs]
@dataclass(frozen=True)
class ExtensionContext:
"""
What a tool call knows about where it runs.
Everything here is a plain value, decided before the call: it reads no database, so that the
tool can run in a worker without one (ADR-038).
:param slug: The extension's slug.
:param version: The extension's installed version.
:param settings: The extension's settings, one attribute per setting in its manifest.
A ``secret`` setting is a :class:`pydantic.SecretStr`; an unset setting is None.
:param caller: Who called the tool: the account number, the user's id and the LLMClient's name.
:param switches: The extension's own waffle switches, by their names in the manifest, and whether each is on.
"""
slug: str
version: str
settings: BaseModel
caller: ExtensionCaller = field(default_factory=ExtensionCaller)
switches: dict[str, bool] = field(default_factory=dict)
logger: ExtensionLogger = field(default=None, compare=False) # type: ignore[assignment]
def __post_init__(self):
if self.logger is None:
object.__setattr__(self, "logger", extension_logger(self.slug))
[docs]
def switch_is_active(self, name: str) -> bool:
"""
Return whether one of the extension's waffle switches is on.
:param name: The switch's name in the manifest's ``[[tool.smarter.switches]]``, without the ``ext_<slug>_`` prefix.
:returns: True if the switch exists and is on, as it was when the tool was called.
"""
return self.switches.get(name, False)
__all__ = [
"ExtensionCaller",
"ExtensionContext",
"ExtensionLogger",
"ExtensionToolError",
"SmarterExtensionTool",
"extension_logger",
"switch_name",
]