Skip to content

Commit 7f8e3e4

Browse files
authored
Merge pull request RasaHQ#4870 from RasaHQ/tracker-sessions
Tracker sessions
2 parents b052678 + 5df314e commit 7f8e3e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1510
-234
lines changed

changelog/4830.feature.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Added conversation sessions to trackers.
2+
3+
A conversation session represents the dialog between the assistant and a user.
4+
Conversation sessions can begin in three ways: 1. the user begins the conversation
5+
with the assistant, 2. the user sends their first message after a configurable period
6+
of inactivity, or 3. a manual session start is triggered with the ``/session_start``
7+
intent message. The period of inactivity after which a new conversation session is
8+
triggered is defined in the domain using the ``session_expiration_time`` key in the
9+
``session_config`` section. The introduction of conversation sessions comprises the
10+
following changes:
11+
12+
- Added a new event ``SessionStarted`` that marks the beginning of a new conversation
13+
session.
14+
- Added a new default action ``ActionSessionStart``. This action takes all
15+
``SlotSet`` events from the previous session and applies it to the next session.
16+
- Added a new default intent ``session_start`` which triggers the start of a new
17+
conversation session.
18+
- ``SQLTrackerStore`` and ``MongoTrackerStore`` only retrieve
19+
events from the last session from the database.
20+
21+
22+
.. note::
23+
24+
The session behaviour is disabled for existing projects, i.e. existing domains
25+
without session config section.

data/test_domains/duplicate_intents.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ actions:
2626
- utter_default
2727
- utter_greet
2828
- utter_goodbye
29+
30+
session_config:
31+
session_expiration_time: 60
32+
carry_over_slots_to_new_session: true

docs/api/events.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,25 @@ Log an executed action
271271
.. literalinclude:: ../../rasa/core/events/__init__.py
272272
:dedent: 4
273273
:pyobject: ActionExecuted.apply_to
274+
275+
Start a new conversation session
276+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277+
278+
:Short: Marks the beginning of a new conversation session. Resets the tracker and
279+
triggers an ``ActionSessionStart`` which by default applies the existing
280+
``SlotSet`` events to the new session.
281+
282+
:JSON:
283+
.. literalinclude:: ../../tests/core/test_events.py
284+
:start-after: # DOCS MARKER SessionStarted
285+
:dedent: 4
286+
:end-before: # DOCS END
287+
:Class:
288+
.. autoclass:: rasa.core.events.SessionStarted
289+
290+
:Effect:
291+
When added to a tracker, this is the code used to update the tracker:
292+
293+
.. literalinclude:: ../../rasa/core/events/__init__.py
294+
:dedent: 4
295+
:pyobject: SessionStarted.apply_to

docs/api/rasa-sdk.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,67 @@ Details of the ``dispatcher.utter_message()`` method:
101101

102102
.. automethod:: rasa_sdk.executor.CollectingDispatcher.utter_message
103103

104+
105+
.. _custom_session_start:
106+
107+
Customising the session start action
108+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
109+
110+
The default behaviour of the session start action is to take all existing slots and to
111+
carry them over into the next session. Let's say you do not want to carry over all
112+
slots, but only a user's name and their phone number. To do that, you'd override the
113+
``action_session_start`` with a custom action that might look like this:
114+
115+
.. testcode::
116+
117+
from typing import Text, List, Dict, Any
118+
119+
from rasa_sdk import Action, Tracker
120+
from rasa_sdk.events import SlotSet, SessionStarted, ActionExecuted, EventType
121+
from rasa_sdk.executor import CollectingDispatcher
122+
123+
124+
class ActionSessionStart(Action):
125+
def name(self) -> Text:
126+
return "action_session_start"
127+
128+
@staticmethod
129+
def fetch_slots(tracker: Tracker) -> List[EventType]:
130+
"""Collect slots that contain the user's name and phone number."""
131+
132+
slots = []
133+
134+
for key in ("name", "phone_number"):
135+
value = tracker.get_slot(key)
136+
if value is not None:
137+
slots.append(SlotSet(key=key, value=value))
138+
139+
return slots
140+
141+
async def run(
142+
self,
143+
dispatcher: CollectingDispatcher,
144+
tracker: Tracker,
145+
domain: Dict[Text, Any],
146+
) -> List[EventType]:
147+
148+
# the session should begin with a `session_started` event
149+
events = [SessionStarted()]
150+
151+
# any slots that should be carried over should come after the
152+
# `session_started` event
153+
events.extend(self.fetch_slots(tracker))
154+
155+
# an `action_listen` should be added at the end as a user message follows
156+
events.append(ActionExecuted("action_listen"))
157+
158+
return events
159+
160+
.. note::
161+
162+
You need to explicitly add ``action_session_start`` to your domain to override this
163+
custom action.
164+
104165
Events
105166
------
106167

docs/api/tracker.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:desc: Trackers mantain the state of the a dialogue and can be
1+
:desc: Trackers maintain the state of the a dialogue and can be
22
featurized for machine learning algorithms right out of
33
the box.
44

@@ -7,6 +7,12 @@
77
Tracker
88
=======
99

10+
.. edit-link::
11+
12+
Trackers maintain the state of a dialogue between the assistant and the user in the form
13+
of conversation sessions. To learn more about how to configure the session behaviour,
14+
check out the docs on :ref:`session_config`.
15+
1016
.. edit-link::
1117
:url: https://github.com/RasaHQ/rasa/edit/master/rasa/core/trackers.py
1218
:text: SUGGEST DOCSTRING EDITS

