Skip to content

Commit

Permalink
Merge pull request #4092 from RasaHQ/metadata_field
Browse files Browse the repository at this point in the history
Create metadata UserMessage attribute
  • Loading branch information
arthurTemporim committed Aug 1, 2019
2 parents 2c4e8f4 + 2a3a975 commit ed42993
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Changed
- API requests are not longer logged to ``rasa_core.log`` by default in order to avoid
problems when running on OpenShift (use ``--log-file rasa_core.log`` to retain the
old behavior)
- ``metadata`` attribute added to ``UserMessage``

Removed
-------
Expand Down
6 changes: 3 additions & 3 deletions data/test_trackers/tracker_moodbot.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"event": "user",
"text": "/greet",
"input_channel": null
"input_channel": null, "message_id": null, "metadata": null
},
{
"timestamp": 1517821726.200373,
Expand Down Expand Up @@ -105,7 +105,7 @@
},
"event": "user",
"text": "/mood_great",
"input_channel": null
"input_channel": null, "message_id": null, "metadata": null
},
{
"timestamp": 1517821726.209908,
Expand All @@ -122,4 +122,4 @@
"confidence": 1.0
}
]
}
}
14 changes: 14 additions & 0 deletions docs/user-guide/connectors/custom-connectors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ To send a message, you would run a command like:
where ``myio`` is the name of your component.

If you need to use extra information from your front end in your custom
actions, you can add this information in the ``metadata`` dict of your user
message. This information will accompany the user message through the rasa
server into the action server when applicable, where you can find it stored in
the ``tracker``. Message metadata will not directly affect NLU classification
or action prediction.

Here are all the attributes of ``UserMessage``:

.. autoclass:: rasa.core.channels.UserMessage

.. automethod:: __init__


In your implementation of the ``receive`` endpoint, you need to make
sure to call ``on_new_message(UserMessage(text, output, sender_id))``.
This will tell Rasa Core to handle this user message. The ``output``
Expand Down
17 changes: 16 additions & 1 deletion rasa/core/channels/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ def __init__(
parse_data: Dict[Text, Any] = None,
input_channel: Optional[Text] = None,
message_id: Optional[Text] = None,
metadata: Optional[Dict] = None,
) -> None:
"""Creates a ``UserMessage`` object.
Args:
text: the message text content.
output_channel: the output channel which should be used to send
bot responses back to the user.
sender_id: the message owner ID.
parse_data: rasa data about the message.
input_channel: the name of the channel which received this message.
message_id: ID of the message.
metadata: additional metadata for this message.
"""
self.text = text.strip() if text else text

