Hook
lazyllm.hook.LazyLLMHook
Bases: ABC
Abstract base class for LazyLLM's hook system, used to insert custom logic before and after function or method execution.
This class is an abstract base class (ABC) that defines the basic interface for the hook system. By inheriting from this class and implementing its abstract methods, you can create custom hooks to monitor, log, or modify function execution processes.
Parameters:
-
obj–The object to monitor (usually a function or method). This object will be stored in the hook instance for use by other methods.
Note: This class is an abstract base class and cannot be instantiated directly. You must inherit from this class and implement all abstract methods to use it.
Source code in lazyllm/hook.py
finalize()
Run the final cleanup logic of the hook lifecycle.
This method is called at the end of the hook lifecycle and is intended for cleanup, resource release, or final reporting.
This is the preferred final-phase interface. For backward compatibility, report is
still supported and treated as a legacy alias of finalize.
Source code in lazyllm/hook.py
on_error(exc)
Error-handling hook, called when the monitored function raises an exception.
The default implementation is a no-op. Subclasses can override it to record error status, attach diagnostic information, or perform cleanup.
Parameters:
-
exc–The exception raised by the monitored function.
Source code in lazyllm/hook.py
post_hook(output)
abstractmethod
Post-hook method, called after the monitored function executes.
This is an abstract method and must be implemented in subclasses.
Parameters:
-
output–The return value of the monitored function.
Source code in lazyllm/hook.py
pre_hook(*args, **kwargs)
abstractmethod
Pre-hook method, called before the monitored function executes.
This is an abstract method and must be implemented in subclasses.
Parameters:
-
*args–Positional arguments passed to the monitored function.
-
**kwargs–Keyword arguments passed to the monitored function.
Source code in lazyllm/hook.py
lazyllm.hook.LazyLLMFuncHook
Bases: LazyLLMHook
Helper class for hooking functions. if the function is a generator function, statements before yield will be executed as pre_hook, and statements after yield will be executed as post_hook.
Parameters:
-
func–The function to hook.