docs/core/actions.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ using the :ref:`callbackInput` channel to send messages to a webhook.
137137
Default Actions
138138
---------------
139139

140-
There are eight default actions:
140+
The available default actions are:
141141

142142
+-----------------------------------+------------------------------------------------+
143143
| ``action_listen`` | Stop predicting more actions and wait for user |
@@ -148,6 +148,17 @@ There are eight default actions:
148148
| | if the :ref:`mapping-policy` is included in |
149149
| | the policy configuration. |
150150
+-----------------------------------+------------------------------------------------+
151+
| ``action_session_start`` | Start a new conversation session. Take all set |
152+
| | slots, mark the beginning of a new conversation|
153+
| | session and re-apply the existing ``SlotSet`` |
154+
| | events. This action is triggered automatically |
155+
| | after an inactivity period defined by the |
156+
| | ``session_expiration_time`` parameter in the |
157+
| | domain's :ref:`session_config`. Can be |
158+
| | triggered manually during a conversation by |
159+
| | entering ``/session_start``. All conversations |
160+
| | begin with an ``action_session_start``. |
161+
+-----------------------------------+------------------------------------------------+
151162
| ``action_default_fallback`` | Undo the last user message (as if the user did |
152163
| | not send it and the bot did not react) and |
153164
| | utter a message that the bot did not |
@@ -175,7 +186,7 @@ There are eight default actions:
175186
| | included in the policy configuration. |
176187
+-----------------------------------+------------------------------------------------+
177188

178-
All the default actions can be overwritten. To do so, add the action name
189+
All the default actions can be overridden. To do so, add the action name
179190
to the list of actions in your domain:
180191

181192
.. code-block:: yaml

docs/core/domains.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,44 @@ featurized as normal.
316316

317317
If you really want these entities not to influence action prediction we
318318
suggest you make the slots with the same name of type ``unfeaturized``.
319+
320+
.. _session_config:
321+
322+
Session configuration
323+
---------------------
324+
325+
A conversation session represents the dialogue between the assistant and the user.
326+
Conversation sessions can begin in three ways:
327+
328+
1. the user begins the conversation with the assistant,
329+
2. the user sends their first message after a configurable period of inactivity, or
330+
3. a manual session start is triggered with the ``/session_start`` intent message.
331+
332+
You can define the period of inactivity after which a new conversation
333+
session is triggered in the domain under the ``session_config`` key.
334+
``session_expiration_time`` defines the time of inactivity in minutes after which a
335+
new session will begin. ``carry_over_slots_to_new_session`` determines whether
336+
existing set slots should be carried over to new sessions.
337+
338+
The default session configuration looks as follows:
339+
340+
.. code-block:: yaml
341+
342+
session_config:
343+
session_expiration_time: 60 # value in minutes, 0 means infinitely long
344+
carry_over_slots_to_new_session: true # set to false to forget slots between sessions
345+
346+
This means that if a user sends their first message after 60 minutes of inactivity, a
347+
new conversation session is triggered, and that any existing slots are carried over
348+
into the new session. Setting the value of ``session_expiration_time`` to 0 means
349+
that sessions will not end (note that the ``action_session_start`` action will still
350+
be triggered at the very beginning of conversations).
351+
352+
.. note::
353+
354+
A session start triggers the default action ``action_session_start``. Its default
355+
implementation moves all existing slots into the new session. Note that all
356+
conversations begin with an ``action_session_start``. Overriding this action could
357+
for instance be used to initialise the tracker with slots from an external API
358+
call, or to start the conversation with a bot message. The docs on
359+
:ref:`custom_session_start` shows you how to do that.

rasa/cli/initial_project/domain.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ templates:
3434

3535
utter_iamabot:
3636
- text: "I am a bot, powered by Rasa."
37+
38+
session_config:
39+
session_expiration_time: 60
40+
carry_over_slots_to_new_session: true

rasa/cli/x.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
import asyncio
33
import importlib.util
44
import logging
5-
import warnings
65
import os
76
import signal
87
import traceback
98
from multiprocessing import get_context
10-
from typing import List, Text, Optional, Tuple, Union, Iterable
9+
from typing import List, Text, Optional, Tuple, Iterable
1110

1211
import aiohttp
1312
import ruamel.yaml as yaml
@@ -328,7 +327,7 @@ def rasa_x(args: argparse.Namespace):
328327
async def _pull_runtime_config_from_server(
329328
config_endpoint: Optional[Text],
330329
attempts: int = 60,
331-
wait_time_between_pulls: Union[int, float] = 5,
330+
wait_time_between_pulls: float = 5,
332331
keys: Iterable[Text] = ("endpoints", "credentials"),
333332
) -> Optional[List[Text]]:
334333
"""Pull runtime config from `config_endpoint`.

rasa/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@
4646
DEFAULT_SANIC_WORKERS = 1
4747
ENV_SANIC_WORKERS = "SANIC_WORKERS"
4848
ENV_SANIC_BACKLOG = "SANIC_BACKLOG"
49+
50+
DEFAULT_SESSION_EXPIRATION_TIME_IN_MINUTES = 60
51+
DEFAULT_CARRY_OVER_SLOTS_TO_NEW_SESSION = True

0 commit comments

Comments
 (0)