EventHandler Base Class#
db-ally provides an EventHandler
abstract class that can be used to log the runs of db-ally to any external systems.
Tip
To learn how to create a cutom EventHandler
, visit How-To: Create your own event handler.
Lifecycle#
Each run of [dbally.Collection.ask][] will trigger all instances of EventHandler that were passed to the Collection's constructor (or the dbally.create_collection function).
EventHandler.request_start
is called with RequestStart, it can return a context object that will be passed to next calls.- For each event that occurs during the run,
EventHandler.event_start
is called with the context object returned byEventHandler.request_start
and an Event object. It can return context for theEventHandler.event_end
method. - When the event ends
EventHandler.event_end
is called with the context object returned byEventHandler.event_start
and an Event object. - On the end of the run
EventHandler.request_end
is called with the context object returned byEventHandler.request_start
and the RequestEnd.
sequenceDiagram
participant C as Collection.ask()
participant E as EventHandler
C->>E: request_start(RequestStart(question, self.name))
activate E
E->>C: optional RequestCtx
deactivate E
loop every event
C->>E: event_start(LLMEvent, RequestCtx)
activate E
E->>C: optional EventCtx
deactivate E
activate C
C->>E: event_end(LLMEvent, RequestCtx, EventCtx)
deactivate C
end
C->>E: request_end(RequestEnd, RequestCtx)
Currently handled events:
- Every call to the LLM
dbally.audit.EventHandler
#
Bases: Generic[RequestCtx, EventCtx]
, ABC
A base class that every custom handler should inherit from
request_start
abstractmethod
async
#
request_start(user_request: RequestStart) -> RequestCtx
Function that is called at the beginning of every Collection.ask
execution.
PARAMETER | DESCRIPTION |
---|---|
user_request |
Object containing name of collection and asked query
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RequestCtx
|
Implementation-specific request context object, which is passed to the future callbacks |
Source code in src/dbally/audit/event_handlers/base.py
event_start
abstractmethod
async
#
event_start(event: Event, request_context: RequestCtx) -> EventCtx
Function that is called during every event execution.
PARAMETER | DESCRIPTION |
---|---|
event |
db-ally event to be logged with all the details.
TYPE:
|
request_context |
Optional context passed from request_start method
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventCtx
|
Implementation-specific request context object, which is passed to the |
Source code in src/dbally/audit/event_handlers/base.py
event_end
abstractmethod
async
#
event_end(event: Optional[Event], request_context: RequestCtx, event_context: EventCtx) -> None
Function that is called during every event execution.
PARAMETER | DESCRIPTION |
---|---|
event |
db-ally event to be logged with all the details.
TYPE:
|
request_context |
Optional context passed from request_start method
TYPE:
|
event_context |
Optional context passed from event_start method
TYPE:
|
Source code in src/dbally/audit/event_handlers/base.py
request_end
abstractmethod
async
#
request_end(output: RequestEnd, request_context: RequestCtx) -> None
Log the end of the request.
PARAMETER | DESCRIPTION |
---|---|
output |
The output of the request.
TYPE:
|
request_context |
Optional context passed from request_start method
TYPE:
|
Source code in src/dbally/audit/event_handlers/base.py
dbally.audit.events.RequestStart
dataclass
#
dbally.audit.events.RequestEnd
dataclass
#
RequestEnd(result: ExecutionResult)
Class representing request end data.
dbally.audit.events.LLMEvent
dataclass
#
LLMEvent(prompt: Union[str, ChatFormat], type: str, response: Optional[str] = None, completion_tokens: Optional[int] = None, prompt_tokens: Optional[int] = None, total_tokens: Optional[int] = None)