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

add single event schema #10572

Merged
merged 4 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog/10572.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a schema for single events.
2 changes: 1 addition & 1 deletion docs/docs/how-to-deploy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ For assistants that will receive a lot of user traffic, setting up a Kubernetes
our Helm charts is the best option. This provides a scalable architecture that is also straightforward to deploy.
However, you can also customize the Helm charts if you have specific requirements.

* Default: Read the [Helm Chart Installation](https://rasa.com/docs/rasa-x/installation-and-setup/install/helm-chart/) docs.
* Default: Read the [Helm Chart Installation](https://rasa.com/docs/rasa-x/0.42.x/installation-and-setup/install/helm-chart) docs.

* Custom: Read the above, as well as the [Advanced Configuration](https://rasa.com/docs/rasa-x/installation-and-setup/customize/#helm-chart)
documentation, and customize the [open source Helm charts](https://github.com/RasaHQ/rasa-x-helm) to your needs.
Expand Down
69 changes: 34 additions & 35 deletions rasa/shared/utils/schemas/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,40 +133,39 @@
SESSION_STARTED = {"properties": {"event": {"const": "session_started"}}}
AGENT_UTTERED = {"properties": {"event": {"const": "agent"}}}

EVENTS_SCHEMA = {
"type": "array",
"items": {
"type": "object",
"properties": {
"event": {"type": "string"},
"timestamp": {"type": ["number", "null"]},
"metadata": {"type": ["object", "null"]},
},
"required": ["event"],
"oneOf": [
USER_UTTERED,
ACTION_EXECUTED,
SLOT_SET,
ENTITIES_ADDED,
USER_UTTERED_FEATURIZATION,
REMINDER_CANCELLED,
REMINDER_SCHEDULED,
ACTION_EXECUTION_REJECTED,
FORM_VALIDATION,
LOOP_INTERRUPTED,
FORM,
ACTIVE_LOOP,
ALL_SLOTS_RESET,
CONVERSATION_RESUMED,
CONVERSATION_PAUSED,
FOLLOWUP_ACTION,
STORY_EXPORTED,
RESTARTED,
ACTION_REVERTED,
USER_UTTERANCE_REVERTED,
BOT_UTTERED,
SESSION_STARTED,
AGENT_UTTERED,
],
EVENT_SCHEMA = {
"type": "object",
"properties": {
"event": {"type": "string"},
"timestamp": {"type": ["number", "null"]},
"metadata": {"type": ["object", "null"]},
},
"required": ["event"],
"oneOf": [
USER_UTTERED,
ACTION_EXECUTED,
SLOT_SET,
ENTITIES_ADDED,
USER_UTTERED_FEATURIZATION,
REMINDER_CANCELLED,
REMINDER_SCHEDULED,
ACTION_EXECUTION_REJECTED,
FORM_VALIDATION,
LOOP_INTERRUPTED,
FORM,
ACTIVE_LOOP,
ALL_SLOTS_RESET,
CONVERSATION_RESUMED,
CONVERSATION_PAUSED,
FOLLOWUP_ACTION,
STORY_EXPORTED,
RESTARTED,
ACTION_REVERTED,
USER_UTTERANCE_REVERTED,
BOT_UTTERED,
SESSION_STARTED,
AGENT_UTTERED,
],
}

EVENTS_SCHEMA = {"type": "array", "items": EVENT_SCHEMA}
78 changes: 76 additions & 2 deletions tests/shared/utils/schemas/test_events.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,74 @@
from typing import Type

import pytest
import jsonschema
from jsonschema import ValidationError, validate

from rasa.core.actions.action import RemoteAction
from rasa.shared.core.events import Event
from rasa.shared.utils.schemas.events import EVENT_SCHEMA
import rasa.shared.utils.common

TEST_EVENT = {
"sender_id": "77bd4d3841294f1f9f82ef8cfe9321a7",
"event": "user",
"timestamp": 1640019570.8848355,
"text": "hey 👋🏻",
"parse_data": {
"intent": {
"id": 5103161883140543062,
"name": "greet",
"confidence": 0.9999818801879883,
},
"entities": [],
"text": "hey",
"message_id": "52ec1cba47c840d798d9f612334a750d",
"metadata": {},
"intent_ranking": [
{
"id": 5103161883140543062,
"name": "greet",
"confidence": 0.9999818801879883,
},
],
"response_selector": {
"all_retrieval_intents": ["chitchat"],
"faq": {
"response": {
"id": None,
"responses": None,
"response_templates": None,
"confidence": 0.0,
"intent_response_key": None,
"utter_action": "utter_None",
"template_name": "utter_None",
},
"ranking": [],
},
"chitchat": {
"response": {
"id": -2223917631873543698,
"responses": [{"text": "I am called Retrieval Bot!"},],
"response_templates": [{"text": "I am called Retrieval Bot!"},],
"confidence": 0.9660752415657043,
"intent_response_key": "chitchat/ask_name",
"utter_action": "utter_chitchat/ask_name",
"template_name": "utter_chitchat/ask_name",
},
"ranking": [
{
"id": -2223917631873543698,
"confidence": 0.9660752415657043,
"intent_response_key": "chitchat/ask_name",
},
],
},
},
},
"input_channel": "rasa",
"message_id": "52ec1cba47c840d798d9f612334a750d",
"metadata": {},
}


@pytest.mark.parametrize("event_class", rasa.shared.utils.common.all_subclasses(Event))
def test_remote_action_validate_all_event_subclasses(event_class: Type[Event]):
Expand All @@ -27,4 +89,16 @@ def test_remote_action_validate_all_event_subclasses(event_class: Type[Event]):
"wrong_action",
"warning_predicted",
]:
jsonschema.validate(response, RemoteAction.action_response_format_spec())
validate(response, RemoteAction.action_response_format_spec())


def test_validate_single_event():
validate(TEST_EVENT, EVENT_SCHEMA)


def test_validate_single_event_raises():
test_wrong_schema = TEST_EVENT.copy()
test_wrong_schema["event"] = "non-existing event 🪲"

with pytest.raises(ValidationError):
validate(test_wrong_schema, EVENT_SCHEMA)