Context#
A 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 provides a convenient interface for working with data, allowing developers to easily add, retrieve, and manipulate data as the conversation progresses.
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, or etc. 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. Return -1 if the dict is empty.
- Parameters:
dictionary (
dict
) – Dictionary with unsorted keys.- Return type:
int
- Returns:
Last index from the dictionary.
- class Context(**data)[source]#
Bases:
BaseModel
A structure that is used to store data about the context of a dialog.
Avoid storing unserializable data in the fields of this class in order for context storages to work.
- id: Union[UUID, int, str]#
id is the unique context identifier. By default, randomly generated using uuid4 id is used. id can be used to trace the user behavior, e.g while collecting the statistical data.
- labels: Dict[int, NodeLabel2Type]#
labels stores the history of all passed labels
key - id of the turn.
value - label on this turn.
- requests: Dict[int, Message]#
requests stores the history of all requests received by the agent
key - id of the turn.
value - request on this turn.
- responses: Dict[int, Message]#
responses stores the history of all agent responses
key - id of the turn.
value - response on this turn.
- misc: Dict[str, Any]#
misc stores any custom data. The scripting doesn’t use this dictionary by default, so storage of any data won’t reflect on the work on the internal Dialog Flow Scripting functions.
Avoid storing unserializable data in order for context storages to work.
key - Arbitrary data name.
value - Arbitrary data.
- validation: bool#
validation is a flag that signals that
Pipeline
, while being initialized, checks theScript
. The functions that can give not valid data while being validated must use this flag to take the validation mode into account. Otherwise the validation will not be passed.
- framework_states: Dict[ModuleName, Dict[str, Any]]#
framework_states is used for addons states or for
Pipeline
’s states.Pipeline
records all its intermediate conditions into the framework_states. AfterContext
processing is finished,Pipeline
resets framework_states and returnsContext
.key - Temporary variable name.
value - Temporary variable data.
- classmethod sort_dict_keys(dictionary)[source]#
Sort the keys in the dictionary. This needs to be done after deserialization, since the keys are deserialized in a random order.
- Parameters:
dictionary (
dict
) – Dictionary with unsorted keys.- Return type:
dict
- Returns:
Dictionary with sorted keys.
- classmethod cast(ctx=None, *args, **kwargs)[source]#
Transform different data types to the objects of the
Context
class. Return an object of theContext
type that is initialized by the input data.
- add_request(request)[source]#
Add a new request to the context. The new request is added with the index of last_index + 1.
- Parameters:
request (
Message
) – request to be added to the context.
- add_response(response)[source]#
Add a new response to the context. The new response is added with the index of last_index + 1.
- Parameters:
response (
Message
) – response to be added to the context.
- model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- add_label(label)[source]#
Add a new
NodeLabel2Type
to the context. The new label is added with the index of last_index + 1.- Parameters:
label (
Tuple
[str
,str
]) – label that we need to add to the context.
- clear(hold_last_n_indices, field_names={'labels', 'requests', 'responses'})[source]#
Delete all records from the requests/responses/labels except for the last hold_last_n_indices turns. If field_names contains misc field, misc field is fully cleared.
- Parameters:
hold_last_n_indices (
int
) – Number of last turns to keep.field_names (
Union
[Set
[str
],List
[str
]]) – Properties ofContext
to clear. Defaults to {“requests”, “responses”, “labels”}
- property last_label: Tuple[str, str] | None#
Return the last
NodeLabel2Type
of theContext
. Return None if labels is empty.Since start_label is not added to the labels field, empty labels usually indicates that the current node is the start_node.
- property last_response: Message | None#
Return the last response of the current
Context
. Return None if responses is empty.