Skip to content

Commit

Permalink
Merge branch 'filter-grads' of https://github.com/RasaHQ/rasa into fi…
Browse files Browse the repository at this point in the history
…lter-grads
  • Loading branch information
Ghostvv committed Apr 23, 2020
2 parents 5b523d7 + 0ad56bf commit 11525d3
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/vulnerability-scan.yml
Expand Up @@ -41,3 +41,4 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
image: ${{ env.IMAGE_WITH_POETRY_LOCK }}
ignore_unfixed: true
issue_label: "tool:trivy,type:vulnerability"
3 changes: 3 additions & 0 deletions changelog/5574.bugfix.rst
@@ -0,0 +1,3 @@
Fixed an issue that happened when metadata is passed in a new session.

Now the metadata is correctly passed to the ActionSessionStart.
1 change: 1 addition & 0 deletions changelog/5690.improvement.rst
@@ -0,0 +1 @@
Add ``full_retrieval_intent`` property to ``ResponseSelector`` rankings
14 changes: 2 additions & 12 deletions docs/user-guide/connectors/your-own-website.rst
Expand Up @@ -12,19 +12,9 @@ is usually the chat interface that ships with Rasa X, where you can `invite user
to test your bot <https://rasa.com/docs/rasa-x/user-guide/enable-workflows/#conversations-with-test-users>`_.

If you already have an existing website and want to add a Rasa assistant to it,
you have a couple of options:
you can use `Chatroom <https://github.com/scalableminds/chatroom>`_, a widget which you can incorporate into your existing webpage by adding a HTML snippet.
Alternatively, you can also build your own chat widget.

- `Rasa Webchat <https://github.com/mrbot-ai/rasa-webchat>`_, which
uses websockets.
- `Chatroom <https://github.com/scalableminds/chatroom>`_, which
uses regular HTTP requests.
- `rasa-bot <https://github.com/assister-ai/assister/tree/master/packages/rasa>`_, a
Web Component which uses regular HTTP requests.

.. note::

These projects are developed by external developers, if there are any issues with
them, please open tickets in the respective repositories.

Websocket Channel
~~~~~~~~~~~~~~~~~
Expand Down
22 changes: 18 additions & 4 deletions rasa/core/processor.py
Expand Up @@ -36,6 +36,7 @@
ReminderScheduled,
SlotSet,
UserUttered,
SessionStarted,
)
from rasa.core.interpreter import (
INTENT_MESSAGE_PREFIX,
Expand Down Expand Up @@ -149,7 +150,10 @@ async def predict_next(self, sender_id: Text) -> Optional[Dict[Text, Any]]:
}

async def _update_tracker_session(
self, tracker: DialogueStateTracker, output_channel: OutputChannel
self,
tracker: DialogueStateTracker,
output_channel: OutputChannel,
metadata: Optional[Dict] = None,
) -> None:
"""Check the current session in `tracker` and update it if expired.
Expand All @@ -158,6 +162,7 @@ async def _update_tracker_session(
restart are considered).
Args:
metadata: Data sent from client associated with the incoming user message.
tracker: Tracker to inspect.
output_channel: Output channel for potential utterances in a custom
`ActionSessionStart`.
Expand All @@ -167,6 +172,9 @@ async def _update_tracker_session(
f"Starting a new session for conversation ID '{tracker.sender_id}'."
)

if metadata:
tracker.events.append(SessionStarted(metadata=metadata))

await self._run_action(
action=self._get_action(ACTION_SESSION_START_NAME),
tracker=tracker,
Expand All @@ -175,13 +183,17 @@ async def _update_tracker_session(
)

async def get_tracker_with_session_start(
self, sender_id: Text, output_channel: Optional[OutputChannel] = None
self,
sender_id: Text,
output_channel: Optional[OutputChannel] = None,
metadata: Optional[Dict] = None,
) -> Optional[DialogueStateTracker]:
"""Get tracker for `sender_id` or create a new tracker for `sender_id`.
If a new tracker is created, `action_session_start` is run.
Args:
metadata: Data sent from client associated with the incoming user message.
output_channel: Output channel associated with the incoming user message.
sender_id: Conversation ID for which to fetch the tracker.
Expand All @@ -193,7 +205,7 @@ async def get_tracker_with_session_start(
if not tracker:
return None

await self._update_tracker_session(tracker, output_channel)
await self._update_tracker_session(tracker, output_channel, metadata)

return tracker

Expand Down Expand Up @@ -233,7 +245,7 @@ async def log_message(
# we have a Tracker instance for each user
# which maintains conversation state
tracker = await self.get_tracker_with_session_start(
message.sender_id, message.output_channel
message.sender_id, message.output_channel, message.metadata
)

if tracker:
Expand Down Expand Up @@ -291,10 +303,12 @@ def predict_next_action(
action = self.domain.action_for_index(
max_confidence_index, self.action_endpoint
)

logger.debug(
f"Predicted next action '{action.name()}' with confidence "
f"{action_confidences[max_confidence_index]:.2f}."
)

return action, policy, action_confidences[max_confidence_index]

@staticmethod
Expand Down
5 changes: 5 additions & 0 deletions rasa/nlu/selectors/response_selector.py
Expand Up @@ -320,6 +320,11 @@ def process(self, message: Message, **kwargs: Any) -> None:
label, label_ranking = self._predict_label(out)
retrieval_intent_name = self.retrieval_intent_mapping.get(label.get("name"))

for ranking in label_ranking:
ranking["full_retrieval_intent"] = self.retrieval_intent_mapping.get(
ranking.get("name")
)

selector_key = (
self.retrieval_intent
if self.retrieval_intent
Expand Down
28 changes: 28 additions & 0 deletions tests/core/test_processor.py
Expand Up @@ -485,6 +485,34 @@ async def test_update_tracker_session(
]


# noinspection PyProtectedMember
async def test_update_tracker_session_with_metadata(
default_channel: CollectingOutputChannel,
default_processor: MessageProcessor,
monkeypatch: MonkeyPatch,
):
sender_id = uuid.uuid4().hex
tracker = default_processor.tracker_store.get_or_create_tracker(sender_id)

# patch `_has_session_expired()` so the `_update_tracker_session()` call actually
# does something
monkeypatch.setattr(default_processor, "_has_session_expired", lambda _: True)

metadata = {"metadataTestKey": "metadataTestValue"}

await default_processor._update_tracker_session(tracker, default_channel, metadata)

# the save is not called in _update_tracker_session()
default_processor._save_tracker(tracker)

# inspect tracker events and make sure SessionStarted event is present and has metadata.
tracker = default_processor.tracker_store.retrieve(sender_id)
session_event_idx = tracker.events.index(SessionStarted())
session_event_metadata = tracker.events[session_event_idx].metadata

assert session_event_metadata == metadata


# noinspection PyProtectedMember
async def test_update_tracker_session_with_slots(
default_channel: CollectingOutputChannel,
Expand Down
8 changes: 8 additions & 0 deletions tests/nlu/selectors/test_selectors.py
Expand Up @@ -43,3 +43,11 @@ def test_train_selector(pipeline, component_builder, tmpdir):
.get("default")
.get("full_retrieval_intent")
) is not None

ranking = parsed.get(RESPONSE_SELECTOR_PROPERTY_NAME).get("default").get("ranking")
assert ranking is not None

for rank in ranking:
assert rank.get("name") is not None
assert rank.get("confidence") is not None
assert rank.get("full_retrieval_intent") is not None

0 comments on commit 11525d3

Please sign in to comment.