Context#

Context is a data structure that is used to store information about the current state of a conversation.

It is used to keep track of the user’s input, the current stage of the conversation, and any other information that is relevant to the current context of a dialog.

The Context data structure provides several key features to make working with data easier. Developers can use the context to store any information that is relevant to the current conversation, such as user data, session data, conversation history, e.t.c. This allows developers to easily access and use this data throughout the conversation flow.

Another important feature of the context is data serialization. The context can be easily serialized to a format that can be stored or transmitted, such as JSON. This allows developers to save the context data and resume the conversation later.

get_last_index(dictionary)[source]#

Obtain the last index from the dictionary.

Parameters:

dictionary (dict) – Dictionary with unsorted keys.

Return type:

int

Returns:

Last index from the dictionary.

Raises:

ValueError – If the dictionary is empty.

exception ContextError[source]#

Bases: Exception

Raised when context methods are not used correctly.

class ServiceState(**data)[source]#

Bases: BaseModel

execution_status: ComponentExecutionState#

ComponentExecutionState of this pipeline service. Cleared at the end of every turn.

finished_event: asyncio.Event#

Asyncio Event which can be awaited until this service finishes. Cleared at the end of every turn.

class FrameworkData(**data)[source]#

Bases: BaseModel

Framework uses this to store data related to any of its modules.

service_states: Dict[str, ServiceState]#

Dictionary containing ServiceState of all the pipeline components. Cleared at the end of every turn.

current_node: Optional[Node]#

A copy of the current node provided by get_inherited_node(). This node can be safely modified by Processing functions to alter current node fields.

pipeline: Optional[Pipeline]#

Instance of the pipeline that manages this context. Can be used to obtain run configuration such as script or fallback label.

stats: Dict[str, Any]#

Enables complex stats collection across multiple turns.

slot_manager: SlotManager#

Stores extracted slots.

class Context(**data)[source]#

Bases: BaseModel

A structure that is used to store data about the context of a dialog.

id: Union[UUID, int, str]#

id is the unique context identifier. By default, randomly generated using uuid4. id can be used to trace the user behavior, e.g while collecting the statistical data.

labels: Dict[int, AbsoluteNodeLabel]#

labels stores the history of labels for all passed nodes.

  • key - id of the turn.

  • value - label of this turn.

Start label is stored at key 0. IDs go up by 1 after that.

requests: Dict[int, Message]#

requests stores the history of all requests received by the pipeline.

  • key - id of the turn.

  • value - request of this turn.

First request is stored at key 1. IDs go up by 1 after that.

responses: Dict[int, Message]#

responses stores the history of all responses produced by the pipeline.

  • key - id of the turn.

  • value - response of this turn.

First response is stored at key 1. IDs go up by 1 after that.

misc: Dict[str, Any]#

misc stores any custom data. The framework doesn’t use this dictionary, so storage of any data won’t reflect on the work of the internal Chatsky functions.

  • key - Arbitrary data name.

  • value - Arbitrary data.

framework_data: FrameworkData#

This attribute is used for storing custom data required for pipeline execution. It is meant to be used by the framework only. Accessing it may result in pipeline breakage.

classmethod init(start_label, id=None)[source]#

Initialize new context from start_label and, optionally, context id.

add_request(request)[source]#

Add a new request to the context.

add_response(response)[source]#

Add a new response to the context.

add_label(label)[source]#

Add a new AbsoluteNodeLabel to the context.

Raises:

ContextError – If labels is empty.

property last_label: AbsoluteNodeLabel#

Return the last AbsoluteNodeLabel of the Context.

Raises:

ContextError – If labels is empty.

property last_response: Message | None#

Return the last response of the current Context. Return None if no responses have been added yet.

property last_request: Message#

Return the last request of the current Context.

Raises:

ContextError – If responses is empty.

property pipeline: Pipeline#

Return FrameworkData.pipeline.

property current_node: Node#

Return FrameworkData.current_node.