Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let external events and reminders trigger intents #4964

Merged
merged 153 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 149 commits
Commits
Show all changes
153 commits
Select commit Hold shift + click to select a range
f15b20f
Draft Intent-triggering Reminder
Nov 5, 2019
0db31c9
Fix intent trigger
Nov 7, 2019
baac807
Hide external event message
Nov 8, 2019
ed52771
Fix test_reminder_scheduled
Nov 27, 2019
e35b90e
Fix test_reminder_cancelled
Nov 27, 2019
0e68baa
Merge branch 'master' into johannes-4464
Nov 27, 2019
04f7759
Merge branch 'master' into johannes-4464
Nov 27, 2019
18debed
Merge branch '1.5.x' into johannes-4464
Nov 28, 2019
3671722
Introduce IS_EXTERNAL and update reminder code
Dec 12, 2019
9a59ec7
Reminders can no longer trigger actions
Dec 12, 2019
dc34680
Implement external triggering of intents
Dec 12, 2019
8c3a750
Ensure that injected intent exists in the domain
Dec 12, 2019
7839f2a
Add API specs
Dec 12, 2019
a14cce7
Merge branch 'master' into johannes-4464b
Dec 12, 2019
fe21396
Update reminder docs
Dec 12, 2019
7203378
Update reminder tests
Dec 12, 2019
5b22353
Run BLACK
Dec 12, 2019
8ffedd1
Fix test_list_routes
Dec 13, 2019
06d0625
Apply BLACK
Dec 13, 2019
adb1009
Merge branch 'master' into johannes-4464b
Dec 13, 2019
4f40667
Add deprecated flag to operation object
Jan 2, 2020
c9c24c6
Remove superfluous new-lines
Jan 2, 2020
ac0598e
Add type for entities
Jan 2, 2020
1654092
Remove superfluous None argument
Jan 2, 2020
65061bd
Merge branch 'johannes-4464b' of github.com:RasaHQ/rasa into johannes…
Jan 2, 2020
df9571d
Remove superfluous None argument
Jan 2, 2020
920a5ed
Declare missing output type
Jan 2, 2020
3297a16
Declare missing output type
Jan 2, 2020
96fa392
Make OutputChannel non-optional
Jan 2, 2020
03348c7
Merge branch 'johannes-4464b' of github.com:RasaHQ/rasa into johannes…
Jan 2, 2020
a5586e0
Convert to f-string
Jan 2, 2020
b0d5804
Avoid using None for entities
Jan 2, 2020
ac035c0
Optimize imports
Jan 2, 2020
d9ec5b7
Show full endpoint spec in warning
Jan 2, 2020
3377a85
Raise the value error as-is
Jan 2, 2020
254f765
Add doc string for inject_external_user_uttered
Jan 2, 2020
da0b6a0
Relabel inject -> trigger intent
Jan 2, 2020
c1faab3
Relabel inject -> trigger intent
Jan 2, 2020
20c3d71
Relabel inject -> trigger intent
Jan 2, 2020
2424574
Merge branch 'johannes-4464b' of github.com:RasaHQ/rasa into johannes…
Jan 2, 2020
903d2a5
Import HTTPResponse
Jan 2, 2020
2cd2b07
Rename inject -> trigger intent
Jan 2, 2020
fb19ed9
Add quote marks
Jan 2, 2020
a1a0308
Clarify description
Jan 2, 2020
f57587e
Add entity parameters to Reminder
Jan 2, 2020
93fcdfb
Pass entities from reminder
Jan 2, 2020
7b06b96
Allow for a short-hand notation for entity specification
Jan 2, 2020
0cbcc58
Add type specs for UserUttered.__init__
Jan 2, 2020
60a8b9e
Fix type specification
Jan 2, 2020
0243b90
Rename endpoint inject-intent -> trigger_intent
Jan 2, 2020
d213477
Handle entities=None case
Jan 2, 2020
5a5ea27
Format files with BLACK
Jan 2, 2020
3e243a3
Fix type in trigger_intent
Jan 2, 2020
1ddfa7a
Fix test_list_routes
Jan 2, 2020
567081d
Fix bug caught by test_trigger_intent
Jan 3, 2020
5b4db5f
Fix test_trigger_intent_with_missing_action_name
Jan 3, 2020
c19cf82
Fix test_trigger_intent_with_not_existing_intent
Jan 3, 2020
8140067
Make `entities` in ReminderScheduled optional
Jan 3, 2020
335c833
Merge branch 'master' into johannes-4464b
Jan 3, 2020
4833b4c
Optimize imports
Jan 3, 2020
f06dc43
Fix missing async io use
Jan 3, 2020
ad9eeff
Update doc on proactively reaching out to user
Jan 3, 2020
f9570be
Fix doc string of _cancel_reminders
Jan 3, 2020
7b5caea
Fix documemntation of proactively reaching out to user
Jan 3, 2020
ee2f1c7
Use nullable flag for optional args in ActionRequest
Jan 3, 2020
8395c3f
Use nullable flag for optional args in IntentTriggerRequest
Jan 3, 2020
678c2d4
Clarify doc string of ReminderScheduled
Jan 3, 2020
4e0b16e
Clarify doc string of trigger_external_user_uttered
Jan 3, 2020
d77b33d
Introduce UserUttered.create_external
Jan 3, 2020
b582d53
Add quotes to warning
Jan 3, 2020
f2423d1
Move 'reaching out to user' section to responses.rst
Jan 3, 2020
ddca1a4
Improve 'Schedule a reminder' doc
Jan 3, 2020
66fc590
Clarify entity parameter description
Jan 10, 2020
97a44bd
Merge branch 'master' into johannes-4464b
Jan 13, 2020
e6e5f4c
Dirty bugfix for sanic-plugins-framework dependency
Jan 14, 2020
ca19ff9
Explain `latest` output channel
Jan 14, 2020
887aa52
Upgrade ReminderCancelled
Jan 14, 2020
dcdf1f4
Add another test for ReminderCancelled
Jan 14, 2020
41cea74
Simplify code in test routine
Jan 14, 2020
f3aa034
Merge branch 'master' into johannes-4464b
Jan 15, 2020
1329916
Update docs/api/events.rst
Jan 16, 2020
444cbcb
Update rasa/core/events/__init__.py
Jan 16, 2020
401fc71
Apply BLACK formatting
Jan 16, 2020
de0d0fa
Make reminder test names more descriptive
Jan 16, 2020
c46cf2e
Remove test description
Jan 16, 2020
ea8df2c
Split test cases
Jan 16, 2020
b080533
Declare types for create_external
Jan 16, 2020
5db5571
Rename job_name and cancels_job_with_name
Jan 16, 2020
7a17857
Handle None entities
Jan 16, 2020
615dcde
Insert comment again
Jan 16, 2020
929febf
Use Google doc string and `or`
Jan 16, 2020
dee18d2
Let `Agent.trigger_intent` return `None` instead of tracker
Jan 16, 2020
76746a2
Raise response status 404 for invalid intents
Jan 16, 2020
7569942
Add docstring and comments to `cancels_job_with_name`
Jan 16, 2020
2c0057a
Apply BLACK formatting
Jan 16, 2020
80aa19a
Merge branch 'master' into johannes-4464b
Jan 16, 2020
8392812
Simplify code with `or`
Jan 16, 2020
85d025c
Declare type
Jan 16, 2020
3f2c164
Rename ReminderScheduled.scheduled_job_name
Jan 16, 2020
cc92b95
Rename ReminderScheduled._properties
Jan 16, 2020
c1fde78
Avoid string-casting the intent string
Jan 16, 2020
f7ee435
Add `Optional` to `entities` parameter
Jan 16, 2020
b78911c
Ensure that special symbols in `sender_id` cannot mess up regex match
Jan 17, 2020
7e64b08
Use `hash` to make job names robust for regex
Jan 17, 2020
2666d9a
Add tests for `ReminderCancelled.cancels_job_with_name`
Jan 17, 2020
8adbd16
Apply BLACK formatting
Jan 17, 2020
c5600a0
Merge branch 'master' into johannes-4464b
Jan 17, 2020
7f0a715
Use loop instead of `sleep(5)`
Jan 17, 2020
3602cf0
Add space
Jan 17, 2020
fcf733b
Use rf-strings
Jan 17, 2020
f3a1dc7
Merge branch 'johannes-4464b' of github.com:RasaHQ/rasa into johannes…
Jan 17, 2020
0b3fbc3
Declare return type
Jan 17, 2020
f541b2d
Kill jobs on timeout
Jan 17, 2020
a3ddf5e
Merge branch 'johannes-4464b' of github.com:RasaHQ/rasa into johannes…
Jan 17, 2020
d6b4a7f
Add changelog news fragments
Jan 17, 2020
1e55e57
Merge branch 'master' into johannes-4464b
Jan 17, 2020
f2b80bd
Add 'and entities' to ReminderScheduled explanation
Jan 17, 2020
71e31b4
Fix `ReminderCancelled.__hash__`
Jan 17, 2020
c210584
Add documentation of `ReminderCancelled`
Jan 17, 2020
66d00fe
Merge branch 'master' into johannes-4464b
Jan 17, 2020
db16577
Update docs/core/responses.rst
Jan 20, 2020
bdae15d
Update rasa/core/constants.py
Jan 20, 2020
212fa2a
Update rasa/core/events/__init__.py
Jan 20, 2020
dbd3a23
Update rasa/core/events/__init__.py
Jan 20, 2020
4ea8dc4
Rename wait_until_all_jobs_were_executed
Jan 20, 2020
60c3979
Fix asking `not timeout_after_seconds` first
Jan 20, 2020
c3ca247
Rename `test_trigger_intent_with_missing_intent_name`
Jan 20, 2020
78fa73c
Shorten doc-string of ReminderCancelled.__init__
Jan 20, 2020
bde7839
Update rasa/core/events/__init__.py
Jan 20, 2020
59dc584
Update rasa/core/events/__init__.py
Jan 20, 2020
8bb8667
Update rasa/core/processor.py
Jan 20, 2020
6c34ebd
Move description of new `ReminderCancelled` abilities to `improvement`
Jan 20, 2020
8e93e5c
Merge branch 'johannes-4464b' of github.com:RasaHQ/rasa into johannes…
Jan 20, 2020
e283f76
Fix missing refactoring
Jan 20, 2020
795217f
Add `ReminderCancelled.matches_*_hash` functions
Jan 20, 2020
c66848f
Remove redundant line
Jan 20, 2020
813c23d
Apply BLACK formatting
Jan 20, 2020
4fd03ed
Update docs/core/responses.rst
Jan 21, 2020
5886dd2
Make `ReminderCancelled._matches_*_hash` private
Jan 21, 2020
72c7458
Remove redundant str-cast
Jan 21, 2020
4210a8e
Update rasa/core/processor.py
Jan 21, 2020
e526d2d
Declare type of input
Jan 21, 2020
ce800c7
Merge branch 'johannes-4464b' of github.com:RasaHQ/rasa into johannes…
Jan 21, 2020
b926426
Define `tracker_with_scheduled_reminder`
Jan 21, 2020
4d57468
Remove outdated ToDo
Jan 21, 2020
164616a
Apply BLACK formatting
Jan 21, 2020
3350155
Remove redundant `+`
Jan 21, 2020
cb93ab0
Declare types for `cancel_reminder_and_check`
Jan 21, 2020
42e2928
Use fixture `tracker_with_six_scheduled_reminders`
Jan 21, 2020
1b3b1dc
Declare type of input
Jan 21, 2020
eeeb8f3
Merge branch 'master' into johannes-4464b
Jan 21, 2020
f70bf0e
Merge branch 'master' into johannes-4464b
Jan 21, 2020
ac9dadb
Merge branch 'master' into johannes-4464b
Jan 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/4964.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
External events and reminders now trigger intents (and entities) instead of actions.

