API call to get response

Hello there, I have build a simple rasabot, and want to use it from web front-end, so I am sending the following json to=> http://127.0.0.1:5005/webhooks/rest/webhook json is => {“sender”:“Me”,“message”:“thanks”,“token”:“None”}

It responds successfully, But I want to have all the info like slot values, entities, intents etc, how to do this? Is this the right way to do it? Please provide me some guidance, Will this work for multiple users chatting simultaneously?

for multiple users, this will work so long as you pass a different value for sender for each one.

You can fetch the tracker using the HTTP API , but maybe you can elaborate on exactly what you want to build?

1 Like

I want to pass a string via an API from frond-end like I mentioned, and the API will respond with a json with the response string and all the regarding info, like slots, intent of the string, so that some minor modifications can be done at front-end, is that clear? BTW thanks for your response…

understood. well you have two options:

1 is that you create extra requests to fetch the tracker (not ideal).

2 is that you create a custom channel. to do that, subclass the rest channel and override the send_message function to send the additional info you want

1 Like

thank you for your response, Can you elaborately describe how to do the option 2, is there any examples or documentations available?

here is a code I found in RASA FORAM and documentation as well;

import logging import uuid import inspect import rasa from sanic import Blueprint, response from sanic.request import Request from socketio import AsyncServer from typing import Text, List, Dict, Any, Optional, Callable, Iterable, Awaitable from asyncio import Queue, CancelledError from rasa.core.channels.channel import UserMessage, OutputChannel, CollectingOutputChannel, InputChannel

logger = logging.getLogger(name)

class RestInput(InputChannel): “”"A custom http input channel.

