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…