Add new endpoint ``/conversations/<conversation_id>/trigger_intent``, which lets the user specify an intent and a
list of entities that is injected into the conversation in place of a user message. The bot then predicts and
executes a response action.
2 changes: 2 additions & 0 deletions changelog/4964.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``ReminderCancelled`` can now cancel multiple reminders if no name is given. It still cancels a single
reminder if the reminder's name is specified.
2 changes: 2 additions & 0 deletions changelog/4964.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The endpoint ``/conversations/<conversation_id>/execute`` is now deprecated. Instead, users should use
the ``/conversations/<conversation_id>/trigger_intent`` endpoint and thus trigger intents instead of actions.
80 changes: 72 additions & 8 deletions docs/_static/spec/rasa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,9 @@ paths:
tags:
- Tracker
summary: Run an action in a conversation
deprecated: true
description: >-
Runs the action, calling the action server if necessary.
DEPRECATED. Runs the action, calling the action server if necessary.
JEM-Mosig marked this conversation as resolved.
Show resolved Hide resolved
Any responses sent by the executed action will be forwarded
to the channel specified in the output_channel parameter.
If no output channel is specified, any messages that should be
Expand Down Expand Up @@ -296,6 +297,57 @@ paths:
500:
$ref: '#/components/responses/500ServerError'

