Including slot values in Core Output

Hello, I’ve been using Rasa for awhile. Currently, I’m only using the NLU part of Rasa and created custom chatbot based on the NLU result in python by accessing rasa.nlu.model.Interpreter. It works fine for simple single question-answering system. I’m using this system to connect with another app that requires these outputs from the system:

text: response
intent: intent_name
entities: [extracted_entities]
api_output: [api_object1, api_object2]
is_oot: boolean

Now, I want the chatbot to have more complex dialog flow and planning to use Rasa Core instead of only Rasa NLU. I have designed the custom actions, domain, config, and stories. However, as mentioned above, I need other things as output for the other app. I tried searching in the documentation and the forum on how to solve the problem:

  1. Assuming I run the Rasa Core using the shell command rasa run, the output format from this REST Channels section is good enough, but I want to add more to the output json and I can’t find on how to connect the REST Channels in python since I don’t have experience outside of python.

  2. I prefer to run the server and obtain the output values in python. The best bet that I can do is by accessing the Agent class

>>> from rasa.core.agent import Agent
>>> from rasa.core.interpreter import RasaNLUInterpreter
>>> agent = Agent.load("examples/restaurantbot/models/current")
>>> await agent.handle_text("hello")
            [u'how can I help you?']

However, the handle_text function seems like to only output the text response. Is there any way to extract all the required values above using this way?

Hi Tom, welcome to the Rasa forums! :smile:

It sounds like a custom connector would probably make the most sense here. A custom connector will allow you to create a new channel with a custom REST endpoint. You can find some examples here.

To get the the value of a specific entity, you can use tracker.get_slot from the tracker for the current conversation.

Does that sound like what you were looking for?

Hi, Thank you very much for replying. I will look into the custom REST endpoint.

As for the tracker.get_slot, that is for getting slot value inside the actions.py, right? What I want to do is to obtain the value of specific entities along with the intent class after the chatbot finish 1 turn of conversation (input from user to execute actions based on the input), then send it to the other app that needs that values. The reason why I need to this is because that other app works not only for the chatbot, but also for other functionality as well that takes text input. So, intent and specific entities’ values are signs whether the app should show chatbot response or not, and needed to log information for that app.

After looking into rasa.core.agent code, I think my best bet is to do:

>>> from rasa.core.agent import Agent
>>> from rasa.core.interpreter import RasaNLUInterpreter
>>> # obtain sender_id from the app
>>> agent = Agent.load("examples/restaurantbot/models/current")
>>> await agent.handle_text("hello", sender_id)
            [u'how can I help you?']
>>> tracker = agent.tracker_store.retrieve(sender_id)
>>> value = tracker.get_slot('specific_entity')

Assuming that I run the above code in my custom websocket/HTTP server that I already created for the previous version, which only by using NLU part of Rasa, I have 2 questions

  1. I’m not really sure about the sender_id, but it’s supposed to be same for each user and not for every conversation turn, am I correct?
  2. Will the above method get me the latest entity value for each conversation turn? or should I use the tracker.get_latest_entity_values method instead?

I tried using the code above, but after I see the result from tracker.current_state(), it seems like the latest_action_name is action_listen and not the custom action that I created. Do I need to start action server before doing something like above? I tried starting the action server but it seems like to be stuck in when it tries to register custom form action.

Edit: Okay seems like i need to run action server and it’s not stuck, it works but I can’t Ctrl+C to stop the action server in Windows.

So, after I run action server, I think what I need to do is to do this:

>>> from rasa.core.agent import Agent
>>> from rasa.core.interpreter import RasaNLUInterpreter
>>> from rasa.utils.endpoints import EndpointConfig
>>> # obtain sender_id from the app
>>> action_endpoint = EndpointConfig("localhost:5055/webhook")
>>> agent = Agent.load("examples/restaurantbot/models/current", action_endpoint=action_endpoint)
>>> await agent.handle_text("hello", sender_id)
            [u'how can I help you?']
>>> tracker = agent.tracker_store.retrieve(sender_id)
>>> value = tracker.get_slot('specific_entity')

Sounds like you’ve made some good progress! It’s not very slick, but to stop my action server on windows I usually just kill the terminal it’s running in. :woman_shrugging:

Thank you very much! Yeah I am more comfortable to work within python instead of having rasa core running as a server since the custom actions already need a server to run.

I wrote a comment in another thread that the custom actions server has a 2s delay on Windows machine even though it runs locally on the same machine. From rasa shell --debug window:

2020-03-30 20:17:27 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_search_stuff'.
2020-03-30 20:17:29 DEBUG    rasa.core.processor  - Action 'action_search_stuff' ended with events '[<rasa.core.events.SlotSet object at 0x0000019F76D05988>, <rasa.core.events.SlotSet object at 0x0000019F3BC814C8>, <rasa.core.events.SlotSet object at 0x000001A094B3C348>, <rasa.core.events.SlotSet object at 0x000001A094B3C6C8>]'.

And from rasa run actions --debug window:

2020-03-30 20:17:29 DEBUG    rasa_sdk.executor  - Received request to run 'action_search_stuff'
2020-03-30 20:17:29 DEBUG    rasa_sdk.executor  - Finished running 'action_search_stuff'

As you can see it has 2s delay from rasa shell to rasa custom actions server. Though, I heard from another thread that this problem doesn’t exist on linux machine and it’s fixed if I change the endpoints from http://localhost:5055/webhook to local IP adress and surprisingly it works like a charm. Hopefully this issue on Windows can be done without changing endpoints. I was quite desperate to make this delay disappear that I planned to override the Domain and Agent class to override some functions there so custom actions won’t need server to run on. But I guess it’s not needed anymore.

Also, I’ve found the best way to stop action server on windows, which is to press ctrl+break/pause key, or in my case since I’m on Lenovo laptop is ctrl+Fn+P

The delay problem above is solved here

Also, one more question and I’m sorry to mention you @rctatman, is there any downside by running Rasa Core this way compared to using rasa run method and connect to rasa by using API instead? Other than the need to setting the server by myself, the only thing that I can think of is probably activating reminder events which I’m not sure how it will work if I run it without rasa run command.