Skip to content

Commit ce1f5d2

Browse files
authored
Get Full Retrieval Intent Name (#12998)
* update full retrieval intent name * format * add test for test_update_full_retrieval_intent * fix linting * fix the structure of dict
1 parent 8310899 commit ce1f5d2

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

rasa/core/processor.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@
6666
ENTITIES,
6767
INTENT,
6868
INTENT_NAME_KEY,
69+
INTENT_RESPONSE_KEY,
6970
PREDICTED_CONFIDENCE_KEY,
71+
FULL_RETRIEVAL_INTENT_NAME_KEY,
72+
RESPONSE_SELECTOR,
73+
RESPONSE,
7074
TEXT,
7175
)
7276
from rasa.utils.endpoints import EndpointConfig
@@ -721,6 +725,7 @@ async def parse_message(
721725
message, tracker, only_output_properties
722726
)
723727

728+
self._update_full_retrieval_intent(parse_data)
724729
structlogger.debug(
725730
"processor.message.parse",
726731
parse_data_text=copy.deepcopy(parse_data["text"]),
@@ -732,6 +737,23 @@ async def parse_message(
732737

733738
return parse_data
734739

740+
def _update_full_retrieval_intent(self, parse_data: Dict[Text, Any]) -> None:
741+
"""Update the parse data with the full retrieval intent.
742+
743+
Args:
744+
parse_data: Message parse data to update.
745+
"""
746+
intent_name = parse_data.get(INTENT, {}).get(INTENT_NAME_KEY)
747+
response_selector = parse_data.get(RESPONSE_SELECTOR, {})
748+
all_retrieval_intents = response_selector.get("all_retrieval_intents", [])
749+
if intent_name and intent_name in all_retrieval_intents:
750+
retrieval_intent = (
751+
response_selector.get(intent_name, {})
752+
.get(RESPONSE, {})
753+
.get(INTENT_RESPONSE_KEY)
754+
)
755+
parse_data[INTENT][FULL_RETRIEVAL_INTENT_NAME_KEY] = retrieval_intent
756+
735757
def _parse_message_with_graph(
736758
self,
737759
message: UserMessage,

tests/core/test_processor.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@
7070
from rasa.core.http_interpreter import RasaNLUHttpInterpreter
7171
from rasa.core.processor import MessageProcessor
7272
from rasa.shared.core.trackers import DialogueStateTracker
73-
from rasa.shared.nlu.constants import INTENT_NAME_KEY, METADATA_MODEL_ID
73+
from rasa.shared.nlu.constants import (
74+
INTENT,
75+
INTENT_NAME_KEY,
76+
FULL_RETRIEVAL_INTENT_NAME_KEY,
77+
METADATA_MODEL_ID,
78+
)
7479
from rasa.shared.nlu.training_data.message import Message
7580
from rasa.utils.endpoints import EndpointConfig
7681
from rasa.shared.core.constants import (
@@ -1928,3 +1933,60 @@ async def test_run_anonymization_pipeline_mocked_pipeline(
19281933
await processor.run_anonymization_pipeline(tracker)
19291934

19301935
event_diff.assert_called_once()
1936+
1937+
1938+
async def test_update_full_retrieval_intent(
1939+
default_processor: MessageProcessor,
1940+
) -> None:
1941+
parse_data = {
1942+
"text": "I like sunny days in berlin",
1943+
"intent": {"name": "chitchat", "confidence": 0.9},
1944+
"entities": [],
1945+
"response_selector": {
1946+
"all_retrieval_intents": ["faq", "chitchat"],
1947+
"faq": {
1948+
"response": {
1949+
"responses": [{"text": "Our return policy lasts 30 days."}],
1950+
"confidence": 1.0,
1951+
"intent_response_key": "faq/what_is_return_policy",
1952+
"utter_action": "utter_faq/what_is_return_policy",
1953+
},
1954+
"ranking": [
1955+
{
1956+
"confidence": 1.0,
1957+
"intent_response_key": "faq/what_is_return_policy",
1958+
},
1959+
{
1960+
"confidence": 2.3378809862799945e-19,
1961+
"intent_response_key": "faq/how_can_i_track_my_order",
1962+
},
1963+
],
1964+
},
1965+
"chitchat": {
1966+
"response": {
1967+
"responses": [
1968+
{
1969+
"text": "The sun is out today! Isn't that great?",
1970+
},
1971+
],
1972+
"confidence": 1.0,
1973+
"intent_response_key": "chitchat/ask_weather",
1974+
"utter_action": "utter_chitchat/ask_weather",
1975+
},
1976+
"ranking": [
1977+
{
1978+
"confidence": 1.0,
1979+
"intent_response_key": "chitchat/ask_weather",
1980+
},
1981+
{"confidence": 0.0, "intent_response_key": "chitchat/ask_name"},
1982+
],
1983+
},
1984+
},
1985+
}
1986+
1987+
default_processor._update_full_retrieval_intent(parse_data)
1988+
1989+
assert parse_data[INTENT][INTENT_NAME_KEY] == "chitchat"
1990+
# assert that parse_data["intent"] has a key called response
1991+
assert FULL_RETRIEVAL_INTENT_NAME_KEY in parse_data[INTENT]
1992+
assert parse_data[INTENT][FULL_RETRIEVAL_INTENT_NAME_KEY] == "chitchat/ask_weather"

0 commit comments

Comments
 (0)