/conversations/{conversation_id}/trigger_intent:
post:
security:
- TokenAuth: []
- JWT: []
operationId: triggerConversationIntent
tags:
- Tracker
summary: Inject an intent into a conversation
description: >-
Sends a specified intent and list of entities in place of a
user message. The bot then predicts and executes a response action.
Any responses sent by the executed action will be forwarded
to the channel specified in the ``output_channel`` parameter.
If no output channel is specified, any messages that should be
sent to the user will be included in the response of this endpoint.
parameters:
- $ref: '#/components/parameters/conversation_id'
- $ref: '#/components/parameters/include_events'
erohmensing marked this conversation as resolved.
Show resolved Hide resolved
- $ref: '#/components/parameters/output_channel'
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/IntentTriggerRequest'
responses:
200:
description: Success
content:
application/json:
schema:
type: object
properties:
tracker:
$ref: '#/components/schemas/Tracker'
messages:
type: array
items:
$ref: '#/components/schemas/BotMessage'
400:
$ref: '#/components/responses/400BadRequest'
401:
$ref: '#/components/responses/401NotAuthenticated'
403:
$ref: '#/components/responses/403NotAuthorized'
409:
$ref: '#/components/responses/409Conflict'
500:
$ref: '#/components/responses/500ServerError'

/conversations/{conversation_id}/predict:
post:
security:
Expand Down Expand Up @@ -905,21 +957,34 @@ components:
type: object
properties:
name:
description: >-
Name of the action to be executed.
description: Name of the action to be executed.
type: string
example: utter_greet
policy:
description: >-
Name of the policy that predicted the action (optional).
description: Name of the policy that predicted the action.
type: string
nullable: true
confidence:
description: >-
Confidence of the prediction (optional).
description: Confidence of the prediction.
type: number
nullable: true
example: 0.987232
required: ["name"]

