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

# pylint: disable=W0613
"""Django signal receivers of ImageSearchPlugin 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.image_search import (
    image_search_plugin_authenticated,
    image_search_plugin_authentication_failed,
    image_search_plugin_failed,
    image_search_plugin_request_completed,
    image_search_plugin_request_started,
    image_search_plugin_responded,
)
from .utils import response_characters

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


[docs] @receiver(image_search_plugin_request_started, dispatch_uid="image_search_plugin_request_started") def handle_image_search_plugin_request_started(sender, plugin: PluginBase, function_args=None, **kwargs): """ Handle ImageSearchPlugin 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(image_search_plugin_authenticated, dispatch_uid="image_search_plugin_authenticated") def handle_image_search_plugin_authenticated(sender, plugin: PluginBase, **kwargs): """ Handle ImageSearchPlugin 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 ``provider`` are logged. """ logger.info( "%s provider: %s", plugin.name, kwargs.get("provider"), )
[docs] @receiver(image_search_plugin_authentication_failed, dispatch_uid="image_search_plugin_authentication_failed") def handle_image_search_plugin_authentication_failed(sender, plugin: PluginBase, error: str, **kwargs): """ Handle ImageSearchPlugin 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 ``provider`` are logged. """ logger.warning( "%s provider: %s error: %s", plugin.name, kwargs.get("provider"), error, )
[docs] @receiver(image_search_plugin_responded, dispatch_uid="image_search_plugin_responded") def handle_image_search_plugin_responded(sender, plugin: PluginBase, response=None, cached: bool = False, **kwargs): """ Handle ImageSearchPlugin 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 ``query`` are logged. """ logger.info( "%s query: %s characters: %s cached: %s", plugin.name, kwargs.get("query"), response_characters(response), cached, )
[docs] @receiver(image_search_plugin_failed, dispatch_uid="image_search_plugin_failed") def handle_image_search_plugin_failed(sender, plugin: PluginBase, error: str, **kwargs): """ Handle ImageSearchPlugin 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(image_search_plugin_request_completed, dispatch_uid="image_search_plugin_request_completed") def handle_image_search_plugin_request_completed(sender, plugin: PluginBase, success: bool, duration: float, **kwargs): """ Handle ImageSearchPlugin 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, )