How to Send/Receive request/response to RASA using Flask

Hi

I am trying to create a simple GUI using the flask framework. I am using rasa_sdk and using my custom actions in it. Everything works fine from command line with my new stories.md and nlu.md. Now, I want to replace that command line with a simple GUI to mimic a chat application. I have read blogs and of course the RASA community, but, I am not able to do it.

Below are the commands I am using to train and test the model.

python -m rasa_nlu.train -c nlu_config.yml --data data/nlu_new.md -o models --fixed_model_name nlu --project rasbot --verbose

python -m rasa_core.train -d domain.yml -s data/stories_new.md --nlu_threshold 0.5 --core_threshold 0.4 --fallback_action_name action_not_defined -o models/rasbot/dialogue --epochs 200

python -m rasa_core_sdk.endpoint --actions actions&

and

python -m rasa_core.run -d models/rasbot/dialogue -u models/rasbot/nlu --port 5002 --credentials credentials.yml as suggested here

In my project folder I have a sub-folder named “gui”, which has main.py in it and another sub-folder named templates. Below is how my main.py looks:

from flask import Flask, render_template, request, jsonify
import requests
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app)

@app.route('/', methods = ['GET'])
def loadPage():
	return render_template("session.html")

@app.route('/chat',methods = ['POST', 'GET'])
def session():
	try:
		msg = request.form['user_input']
		print(type(msg))
		
		response = requests.post('http://localhost:5002/parse', params={'q':msg})
		response = response.json()
		entities = response.get("entities")
		topresponse = response["topScoringIntent"]
		intent = topresponse.get("intent")
		print("Intent {}, Entities {}".format(intent,entities))
		if intent == 'greet':
			return jsonify({"status":"success","response":"YAYYYYYYYYYYYYYY!"})
	except Exception as e:
		print(e)
		return jsonify({"status":"success","response":"Sorry no matching intent..."})
		
if __name__ == '__main__':
    app.run(debug=True)

My request/response sequence is as follows: I build both rasa nlu and rasa core before following the below steps.

  1. I run “python main.py” which starts the server and @localhost:5000 I can see my xyz.html page loaded.

  2. Then, I try to make a post request using the above code from **“main.py(pasted above)”**to localhost:5002 where my rasa core server is running by entering “Hi bot” and submit from my html page. I get the following error: error.

My questions are:

  1. Is it the right way to do, to connect GUI to rasa sdk?
  2. Am I forming the URL the right way?
  3. What should be the requests form (post or get) to chat with the bot?
  4. Is it the right way to send a message “Hi” from GUI which first goes to localhost:5000 and from there I make a GET/POST request to localhost:5002 to rasa core server?

Thanks

I can’t see your error. Also what’s in your credentials, are you using the rest input channel?

I’m facing a somewhat similar issue. Were you able to figure it out?

1 Like

i get
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
did anyone find a solution to that problem ?

Even I am facing same issue with rasa 1.0.1 while training and app is running on Docker. I am using rasa core + nlu.

Can anyone help?

1 Like

Did you get some answer to your queries? I am also trying to do the same and having hard time with building connector.

1 Like

I am also trying to connect my Flask application with RASA. but could not. Please help, if you find any solution.

Hello @Nehasingh1300 and everyone, you can check out this old github repo GitHub - bhavaniravi/rasa-site-bot: A chatbot that fetches events details from a conference's website

I think changing the endpoints in the flask run bot to your local host or to server will work properly in today setup

1 Like