Here, I assume you have installed the Rasa server by following the link https://learning.rasa.com/installation/ubuntu/
Also install Spacy the required thing to classify Name of person and many more entities it can detect. You can follow link, https://rasa.com/docs/rasa/installation/installing-rasa-open-source/#dependencies-for-spacy to install it.
Once you have installed Rasa you can clone my repository located at, https://github.com/ankitjayswal87/VoiceBot and play with my rasa code to check appointment booking demo. Also here I will show you how we can pass text input to Rasa server via API requests and get response for intent/entity classification. This response we can use to take decision for the next action.
I hope you created virtual environment and installed Rasa inside it.
Let’s first activate the venv with typing below command on the terminal.
source ./rasa-venv/bin/activate
Go inside the rasa project folder. (You can create your own rasa project folder here with command mkdir test-rasa and do rasa init command inside this new folder to initialize the new project OR you can use my rasa-projects folder available at Github code). Here, I have already created my folder and initialized the project so switching to that directory as below.
cd rasa-projects
Now, if we have done changes in the project we can train the model via command below and that will create one file under folder models (something like 20230210-113625-lime-primer.tar.gz) For the fresh project this step required.
rasa train
Now, next step is to start the Rasa server in the API mode. To start the server run below command on terminal.
rasa run –enable-api
Great! now our Rasa server is running in API mode and we can hit API request on it.
INTENT AND ENTITY CLASSIFICATION API:
Let’s say ‘hi’ to the Rasa server via below curl request. In below response the greet intent is classified.
REQUEST:
curl -XPOST http://localhost:5005/model/parse -s -d ‘{ “text”: “hi” }’
RESPONSE:
{“text”:”hi”,”intent“:{“name”:”greet“,”confidence”:0.9965554475784302},”entities”:[],”text_tokens”:[[0,2]],”intent_ranking”:[{“name”:”greet”,”confidence”:0.9965554475784302},{“name”:”mobile_number”,”confidence”:0.0017740302719175816},{“name”:”mood_great”,”confidence”:0.00039736428880132735},{“name”:”goodbye”,”confidence”:0.0003147204697597772},{“name”:”bot_challenge”,”confidence”:0.00020684130140580237},{“name”:”mood_unhappy”,”confidence”:0.00017661831225268543},{“name”:”say_name”,”confidence”:0.00015119690215215087},{“name”:”appointment_time”,”confidence”:0.00013099773786962032},{“name”:”deny”,”confidence”:0.0001235026866197586},{“name”:”book_appointment”,”confidence”:7.637307135155424e-5}]}
Let’s say, ‘my number is 9979270000’
REQUEST:
curl -XPOST http://localhost:5005/model/parse -s -d ‘{ “text”: “my number is 9979270000” }’
RESPONSE:
{“text”:”my number is 9979270000″,”intent”:{“name”:”mobile_number“,”confidence”:0.849086582660675},”entities”:[{“entity”:”number“,”start”:13,”end”:23,”confidence_entity”:0.9989855885505676,”value”:”9979270000″,”extractor”:”DIETClassifier”}],”text_tokens”:[[0,2],[3,9],[10,12],[13,23]],”intent_ranking”:[{“name”:”mobile_number”,”confidence”:0.849086582660675},{“name”:”my_size”,”confidence”:0.04768197983503342},{“name”:”mood_unhappy”,”confidence”:0.036454521119594574},{“name”:”deny”,”confidence”:0.03295041248202324},{“name”:”appointment_time”,”confidence”:0.01802949234843254},{“name”:”greet”,”confidence”:0.006991012487560511},{“name”:”say_name”,”confidence”:0.003854062408208847},{“name”:”book_appointment”,”confidence”:0.0018745406996458769},{“name”:”my_name”,”confidence”:0.0013263542205095291},{“name”:”mood_great”,”confidence”:0.0006634475430473685}]}
Let’s say ‘my name is ankit’. We can see it has detected intent my_name, entity PERSON and entity value as ankit
REQUEST:
curl -XPOST http://localhost:5005/model/parse -s -d ‘{ “text”: “people call me ankit” }’
RESPONSE:
{“text”:”people call me ankit”,”intent”:{“name”:”my_name“,”confidence”:0.7218146324157715},”entities”:[{“entity”:”PERSON“,”value”:”ankit“,”start”:15,”confidence”:null,”end”:20,”extractor”:”SpacyEntityExtractor”},{“entity”:”PERSON”,”start”:15,”end”:20,”confidence_entity”:0.9989639520645142,”value”:”ankit”,”extractor”:”DIETClassifier”}],”text_tokens”:[[0,6],[7,11],[12,14],[15,20]],”intent_ranking”:[{“name”:”my_name”,”confidence”:0.7218146324157715},{“name”:”appointment_time”,”confidence”:0.09681735187768936},{“name”:”deny”,”confidence”:0.04496312513947487},{“name”:”bot_challenge”,”confidence”:0.030082207173109055},{“name”:”request_restaurant”,”confidence”:0.02007092908024788},{“name”:”affirm”,”confidence”:0.01739741675555706},{“name”:”say_name”,”confidence”:0.01582713983952999},{“name”:”goodbye”,”confidence”:0.015156108886003494},{“name”:”out_of_scope”,”confidence”:0.012605207040905952},{“name”:”book_appointment”,”confidence”:0.011155341751873493}]}
Let’s say ‘my size is small’. We can see here it has detected my_size intent, entity as size and entity value as small.
REQUEST:
curl -XPOST http://localhost:5005/model/parse -s -d ‘{ “text”: “my size is small” }’
RESPONSE:
{“text”:”my size is small”,”intent”:{“name”:”my_size“,”confidence”:0.7397687435150146},”entities”:[{“entity”:”size“,”start”:11,”end”:16,”value”:”small“,”extractor”:”RegexEntityExtractor”}],”text_tokens”:[[0,2],[3,7],[8,10],[11,16]],”intent_ranking”:[{“name”:”my_size”,”confidence”:0.7397687435150146},{“name”:”mobile_number”,”confidence”:0.06258536130189896},{“name”:”mood_unhappy”,”confidence”:0.05957704409956932},{“name”:”say_name”,”confidence”:0.03620059788227081},{“name”:”affirm”,”confidence”:0.01866174302995205},{“name”:”appointment_time”,”confidence”:0.012721167877316475},{“name”:”mood_great”,”confidence”:0.012413665652275085},{“name”:”my_name”,”confidence”:0.011516008526086807},{“name”:”deny”,”confidence”:0.01118539646267891},{“name”:”greet”,”confidence”:0.009906858205795288}]}
Let’s say “i want to play football” and observe what reply we get. Here we can see it has classified mood_greet intent but at very low level of confidence. Such classified intents with very low level of confidence we should ignore. It is showing like we do not have the intent specified which can relates to user input.
REQUEST:
curl -XPOST http://localhost:5005/model/parse -s -d ‘{ “text”: “i want to play football” }’
RESPONSE:
{“text”:”i want to play football”,”intent”:{“name”:”mood_great“,”confidence”:0.3442094624042511},”entities”:[],”text_tokens”:[[0,1],[2,6],[7,9],[10,14],[15,23]],”intent_ranking”:[{“name”:”mood_great”,”confidence”:0.3442094624042511},{“name”:”bot_challenge”,”confidence”:0.13785406947135925},{“name”:”request_restaurant”,”confidence”:0.13730378448963165},{“name”:”greet”,”confidence”:0.11971883475780487},{“name”:”book_appointment”,”confidence”:0.079447440803051},{“name”:”out_of_scope”,”confidence”:0.05542973428964615},{“name”:”goodbye”,”confidence”:0.05150827020406723},{“name”:”mood_unhappy”,”confidence”:0.0229413490742445},{“name”:”affirm”,”confidence”:0.011725005693733692},{“name”:”my_name”,”confidence”:0.01068874355405569}]}
Sometimes user saying completely different thing that our Bot is not able to handle this. For example “sachin is my favorite batsman” , this time the intent detected is nlu_fallback which means Bot is not able to find any particular intent.
REQUEST:
curl -XPOST http://localhost:5005/model/parse -s -d ‘{ “text”: “sachin is my favorite batsman” }’
RESPONSE:
{“text”:”sachin is my favorite batsman”,”intent”:{“name”:”nlu_fallback“,”confidence”:0.3},”entities”:[],”text_tokens”:[[0,6],[7,9],[10,12],[13,21],[22,29]],”intent_ranking”:[{“name”:”nlu_fallback”,”confidence”:0.3},{“name”:”my_size”,”confidence”:0.3034037947654724},{“name”:”out_of_scope”,”confidence”:0.20758606493473053},{“name”:”appointment_time”,”confidence”:0.1743306666612625},{“name”:”request_restaurant”,”confidence”:0.08736144751310349},{“name”:”mobile_number”,”confidence”:0.08299102634191513},{“name”:”mood_unhappy”,”confidence”:0.030642300844192505},{“name”:”my_name”,”confidence”:0.02442915178835392},{“name”:”affirm”,”confidence”:0.021201880648732185},{“name”:”deny”,”confidence”:0.016715576872229576},{“name”:”say_name”,”confidence”:0.012855641543865204}]}
BOT RESPONSE API:
Let’s ask to Bot “what is your name?” , in this case we will have list of responses available which we trained.
REQUEST:
curl –request POST –url http://localhost:5005/webhooks/rest/webhook –header ‘content-type: application/json’ –data ‘{“message”: “what is your name?”}’
RESPONSE:
[{“recipient_id”:”default”,”text”:”My name is Rasa Bot Advanced…”},{“recipient_id”:”default”,”text”:”What is your name?”}]