Source code for smarter.apps.llmtool.receivers.api

# pylint: disable=W0613
"""Django signal receivers of ApiPlugin tool calls."""

from django.dispatch import receiver

from smarter.lib import logging
from smarter.lib.django.waffle import SmarterWaffleSwitches

from ..plugin.static import PluginBase
from ..signals.api import (
    api_plugin_authenticated,
    api_plugin_authentication_failed,
    api_plugin_failed,
    api_plugin_request_completed,
    api_plugin_request_started,
    api_plugin_responded,
)
from .utils import response_characters

logger = logging.getSmarterLogger(__name__, any_switches=[SmarterWaffleSwitches.RECEIVER_LOGGING])


[docs] @receiver(api_plugin_request_started, dispatch_uid="api_plugin_request_started") def handle_api_plugin_request_started(sender, plugin: PluginBase, function_args=None, **kwargs): """ Handle ApiPlugin request started signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. :param function_args: The arguments that the LLM called the tool with. """ logger.info( "%s function_args: %s", plugin.name, function_args, )
[docs] @receiver(api_plugin_authenticated, dispatch_uid="api_plugin_authenticated") def handle_api_plugin_authenticated(sender, plugin: PluginBase, **kwargs): """ Handle ApiPlugin authenticated signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. :param kwargs: The signal's other arguments, of which ``connection``, ``auth_method``, ``status_code`` are logged. """ logger.info( "%s connection: %s auth_method: %s status_code: %s", plugin.name, kwargs.get("connection"), kwargs.get("auth_method"), kwargs.get("status_code"), )
[docs] @receiver(api_plugin_authentication_failed, dispatch_uid="api_plugin_authentication_failed") def handle_api_plugin_authentication_failed(sender, plugin: PluginBase, error: str, **kwargs): """ Handle ApiPlugin authentication failed signal. Logs it at WARNING. :param sender: The class that sent the signal. :param plugin: The plugin. :param error: Why it failed. :param kwargs: The signal's other arguments, of which ``connection``, ``auth_method``, ``status_code`` are logged. """ logger.warning( "%s connection: %s auth_method: %s status_code: %s error: %s", plugin.name, kwargs.get("connection"), kwargs.get("auth_method"), kwargs.get("status_code"), error, )
[docs] @receiver(api_plugin_responded, dispatch_uid="api_plugin_responded") def handle_api_plugin_responded(sender, plugin: PluginBase, response=None, cached: bool = False, **kwargs): """ Handle ApiPlugin responded signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. :param response: The response, whose length in characters is logged. :param cached: Whether the response was served from the cache. :param kwargs: The signal's other arguments, of which ``method``, ``endpoint`` are logged. """ logger.info( "%s %s %s characters: %s cached: %s", plugin.name, kwargs.get("method"), kwargs.get("endpoint"), response_characters(response), cached, )
[docs] @receiver(api_plugin_failed, dispatch_uid="api_plugin_failed") def handle_api_plugin_failed(sender, plugin: PluginBase, error: str, **kwargs): """ Handle ApiPlugin failed signal. Logs it at WARNING. :param sender: The class that sent the signal. :param plugin: The plugin. :param error: Why it failed. """ logger.warning("%s failed: %s", plugin.name, error)
[docs] @receiver(api_plugin_request_completed, dispatch_uid="api_plugin_request_completed") def handle_api_plugin_request_completed(sender, plugin: PluginBase, success: bool, duration: float, **kwargs): """ Handle ApiPlugin request completed signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. :param success: Whether the request succeeded. :param duration: Seconds that the request took. """ logger.info( "%s success: %s duration: %.3fs", plugin.name, success, duration, )