] How can you create your RASA custom Actions as Asynchronous Skip to main content

Featured

Let's Learn about RASA Policies

What are policies? How they affect your model? What is its importance? What are different policies present in RASA? These are some of the questions which comes in mind of any new RASA developer. We will be discussing answers of all these questions in detail. Before moving ahead to learn about policies, it will be helpful to have a look on rasa pipelines . What are policies in Rasa? Once we have correctly predicted our intents and entities, the model now has to identify the actions which are required to be performed in order to give a final output. Policies are used to decide which action needs to be performed at each step in a conversation. There are machine Learning and Rule Based policies which your assistant can use. While predicting the next action, RASA uses intents, entities and the conversation done so far. RASA Prediction You can add policies in config.yml file under the keyword policies . These policies along with the Intents, Entities and conversation done so far will assign

How can you create your RASA custom Actions as Asynchronous

In this current era of Artificial Intelligence, lots of businesses are shifting towards developing chatbots. Chatbots are fast and they also give you a sense of belongingness. 

There are lots of framework which can help you with chatbot development, RASA is one of them. RASA is an open source framework to build Chatbots. To make bots more versatile we usually have to create multiple intents, entities and custom actions. Once your bot expands its scope, there are chances that the number of users increase drastically. This can sometimes result in slower response time from your bots. To make sure that your bot is processing every response in parallel you have to make your custom actions ASYNCHRONOUS.


Lets see how we can make our RASA custom actions async...

To achieve this we just have to follow two steps.

  • Add the async keyword in front of run method of your custom action
class ActionMyCustomAction(Action):
    def name(self):
        return "action_my_custom_action"
    async def run(self, dispatcher, tracker, domain):
        '''
         write your code here
        '''
        return []                         

Here we have just created our run function as asynchronous.
  • Make all function calls and API calls as asynchronous from this run method of your action.
    • To create function calls as asynchronous you have to make the function asynchronous by adding async keyword. Then use await keyword to call that function. 
      • Example:
                await asyncio.sleep(5)

    • To create API calls as asynchronous, you can use aiohttp library.
      • Following is an example of my code:
import asyncio from aiohttp 
import ClientSession 
async with ClientSession(headers=headers) as session: 
    async with session.post(url=url, json={"message": "hello"}) as response: 
        response = await response.text()

    For better understanding of aiohttp click here. 


I hope this article helped. Please comment if you have any doubt. I'll try my best to resolve it. 

To understand more about rasa click here.

Happy coding!!!

Comments

  1. Hey, I am using your given approach for makinbg function calling as async but when I call API it will taking time to fetch data in 2 to 3 minutes but I am getting this "" ERROR rasa_sdk.endpoint - Exception occurred during execution of request
    sanic.exceptions.ServiceUnavailable: Response Timeout ""' in 1 minutes or before it. So please could you give some solution for this.

    ReplyDelete

Post a Comment

Popular Posts