From 70f397cdbe1cfbaafd179541f979dc80e4401b85 Mon Sep 17 00:00:00 2001 From: vcidst Date: Thu, 18 Jan 2024 17:48:44 +0100 Subject: [PATCH 1/5] update full retrieval intent name --- rasa/core/processor.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rasa/core/processor.py b/rasa/core/processor.py index ec6ef1260a3e..9c6afbbe1635 100644 --- a/rasa/core/processor.py +++ b/rasa/core/processor.py @@ -66,7 +66,11 @@ ENTITIES, INTENT, INTENT_NAME_KEY, + INTENT_RESPONSE_KEY, PREDICTED_CONFIDENCE_KEY, + FULL_RETRIEVAL_INTENT_NAME_KEY, + RESPONSE_SELECTOR, + RESPONSE, TEXT, ) from rasa.utils.endpoints import EndpointConfig @@ -721,6 +725,7 @@ async def parse_message( message, tracker, only_output_properties ) + self._update_full_retrieval_intent(parse_data) structlogger.debug( "processor.message.parse", parse_data_text=copy.deepcopy(parse_data["text"]), @@ -732,6 +737,19 @@ async def parse_message( return parse_data + def _update_full_retrieval_intent(self, parse_data: Dict[Text, Any]) -> None: + """Update the parse data with the full retrieval intent. + + Args: + parse_data: Message parse data to update. + """ + intent_name = parse_data.get(INTENT, {}).get(INTENT_NAME_KEY) + response_selector = parse_data.get(RESPONSE_SELECTOR, {}) + all_retrieval_intents = response_selector.get("all_retrieval_intents", []) + if intent_name and intent_name in all_retrieval_intents: + retrieval_intent = response_selector.get(intent_name, {}).get(RESPONSE, {}).get(INTENT_RESPONSE_KEY) + parse_data[INTENT][FULL_RETRIEVAL_INTENT_NAME_KEY] = retrieval_intent + def _parse_message_with_graph( self, message: UserMessage, From 3b56c647005cde390276045b8eee9f84d4206cdb Mon Sep 17 00:00:00 2001 From: vcidst Date: Thu, 18 Jan 2024 17:53:33 +0100 Subject: [PATCH 2/5] format --- rasa/core/processor.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rasa/core/processor.py b/rasa/core/processor.py index 9c6afbbe1635..fc628c7a7247 100644 --- a/rasa/core/processor.py +++ b/rasa/core/processor.py @@ -747,7 +747,11 @@ def _update_full_retrieval_intent(self, parse_data: Dict[Text, Any]) -> None: response_selector = parse_data.get(RESPONSE_SELECTOR, {}) all_retrieval_intents = response_selector.get("all_retrieval_intents", []) if intent_name and intent_name in all_retrieval_intents: - retrieval_intent = response_selector.get(intent_name, {}).get(RESPONSE, {}).get(INTENT_RESPONSE_KEY) + retrieval_intent = ( + response_selector.get(intent_name, {}) + .get(RESPONSE, {}) + .get(INTENT_RESPONSE_KEY) + ) parse_data[INTENT][FULL_RETRIEVAL_INTENT_NAME_KEY] = retrieval_intent def _parse_message_with_graph( From acf376b675d42acf8faaffff5564a2cad3e17ede Mon Sep 17 00:00:00 2001 From: vcidst Date: Fri, 19 Jan 2024 11:13:30 +0100 Subject: [PATCH 3/5] add test for test_update_full_retrieval_intent --- tests/core/test_processor.py | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/core/test_processor.py b/tests/core/test_processor.py index 392d85c29745..2a86592a3c70 100644 --- a/tests/core/test_processor.py +++ b/tests/core/test_processor.py @@ -84,6 +84,11 @@ IS_EXTERNAL, SESSION_START_METADATA_SLOT, ) +from rasa.shared.nlu.constants import ( + INTENT, + INTENT_NAME_KEY, + FULL_RETRIEVAL_INTENT_NAME_KEY, +) import logging @@ -1928,3 +1933,62 @@ async def test_run_anonymization_pipeline_mocked_pipeline( await processor.run_anonymization_pipeline(tracker) event_diff.assert_called_once() + + +async def test_update_full_retrieval_intent( + default_processor: MessageProcessor, +) -> None: + parse_data = { + "text": "I like sunny days in berlin", + "intent": {"name": "chitchat", "confidence": 0.9}, + "entities": [], + "response_selector": { + { + "all_retrieval_intents": ["faq", "chitchat"], + "faq": { + "response": { + "responses": [{"text": "Our return policy lasts 30 days."}], + "confidence": 1.0, + "intent_response_key": "faq/what_is_return_policy", + "utter_action": "utter_faq/what_is_return_policy", + }, + "ranking": [ + { + "confidence": 1.0, + "intent_response_key": "faq/what_is_return_policy", + }, + { + "confidence": 2.3378809862799945e-19, + "intent_response_key": "faq/how_can_i_track_my_order", + }, + ], + }, + "chitchat": { + "response": { + "responses": [ + { + "text": "I am not sure of the whole week but I can see the sun is out today." + }, + ], + "confidence": 1.0, + "intent_response_key": "chitchat/ask_weather", + "utter_action": "utter_chitchat/ask_weather", + }, + "ranking": [ + { + "confidence": 1.0, + "intent_response_key": "chitchat/ask_weather", + }, + {"confidence": 0.0, "intent_response_key": "chitchat/ask_name"}, + ], + }, + } + }, + } + + default_processor._update_full_retrieval_intent(parse_data) + + assert parse_data[INTENT][INTENT_NAME_KEY] == "chitchat" + # assert that parse_data["intent"] has a key called response + assert FULL_RETRIEVAL_INTENT_NAME_KEY in parse_data[INTENT] + assert parse_data[INTENT][FULL_RETRIEVAL_INTENT_NAME_KEY] == "chitchat/ask_weather" From 58dc8fb9108eec9171785a960c926ed0446e068d Mon Sep 17 00:00:00 2001 From: vcidst Date: Fri, 19 Jan 2024 11:25:28 +0100 Subject: [PATCH 4/5] fix linting --- tests/core/test_processor.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/core/test_processor.py b/tests/core/test_processor.py index 2a86592a3c70..41ee58c70a08 100644 --- a/tests/core/test_processor.py +++ b/tests/core/test_processor.py @@ -70,7 +70,12 @@ from rasa.core.http_interpreter import RasaNLUHttpInterpreter from rasa.core.processor import MessageProcessor from rasa.shared.core.trackers import DialogueStateTracker -from rasa.shared.nlu.constants import INTENT_NAME_KEY, METADATA_MODEL_ID +from rasa.shared.nlu.constants import ( + INTENT, + INTENT_NAME_KEY, + FULL_RETRIEVAL_INTENT_NAME_KEY, + METADATA_MODEL_ID, +) from rasa.shared.nlu.training_data.message import Message from rasa.utils.endpoints import EndpointConfig from rasa.shared.core.constants import ( @@ -84,11 +89,6 @@ IS_EXTERNAL, SESSION_START_METADATA_SLOT, ) -from rasa.shared.nlu.constants import ( - INTENT, - INTENT_NAME_KEY, - FULL_RETRIEVAL_INTENT_NAME_KEY, -) import logging @@ -1967,7 +1967,7 @@ async def test_update_full_retrieval_intent( "response": { "responses": [ { - "text": "I am not sure of the whole week but I can see the sun is out today." + "text": "The sun is out today! Isn't that great?", }, ], "confidence": 1.0, From c423d683c70e70ea9068fae096fabd90b882d831 Mon Sep 17 00:00:00 2001 From: vcidst Date: Fri, 19 Jan 2024 11:40:40 +0100 Subject: [PATCH 5/5] fix the structure of dict --- tests/core/test_processor.py | 58 +++++++++++++++++------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/tests/core/test_processor.py b/tests/core/test_processor.py index 41ee58c70a08..d0581b1800ef 100644 --- a/tests/core/test_processor.py +++ b/tests/core/test_processor.py @@ -1943,46 +1943,44 @@ async def test_update_full_retrieval_intent( "intent": {"name": "chitchat", "confidence": 0.9}, "entities": [], "response_selector": { - { - "all_retrieval_intents": ["faq", "chitchat"], - "faq": { - "response": { - "responses": [{"text": "Our return policy lasts 30 days."}], + "all_retrieval_intents": ["faq", "chitchat"], + "faq": { + "response": { + "responses": [{"text": "Our return policy lasts 30 days."}], + "confidence": 1.0, + "intent_response_key": "faq/what_is_return_policy", + "utter_action": "utter_faq/what_is_return_policy", + }, + "ranking": [ + { "confidence": 1.0, "intent_response_key": "faq/what_is_return_policy", - "utter_action": "utter_faq/what_is_return_policy", }, - "ranking": [ - { - "confidence": 1.0, - "intent_response_key": "faq/what_is_return_policy", - }, + { + "confidence": 2.3378809862799945e-19, + "intent_response_key": "faq/how_can_i_track_my_order", + }, + ], + }, + "chitchat": { + "response": { + "responses": [ { - "confidence": 2.3378809862799945e-19, - "intent_response_key": "faq/how_can_i_track_my_order", + "text": "The sun is out today! Isn't that great?", }, ], + "confidence": 1.0, + "intent_response_key": "chitchat/ask_weather", + "utter_action": "utter_chitchat/ask_weather", }, - "chitchat": { - "response": { - "responses": [ - { - "text": "The sun is out today! Isn't that great?", - }, - ], + "ranking": [ + { "confidence": 1.0, "intent_response_key": "chitchat/ask_weather", - "utter_action": "utter_chitchat/ask_weather", }, - "ranking": [ - { - "confidence": 1.0, - "intent_response_key": "chitchat/ask_weather", - }, - {"confidence": 0.0, "intent_response_key": "chitchat/ask_name"}, - ], - }, - } + {"confidence": 0.0, "intent_response_key": "chitchat/ask_name"}, + ], + }, }, }