if message_id is not None:
Expand All @@ -56,6 +70,7 @@ def __init__(
self.input_channel = input_channel

self.parse_data = parse_data
self.metadata = metadata


def register(
Expand Down Expand Up @@ -395,7 +410,7 @@ async def on_message_wrapper(
text: Text,
queue: Queue,
sender_id: Text,
input_channel,
input_channel: Text,
) -> None:
collector = QueueOutputChannel(queue)

Expand Down
42 changes: 28 additions & 14 deletions rasa/core/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,21 @@ class UserUttered(Event):

def __init__(
self,
text,
text: Optional[Text] = None,
intent=None,
entities=None,
parse_data=None,
timestamp=None,
input_channel=None,
message_id=None,
parse_data: Optional[Dict[Text, Any]] = None,
timestamp: int = time.time(),
input_channel: Optional[Text] = None,
message_id: Optional[Text] = None,
metadata: Optional[Dict] = None,
):
self.text = text
self.intent = intent if intent else {}
self.entities = entities if entities else []
self.input_channel = input_channel
self.message_id = message_id
self.metadata = metadata

if parse_data:
self.parse_data = parse_data
Expand All @@ -201,19 +203,30 @@ def __init__(
"intent": self.intent,
"entities": self.entities,
"text": text,
"message_id": self.message_id,
"metadata": self.metadata,
}

super(UserUttered, self).__init__(timestamp)

@staticmethod
def _from_parse_data(text, parse_data, timestamp=None, input_channel=None):
def _from_parse_data(
text: Text,
parse_data: Dict[Text, Any],
timestamp: Optional[int] = None,
input_channel: Optional[Text] = None,
message_id: Optional[Text] = None,
metadata: Optional[Dict] = None,
):
return UserUttered(
text,
parse_data.get("intent"),
parse_data.get("entities", []),
parse_data,
timestamp,
input_channel,
message_id,
metadata,
)

def __hash__(self):
Expand Down Expand Up @@ -244,19 +257,18 @@ def __str__(self):
def empty():
return UserUttered(None)

def as_dict(self):
d = super(UserUttered, self).as_dict()
input_channel = None # for backwards compatibility (persisted evemts)
if hasattr(self, "input_channel"):
input_channel = self.input_channel
d.update(
def as_dict(self) -> Dict[Text, Any]:
_dict = super(UserUttered, self).as_dict()
_dict.update(
{
"text": self.text,
"parse_data": self.parse_data,
"input_channel": input_channel,
"input_channel": getattr(self, "input_channel", None),
"message_id": getattr(self, "message_id", None),
"metadata": getattr(self, "metadata", None),
}
)
return d
return _dict

@classmethod
def _from_story_string(cls, parameters: Dict[Text, Any]) -> Optional[List[Event]]:
Expand All @@ -267,6 +279,8 @@ def _from_story_string(cls, parameters: Dict[Text, Any]) -> Optional[List[Event]
parameters.get("parse_data"),
parameters.get("timestamp"),
parameters.get("input_channel"),
parameters.get("message_id"),
parameters.get("metadata"),
)
]
except KeyError as e:
Expand Down
1 change: 1 addition & 0 deletions rasa/core/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ async def _handle_message_with_tracker(
parse_data,
input_channel=message.input_channel,
message_id=message.message_id,
metadata=message.metadata,
),
self.domain,
)
Expand Down
16 changes: 14 additions & 2 deletions tests/core/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ async def test_remote_action_runs(
"sender_id": "my-sender",
"version": rasa.__version__,
"tracker": {
"latest_message": {"entities": [], "intent": {}, "text": None},
"latest_message": {
"entities": [],
"intent": {},
"text": None,
"message_id": None,
"metadata": None,
},
"active_form": {},
"latest_action_name": None,
"sender_id": "my-sender",
Expand Down Expand Up @@ -171,7 +177,13 @@ async def test_remote_action_logs_events(
"sender_id": "my-sender",
"version": rasa.__version__,
"tracker": {
"latest_message": {"entities": [], "intent": {}, "text": None},
"latest_message": {
"entities": [],
"intent": {},
"text": None,
"message_id": None,
"metadata": None,
},
"active_form": {},
"latest_action_name": None,
"sender_id": "my-sender",
Expand Down
5 changes: 5 additions & 0 deletions tests/core/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def test_event_has_proper_implementation(one_event, another_event):
"one_event",
[
UserUttered("/greet", {"name": "greet", "confidence": 1.0}, []),
UserUttered(metadata={"type": "text"}),
UserUttered(metadata=None),
UserUttered(text="hi", message_id="1", metadata={"type": "text"}),
SlotSet("name", "rasa"),
Restarted(),
AllSlotsReset(),
Expand Down Expand Up @@ -135,6 +138,7 @@ def test_json_parse_user():
},
"entities": []
},
"metadata": {},
}
# DOCS END
# fmt: on
Expand All @@ -143,6 +147,7 @@ def test_json_parse_user():
intent={"name": "greet", "confidence": 0.9},
entities=[],
parse_data={"intent": {"name": "greet", "confidence": 0.9}, "entities": []},
metadata={},
)


Expand Down
8 changes: 7 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,13 @@ def test_requesting_non_existent_tracker(rasa_app: SanicTestClient):
"timestamp": 1514764800,
}
]
assert content["latest_message"] == {"text": None, "intent": {}, "entities": []}
assert content["latest_message"] == {
"text": None,
"intent": {},
"entities": [],
"message_id": None,
"metadata": None,
}


@pytest.mark.parametrize("event", test_events)
Expand Down

0 comments on commit ed42993

Please sign in to comment.