IntentTriggerRequest:
type: object
properties:
name:
description: Name of the intent to be executed.
type: string
example: greet
entities:
description: Entities to be passed on.
type: object
nullable: true
example: {"temperature": "high"}
required: ["name"]

Message:
type: object
properties:
Expand Down Expand Up @@ -1592,4 +1657,3 @@ components:
properties:
use_entities:
type: boolean

31 changes: 28 additions & 3 deletions docs/api/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Reset all Slots
Schedule a reminder
~~~~~~~~~~~~~~~~~~~

:Short: Schedule an action to be executed in the future.
:Short: Schedule an intent to be triggered in the future.
:JSON:
.. literalinclude:: ../../tests/core/test_events.py
:lines: 1-
Expand All @@ -99,8 +99,33 @@ Schedule a reminder
.. autoclass:: rasa.core.events.ReminderScheduled

:Effect:
When added to a tracker, core will schedule the action to be
run in the future.
When added to a tracker, Rasa Core will schedule the intent (and entities) to be
triggered in the future, in place of a user input. You can link
this intent to an action of your choice using the :ref:`mapping-policy`.


Cancel a reminder
~~~~~~~~~~~~~~~~~~~

:Short: Cancel one or more reminders.
:JSON:
.. literalinclude:: ../../tests/core/test_events.py
:lines: 1-
:start-after: # DOCS MARKER ReminderCancelled
:dedent: 4
:end-before: # DOCS END
:Class:
.. autoclass:: rasa.core.events.ReminderCancelled

:Effect:
When added to a tracker, Rasa Core will cancel any outstanding reminders that
match the ``ReminderCancelled`` event. For example,

- ``ReminderCancelled(intent="greet")`` cancels all reminders with intent ``greet``
- ``ReminderCancelled(entities={...})`` cancels all reminders with the given entities
- ``ReminderCancelled("...")`` cancels the one unique reminder with the given name
- ``ReminderCancelled()`` cancels all reminders


