Give as an output the entities found

Hello everyone,

I’m a bit stuck trying to give as response the entities found in the input. I’ve tried with the command condition in the responses, but it doesn’t work and it is being tested. Now I create an action and the idea is this one: entities = tracker.latest_message[‘entities’] dispatcher.utter_message({entities}) I suppose this is not well spelled, but I don’t find how to tell the bot to do so.

Thank you so much

Hello @mar

This code in a custom action should let your bot print out all entities present in the latest user message:

last_user_message = tracker.get_last_event_for(USER)
if last_user_message:
    entities = last_user_message.get("parse_data", {}).get(ENTITIES)
    dispatcher.utter_message(f"Entities: {entities}")

where I used two common constants from Rasa:

from rasa.shared.core.constants import USER
from rasa.shared.nlu.constants import ENTITIES

Thank you so much. I’ve tried it and it works perfectly. By the way, do you know if it’s possible just to give as the output the value of the entity? Because with this code it gives me ‘entity’, ‘start’, ‘end’…‘value’

Again, thank you so much.

Sure. You can extract the values like this:

entity_values = [
  entity.get("value")
  for entity in entities
]
dispatcher.utter_message(f"Entities: {entity_values}")
1 Like

Thank you so much. It is exactly what I was looking for.