Event
Modules concerning the event handling system used throughout the Gen AI applications.
EventEmitter(handlers, event_level=EventLevel.INFO)
Handles events emitting using event handlers with various levels and types.
The EventEmitter
class is responsible for handling and emitting events using a list of event handlers.
Events are processed based on their severity level and type, with the option to disable specific handlers.
Attributes:
Name | Type | Description |
---|---|---|
handlers |
list[BaseEventHandler]
|
A list of event handlers to process emitted events. |
severity |
int
|
The minimum severity level of events to be processed. |
Raises:
Type | Description |
---|---|
ValueError
|
If the event handlers list is empty. |
ValueError
|
If an invalid event level is provided. |
Initializes a new instance of the EventEmitter class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handlers |
list[BaseEventHandler]
|
A list of event handlers to process emitted events. |
required |
event_level |
EventLevel
|
The minimum severity level of events to be processed. Defaults to EventLevel.INFO. |
INFO
|
close()
async
Closes all handlers in the handler list.
This method iterates through the list of handlers and calls the close
method on each handler.
Returns:
Type | Description |
---|---|
None
|
None |
emit(value, event_level=EventLevel.DEBUG, event_type=EventType.STATUS, disabled_handlers=None)
async
Emits an event using the event handlers with the specified severity and type.
This method emits an event by creating an Event
object with the provided message, severity level,
and event type. It then passes the event to the available handlers, unless they are listed in the disabled
handlers. The event will only be processed if its severity is greater than or equal to the configured
severity level of the EventEmitter
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
The event message to be emitted. |
required |
event_level |
EventLevel
|
The severity level of the event. Defaults to EventLevel.DEBUG. |
DEBUG
|
event_type |
EventType
|
The type of event (e.g., status, response). Defaults to EventType.STATUS. |
STATUS
|
disabled_handlers |
list[str]
|
The list of handler names to be disabled. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
If the provided |
ValueError
|
If the provided |
Messenger(message, is_template=True)
Bases: Component
Emits a custom event message with possible access to the state variables.
This component acts as an intermediary step, designed to be placed between other pipeline steps. It allows for event messaging operations to be performed outside individual components but still within the context of the pipeline execution.
Attributes:
Name | Type | Description |
---|---|---|
message |
str
|
The message to be sent, may contain placeholders enclosed in curly braces |
is_template |
bool
|
Whether the message is a template that can be injected with state variables. Defaults to True. |
variable_keys |
list[str]
|
The keys of the message that can be injected with state variables.
Only used if |
Plain string message example:
event_emitter = EventEmitter(handlers=[ConsoleEventHandler()])
kwargs = {"event_emitter": event_emitter}
messenger = Messenger("Executing component.", is_template=False)
await messenger.run(**kwargs)
Template message example:
event_emitter = EventEmitter(handlers=[ConsoleEventHandler()])
state_variables = {"query": "Hi!", "top_k": 10}
kwargs = {"event_emitter": event_emitter, "state_variables": state_variables}
messenger = Messenger("Executing component for query {query} and top k {top_k}.")
await messenger.run(**kwargs)
Initializes a new instance of the Messenger class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str
|
The message to be sent, may contain placeholders enclosed in curly braces |
required |
is_template |
bool
|
Whether the message is a template that can be injected with state variables. Defaults to True. |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
If the keys of the message does not match the provided keys. |
send_message(event_emitter, state_variables=None, emit_kwargs=None)
async
Emits the message to the event emitter.
This method validates the variables, formats the message if required, and then emits the message using the event emitter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_emitter |
EventEmitter
|
The event emitter instance to emit the message. |
required |
state_variables |
dict[str, Any] | None
|
The state variables to be injected into the message
placeholders. Can only be provided if |
None
|
emit_kwargs |
dict[str, Any] | None
|
The keyword arguments to be passed to the event emitter's emit method. Defaults to None. |
None
|