Pause a conversation
~~~~~~~~~~~~~~~~~~~~
Expand Down
30 changes: 0 additions & 30 deletions docs/core/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,6 @@ e.g. by setting slots and send responses back to the user.
All of the modifications are done using events.
There is a list of all possible event types in :ref:`events`.

Proactively Reaching Out to the User Using Actions
--------------------------------------------------

You may want to proactively reach out to the user,
for example to display the output of a long running background operation
or notify the user of an external event.

To do so, you can ``POST`` to this
`endpoint <../../api/http-api/#operation/executeConversationAction>`_ ,
specifying the action which should be run for a specific user in the request body. Use the
``output_channel`` query parameter to specify which output
channel should be used to communicate the assistant's responses back to the user.
If your message is static, you can define an ``utter_`` action in your domain file with
a corresponding template. If you need more control, add a custom action in your
domain and implement the required steps in your action server. Any messages which are
dispatched in the custom action will be forwarded to the specified output channel.


Proactively reaching out to the user is dependent on the abilities of a channel and
hence not supported by every channel. If your channel does not support it, consider
using the :ref:`callbackInput` channel to send messages to a webhook.


.. note::

Running an action in a conversation changes the conversation history and affects the
assistant's next predictions. If you don't want this to happen, make sure that your action
reverts itself by appending a ``ActionReverted`` event to the end of the
conversation tracker.

.. _default-actions:

Default Actions
Expand Down
29 changes: 29 additions & 0 deletions docs/core/responses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,32 @@ The endpoint then needs to respond with the generated response:
}

Rasa will then use this response and sent it back to the user.


.. _external-events:

Proactively Reaching Out to the User with External Events
---------------------------------------------------------

You may want to proactively reach out to the user,
for example to display the output of a long running background operation
or notify the user of an external event.

To do so, you can ``POST`` an intent to the
`trigger_intent endpoint <../../api/http-api/#operation/triggerConversationIntent>`_.
The intent, let's call it ``EXTERNAL_sensor``, will be treated as if the user had sent a message with this intent.
You can even provide a dictionary of entities as parameters, e.g. ``{"temperature": "high"}``.
For your bot to respond, we recommend you use the :ref:`mapping-policy` to connect the sent intent ``EXTERNAL_sensor``
with the action you want your bot to execute, e.g. ``utter_warn_temperature``.
You can also use a custom action here, of course.

Use the ``output_channel`` query parameter to specify which output
channel should be used to communicate the assistant's responses back to the user.
Any messages that are dispatched in the custom action will be forwarded to the specified output channel.
JEM-Mosig marked this conversation as resolved.
Show resolved Hide resolved
Set this parameter to ``"latest"`` if you want to use the latest input channel that the user has used.

.. note::

Proactively reaching out to the user is dependent on the abilities of a channel and
hence not supported by every channel. If your channel does not support it, consider
using the :ref:`callbackInput` channel to send messages to a webhook.
erohmensing marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 14 additions & 0 deletions rasa/core/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,20 @@ async def execute_action(
sender_id, action, output_channel, self.nlg, policy, confidence
)

async def trigger_intent(
self,
intent_name: Text,
entities: List[Dict[Text, Any]],
output_channel: OutputChannel,
tracker: DialogueStateTracker,
) -> None:
"""Trigger a user intent, e.g. triggered by an external event."""

processor = self.create_processor()
await processor.trigger_external_user_uttered(
intent_name, entities, tracker, output_channel,
)

async def handle_text(
self,
text_message: Union[Text, Dict[Text, Any]],
Expand Down
4 changes: 4 additions & 0 deletions rasa/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

# start of special user message section
INTENT_MESSAGE_PREFIX = "/"
EXTERNAL_MESSAGE_PREFIX = "EXTERNAL: "

USER_INTENT_RESTART = "restart"

Expand All @@ -37,6 +38,9 @@

BEARER_TOKEN_PREFIX = "Bearer "

# Key to access data in the event metadata which specifies if an event was caused by an external entity (e.g. a sensor).
IS_EXTERNAL = "is_external"

# the lowest priority intended to be used by machine learning policies
DEFAULT_POLICY_PRIORITY = 1
# the priority intended to be used by mapping policies
Expand Down
Loading