This implementation is the basis for a custom implementation of a chat
frontend. You can customize this to send messages to Rasa Core and
retrieve responses from the agent."""

@classmethod
def name(cls):
    print("hi from name method")
    return "rest"

@staticmethod
async def on_message_wrapper(
    on_new_message: Callable[[UserMessage], Awaitable[None]],
    text: Text,
    queue: Queue,
    sender_id: Text,
) -> None:

    print("Inside on_message_wrapper function")
    collector = QueueOutputChannel(queue)

    message = UserMessage(
        text, collector, sender_id, input_channel=RestInput.name()
    )

    print("above on_new_message method")
    await on_new_message(message)

    await queue.put("DONE")  # pytype: disable=bad-return-type

async def _extract_sender(self, req) -> Optional[Text]:
    return req.json.get("sender", None)

# noinspection PyMethodMayBeStatic
def _extract_message(self, req):
    print("User message ::- ",req.json.get("message", None))
    return req.json.get("message", None)

def stream_response(
    self,
    on_new_message: Callable[[UserMessage], Awaitable[None]],
    text: Text,
    sender_id: Text,
) -> Callable[[Any], Awaitable[None]]:
    async def stream(resp: Any) -> None:
        q = Queue()
        task = asyncio.ensure_future(
            self.on_message_wrapper(on_new_message, text, q, sender_id)
        )
        while True:
            result = await q.get()  # pytype: disable=bad-return-type
            if result == "DONE":
                break
            else:
                await resp.write(json.dumps(result) + "\n")
        await task

    return stream  # pytype: disable=bad-return-type

def blueprint(self, on_new_message: Callable[[UserMessage], Awaitable[None]]):
    custom_webhook = Blueprint(
        "custom_webhook_{}".format(type(self).__name__),
        inspect.getmodule(self).__name__,
    )

    # noinspection PyUnusedLocal
    @custom_webhook.route("/", methods=["GET"])
    async def health(request: Request):
        print("Inside health")
        return response.json({"status": "ok"})

    @custom_webhook.route("/webhook", methods=["POST"])
    async def receive(request: Request):
        print("Inside receive")
        sender_id = await self._extract_sender(request)
        text = self._extract_message(request)
        print("sender_id is ::-",sender_id)
        print("text is ::-",text)
        should_use_stream = rasa.utils.endpoints.bool_arg(
            request, "stream", default=False
        )

        if should_use_stream:
            return response.stream(
                self.stream_response(on_new_message, text, sender_id),
                 content_type="text/event-stream",
                
            )
        else:
            collector = CollectingOutputChannel()
            on_new_message(UserMessage(text, collector, sender_id))
            print("collector MSG::",collector)
            # noinspection PyBroadException
            try:
                await on_new_message(
                    UserMessage(
                        text, collector, sender_id, input_channel=self.name()
                    )
                )
            except CancelledError:
                logger.error(
                    "Message handling timed out for "
                    "user message '{}'.".format(text)
                )
            except Exception:
                logger.exception(
                    "An exception occured while handling "
                    "user message '{}'.".format(text)
                )
            return response.json(collector.messages)
            

    return custom_webhook

Its working fine, like the output is:

(rasa-project-1) gourab@gourab:~/rasa/rasabot$ rasa run -m models --enable-api --cors “*” --debug 2020-01-23 13:30:36 DEBUG rasa.cli.utils - Parameter ‘endpoints’ not set. Using default location ‘endpoints.yml’ instead. 2020-01-23 13:30:36 DEBUG rasa.cli.utils - Parameter ‘credentials’ not set. Using default location ‘credentials.yml’ instead. hi from name method 2020-01-23 13:30:37 DEBUG rasa.core.utils - Available web server routes: /conversations/<conversation_id>/messages POST add_message /conversations/<conversation_id>/tracker/events POST append_events /webhooks/rasa GET custom_webhook_RasaChatInput.health /webhooks/rasa/webhook POST custom_webhook_RasaChatInput.receive /webhooks/rest GET custom_webhook_RestInput.health /webhooks/rest/webhook POST custom_webhook_RestInput.receive /model/test/intents POST evaluate_intents /model/test/stories POST evaluate_stories /conversations/<conversation_id>/execute POST execute_action /domain GET get_domain / GET hello /model PUT load_model /model/parse POST parse /conversations/<conversation_id>/predict POST predict /conversations/<conversation_id>/tracker/events PUT replace_events /conversations/<conversation_id>/story GET retrieve_story /conversations/<conversation_id>/tracker GET retrieve_tracker /status GET status /model/predict POST tracker_predict /model/train POST train /model DELETE unload_model /version GET version hi from name method 2020-01-23 13:30:37 INFO root - Starting Rasa server on http://localhost:5005 2020-01-23 13:30:37 DEBUG rasa.core.utils - Using the default number of Sanic workers (1). 2020-01-23 13:30:37 INFO root - Enabling coroutine debugging. Loop id 105198376. 2020-01-23 13:30:37 DEBUG rasa.model - Extracted model to ‘/tmp/tmpa6sqtcg1’. 2020-01-23 13:30:40 INFO absl - Entry Point [tensor2tensor.envs.tic_tac_toe_env:TicTacToeEnv] registered with id [T2TEnv-TicTacToeEnv-v0] 2020-01-23 13:30:40.503954: E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit: UNKNOWN ERROR (303) 2020-01-23 13:30:40 DEBUG rasa.core.tracker_store - Connected to InMemoryTrackerStore. 2020-01-23 13:30:40 DEBUG rasa.core.lock_store - Connected to lock store ‘InMemoryLockStore’. 2020-01-23 13:30:41 DEBUG rasa.model - Extracted model to ‘/tmp/tmp5imkw3y7’. 2020-01-23 13:30:41 DEBUG pykwalify.compat - Using yaml library: /home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/ruamel/yaml/init.py Inside receive User message ::- hi sender_id is ::- Me text is ::- hi 2020-01-23 13:33:13 ERROR asyncio - <CoroWrapper register..handler() running at /home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/channels/channel.py:81, created at /usr/lib/python3.6/asyncio/coroutines.py:85> was never yielded from Coroutine object created at (most recent call last, truncated to 10 last lines): File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/run.py”, line 206, in serve_application endpoints.lock_store if endpoints else None File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 1133, in run serve(**server_settings) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/server.py”, line 885, in serve loop.run_forever() File “/usr/lib/python3.6/asyncio/coroutines.py”, line 126, in send return self.gen.send(value) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/spf/framework.py”, line 512, in _handle_request stream_callback) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 942, in handle_request response = await response File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasabot/MyIo.py”, line 106, in receive on_new_message(UserMessage(text, collector, sender_id)) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 85, in debug_wrapper return CoroWrapper(gen, None) /home/gourab/rasa/rasabot/MyIo.py:106: RuntimeWarning: coroutine ‘register..handler’ was never awaited on_new_message(UserMessage(text, collector, sender_id)) collector MSG:: <rasa.core.channels.channel.CollectingOutputChannel object at 0x7f88b06a48d0> hi from name method 2020-01-23 13:33:13 DEBUG rasa.core.tracker_store - Creating a new tracker for id ‘Me’. 2020-01-23 13:33:13 DEBUG rasa.core.processor - Received user message ‘hi’ with intent ‘{‘name’: ‘greet’, ‘confidence’: 0.9369099140167236}’ and entities ‘[]’ 2020-01-23 13:33:13 DEBUG rasa.core.processor - Logged UserUtterance - tracker now has 2 events 2020-01-23 13:33:13 DEBUG rasa.core.policies.memoization - Current tracker state [None, None, None, {}, {‘prev_action_listen’: 1.0, ‘intent_greet’: 1.0}] 2020-01-23 13:33:13 DEBUG rasa.core.policies.memoization - There is a memorised next action ‘12’ 2020-01-23 13:33:14 DEBUG rasa.core.policies.form_policy - There is no active form 2020-01-23 13:33:14 DEBUG rasa.core.policies.ensemble - Predicted next action using policy_0_MemoizationPolicy 2020-01-23 13:33:14 DEBUG rasa.core.processor - Predicted next action ‘utter_greet’ with confidence 1.00. 2020-01-23 13:33:14 DEBUG rasa.core.processor - Action ‘utter_greet’ ended with events ‘[‘BotUttered(text: Hello I’m customer service assistant! How can I help you?, data: {“elements”: null, “quick_replies”: null, “buttons”: null, “attachment”: null, “image”: null, “custom”: null}, metadata: {})’]’ 2020-01-23 13:33:14 DEBUG rasa.core.policies.memoization - Current tracker state [None, None, {}, {‘prev_action_listen’: 1.0, ‘intent_greet’: 1.0}, {‘prev_utter_greet’: 1.0, ‘intent_greet’: 1.0}] 2020-01-23 13:33:14 DEBUG rasa.core.policies.memoization - There is a memorised next action ‘0’ 2020-01-23 13:33:14 DEBUG rasa.core.policies.mapping_policy - There is no mapped action for the predicted intent, ‘greet’. 2020-01-23 13:33:14 DEBUG rasa.core.policies.form_policy - There is no active form 2020-01-23 13:33:14 DEBUG rasa.core.policies.ensemble - Predicted next action using policy_0_MemoizationPolicy 2020-01-23 13:33:14 DEBUG rasa.core.processor - Predicted next action ‘action_listen’ with confidence 1.00. 2020-01-23 13:33:14 DEBUG rasa.core.processor - Action ‘action_listen’ ended with events ‘[]’ 2020-01-23 13:33:14 DEBUG rasa.core.lock_store - Deleted lock for conversation ‘Me’. Inside receive User message ::- i have a lenovo laptop sender_id is ::- Me text is ::- i have a lenovo laptop 2020-01-23 13:33:42 ERROR asyncio - <CoroWrapper register..handler() running at /home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/channels/channel.py:81, created at /usr/lib/python3.6/asyncio/coroutines.py:85> was never yielded from Coroutine object created at (most recent call last, truncated to 10 last lines): File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/run.py”, line 206, in serve_application endpoints.lock_store if endpoints else None File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 1133, in run serve(**server_settings) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/server.py”, line 885, in serve loop.run_forever() File “/usr/lib/python3.6/asyncio/coroutines.py”, line 126, in send return self.gen.send(value) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/spf/framework.py”, line 512, in _handle_request stream_callback) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 942, in handle_request response = await response File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasabot/MyIo.py”, line 106, in receive on_new_message(UserMessage(text, collector, sender_id)) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 85, in debug_wrapper return CoroWrapper(gen, None) collector MSG:: <rasa.core.channels.channel.CollectingOutputChannel object at 0x7f88b06a4470> hi from name method 2020-01-23 13:33:42 DEBUG rasa.core.tracker_store - Recreating tracker for id ‘Me’ 2020-01-23 13:33:42 DEBUG rasa.core.processor - Received user message ‘i have a lenovo laptop’ with intent ‘{‘name’: ‘details_provider’, ‘confidence’: 0.9882424473762512}’ and entities ‘[{‘start’: 9, ‘end’: 15, ‘value’: ‘lenovo’, ‘entity’: ‘brand’, ‘confidence’: 0.9828931437502632, ‘extractor’: ‘CRFEntityExtractor’}, {‘start’: 16, ‘end’: 22, ‘value’: ‘laptop’, ‘entity’: ‘item’, ‘confidence’: 0.9987055595943064, ‘extractor’: ‘CRFEntityExtractor’}]’ 2020-01-23 13:33:42 DEBUG rasa.core.processor - Current slot values: brand: lenovo issue_duration: None issue_location: None issue_nature: None item: laptop part: None requested_slot: None 2020-01-23 13:33:42 DEBUG rasa.core.processor - Logged UserUtterance - tracker now has 8 events 2020-01-23 13:33:42 DEBUG rasa.core.policies.memoization - Current tracker state [None, {}, {‘prev_action_listen’: 1.0, ‘intent_greet’: 1.0}, {‘prev_utter_greet’: 1.0, ‘intent_greet’: 1.0}, {‘prev_action_listen’: 1.0, ‘entity_brand’: 1.0, ‘entity_item’: 1.0, ‘intent_details_provider’: 1.0}] 2020-01-23 13:33:42 DEBUG rasa.core.policies.memoization - There is no memorised next action 2020-01-23 13:33:42 DEBUG rasa.core.policies.form_policy - There is no active form 2020-01-23 13:33:42 DEBUG rasa.core.policies.ensemble - Predicted next action using policy_1_KerasPolicy 2020-01-23 13:33:42 DEBUG rasa.core.processor - Predicted next action ‘issue_form’ with confidence 0.70. 2020-01-23 13:33:42 DEBUG rasa.core.actions.action - Calling action endpoint to run action ‘issue_form’. 2020-01-23 13:33:42 DEBUG rasa.core.processor - Action ‘issue_form’ ended with events ‘[‘BotUttered(text: which part of your laptop?, data: {“elements”: null, “quick_replies”: null, “buttons”: null, “attachment”: null, “image”: null, “custom”: null}, metadata: {“brand”: “lenovo”, “item”: “laptop”})’, ‘Form(issue_form)’, ‘SlotSet(key: item, value: laptop)’, ‘SlotSet(key: brand, value: lenovo)’, ‘SlotSet(key: requested_slot, value: part)’]’ 2020-01-23 13:33:42 DEBUG rasa.core.processor - Current slot values: brand: lenovo issue_duration: None issue_location: None issue_nature: None item: laptop part: None requested_slot: part 2020-01-23 13:33:42 DEBUG rasa.core.policies.memoization - Current tracker state [None, {}, {‘prev_action_listen’: 1.0, ‘intent_greet’: 1.0}, {‘prev_utter_greet’: 1.0, ‘intent_greet’: 1.0}, {‘prev_action_listen’: 1.0, ‘entity_brand’: 1.0, ‘entity_item’: 1.0, ‘intent_details_provider’: 1.0}] 2020-01-23 13:33:42 DEBUG rasa.core.policies.memoization - There is no memorised next action 2020-01-23 13:33:42 DEBUG rasa.core.policies.mapping_policy - There is no mapped action for the predicted intent, ‘details_provider’. 2020-01-23 13:33:42 DEBUG rasa.core.policies.form_policy - There is an active form ‘issue_form’ 2020-01-23 13:33:42 DEBUG rasa.core.policies.ensemble - Predicted next action using policy_3_FormPolicy 2020-01-23 13:33:42 DEBUG rasa.core.processor - Predicted next action ‘action_listen’ with confidence 1.00. 2020-01-23 13:33:42 DEBUG rasa.core.processor - Action ‘action_listen’ ended with events ‘[]’ 2020-01-23 13:33:42 DEBUG rasa.core.lock_store - Deleted lock for conversation ‘Me’. Inside receive User message ::- i have a acer laptop sender_id is ::- Me1 text is ::- i have a acer laptop 2020-01-23 13:34:06 ERROR asyncio - <CoroWrapper register..handler() running at /home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/channels/channel.py:81, created at /usr/lib/python3.6/asyncio/coroutines.py:85> was never yielded from Coroutine object created at (most recent call last, truncated to 10 last lines): File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/run.py”, line 206, in serve_application endpoints.lock_store if endpoints else None File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 1133, in run serve(**server_settings) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/server.py”, line 885, in serve loop.run_forever() File “/usr/lib/python3.6/asyncio/coroutines.py”, line 126, in send return self.gen.send(value) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/spf/framework.py”, line 512, in _handle_request stream_callback) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 942, in handle_request response = await response File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasabot/MyIo.py”, line 106, in receive on_new_message(UserMessage(text, collector, sender_id)) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 85, in debug_wrapper return CoroWrapper(gen, None) collector MSG:: <rasa.core.channels.channel.CollectingOutputChannel object at 0x7f894167aeb8> hi from name method

Now can you please help me why its showing the error, and how can I respond back to frond-end with not only just the string but also with some metadata like slotvalues, intent of the string etc…

2020-01-23 13:34:06 ERROR asyncio - <CoroWrapper register..handler() running at /home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/channels/channel.py:81, created at /usr/lib/python3.6/asyncio/coroutines.py:85> was never yielded from Coroutine object created at (most recent call last, truncated to 10 last lines): File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/rasa/core/run.py”, line 206, in serve_application endpoints.lock_store if endpoints else None File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 1133, in run serve(**server_settings) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/server.py”, line 885, in serve loop.run_forever() File “/usr/lib/python3.6/asyncio/coroutines.py”, line 126, in send return self.gen.send(value) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/spf/framework.py”, line 512, in _handle_request stream_callback) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasa-project-1/lib/python3.6/site-packages/sanic/app.py”, line 942, in handle_request response = await response File “/usr/lib/python3.6/asyncio/coroutines.py”, line 110, in next return self.gen.send(None) File “/home/gourab/rasa/rasabot/MyIo.py”, line 106, in receive on_new_message(UserMessage(text, collector, sender_id)) File “/usr/lib/python3.6/asyncio/coroutines.py”, line 85, in debug_wrapper return CoroWrapper(gen, None)

I get this error as mentioned earlier

I want to send these info to the API caller from front-end:

2020-01-23 13:35:24 DEBUG rasa.core.tracker_store - Recreating tracker for id ‘Me1’ 2020-01-23 13:35:24 DEBUG rasa.core.processor - Received user message ‘flickering’ with intent ‘{‘name’: ‘issue_provider’, ‘confidence’: 0.7306471467018127}’ and entities ‘[]’ 2020-01-23 13:35:24 DEBUG rasa.core.processor - Logged UserUtterance - tracker now has 19 events 2020-01-23 13:35:24 DEBUG rasa.core.policies.memoization - Current tracker state [None, None, None, {}, {‘prev_action_listen’: 1.0, ‘entity_brand’: 1.0, ‘entity_item’: 1.0, ‘intent_details_provider’: 1.0}] 2020-01-23 13:35:24 DEBUG rasa.core.policies.memoization - There is no memorised next action 2020-01-23 13:35:24 DEBUG rasa.core.policies.form_policy - There is an active form ‘issue_form’ 2020-01-23 13:35:24 DEBUG rasa.core.policies.ensemble - Predicted next action using policy_3_FormPolicy 2020-01-23 13:35:24 DEBUG rasa.core.processor - Predicted next action ‘issue_form’ with confidence 1.00. 2020-01-23 13:35:24 DEBUG rasa.core.actions.action - Calling action endpoint to run action ‘issue_form’. 2020-01-23 13:35:24 DEBUG rasa.core.processor - Action ‘issue_form’ ended with events ‘[‘BotUttered(text: Is the problem in the entire screen or only in parts of it?, data: {“elements”: null, “quick_replies”: null, “buttons”: [{“payload”: “entire”, “title”: “entire”}, {“payload”: “left-side”, “title”: “left-side”}, {“payload”: “right-side”, “title”: “right-side”}, {“payload”: “top-part”, “title”: “top-part”}, {“payload”: “bottom-part”, “title”: “bottom-part”}, {“payload”: “center”, “title”: “center”}], “attachment”: null, “image”: null, “custom”: null}, metadata: {“brand”: “acer”, “issue_nature”: “flickering”, “item”: “laptop”, “part”: “screen”, “requested_slot”: “issue_nature”})’, ‘SlotSet(key: issue_nature, value: flickering)’, ‘SlotSet(key: requested_slot, value: issue_location)’]’ 2020-01-23 13:35:24 DEBUG rasa.core.processor - Current slot values: brand: acer issue_duration: None issue_location: None issue_nature: flickering item: laptop part: screen requested_slot: issue_location

there is no method called send_message in that link…

my mistake, you want to override the method in the OutputChannel rasa/channel.py at master · RasaHQ/rasa · GitHub