Skip to content

Messenger

Defines a component used for custom event messaging in Gen AI applications pipelines.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id)

References

NONE

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 is_template is set to True.

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 is_template is set to True. Defaults to None.

None
emit_kwargs dict[str, Any] | None

The keyword arguments to be passed to the event emitter's emit method. Defaults to None.

None