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:
ExceptionRaised when context methods are not used correctly.
- class ServiceState(**data)[source]#
Bases:
BaseModel- execution_status: ComponentExecutionState#
ComponentExecutionStateof 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:
BaseModelFramework uses this to store data related to any of its modules.
- service_states: Dict[str, ServiceState]#
Dictionary containing
ServiceStateof 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:
BaseModelA structure that is used to store data about the context of a dialog.
- id: Union[UUID, int, str]#
idis the unique context identifier. By default, randomly generated usinguuid4.idcan be used to trace the user behavior, e.g while collecting the statistical data.
- labels: Dict[int, AbsoluteNodeLabel]#
labelsstores the history of labels for all passed nodes.key -
idof the turn.value -
labelof this turn.
Start label is stored at key
0. IDs go up by1after that.
- requests: Dict[int, Message]#
requestsstores the history of all requests received by the pipeline.key -
idof the turn.value -
requestof this turn.
First request is stored at key
1. IDs go up by1after that.
- responses: Dict[int, Message]#
responsesstores the history of all responses produced by the pipeline.key -
idof the turn.value -
responseof this turn.
First response is stored at key
1. IDs go up by1after that.
- misc: Dict[str, Any]#
miscstores 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_labeland, optionally, contextid.
- add_label(label)[source]#
Add a new
AbsoluteNodeLabelto the context.- Raises:
ContextError – If
labelsis empty.
- property last_label: AbsoluteNodeLabel#
Return the last
AbsoluteNodeLabelof theContext.- Raises:
ContextError – If
labelsis empty.
- property last_response: Message | None#
Return the last response of the current
Context. ReturnNoneif no responses have been added yet.
- property last_request: Message#
Return the last request of the current
Context.- Raises:
ContextError – If
responsesis empty.
- property pipeline: Pipeline#
Return
FrameworkData.pipeline.
- property current_node: Node#
Return
FrameworkData.current_node.