Need help to connect sql database from phpmyadmin to my rasa chatbot in flask app

i have file named app.py which contain the flask app of my rasa chatbot , , so i want to connect my sql database named backup from phpmyadmin to my rasa chatbot , this database contain the responses of our chatbot andgive me the code to define the questions of users as intents in nlu.yml file , give me all the codes that will be able to connect my rasa chatbot in flsask app to our database as responses of our chatbot with the intents as questions of users that is gives the possibility to fetch and display the content of column nom of our table atsufas1 from our database

To connect your Rasa chatbot Flask app to your MySQL database and fetch the responses based on the user’s intents, you can follow these steps:

  • Install the required dependencies:
  1. Flask: pip install flask
  2. PyMySQL (for interacting with the MySQL database): pip install pymysql
  • Update your app.py file:
from flask import Flask, request, jsonify
import pymysql

app = Flask(__name__)

# MySQL connection details
db_host = 'localhost'
db_user = 'your_username'
db_password = 'your_password'
db_name = 'backup'

# Connect to the MySQL database
connection = pymysql.connect(
    host=db_host,
    user=db_user,
    password=db_password,
    db=db_name
)

@app.route('/chat', methods=['POST'])
def chat():
    # Get the user's message from the request
    message = request.json['message']

    # Query the database to fetch the response
    with connection.cursor() as cursor:
        sql = "SELECT nom FROM atsufas1 WHERE message = %s"
        cursor.execute(sql, (message,))
        result = cursor.fetchone()
        if result:
            response = result[0]
        else:
            response = "I'm sorry, I don't have a response for that."

    return jsonify({'response': response})

if __name__ == '__main__':
    app.run()
  • Update your nlu.yml file:
version: "3.1"

nlu:
- intent: greet
  examples: |
    - hey
    - hello
    - hi
    - good morning
    - good evening
    - hey there

- intent: goodbye
  examples: |
    - bye
    - goodbye
    - see you later
    - catch you later

- intent: ask_name
  examples: |
    - what is your name
    - who are you
    - what should i call you

# Add more intents here, for example:
# - intent: ask_weather
#   examples: |
#     - what's the weather like
#     - how's the weather today
#     - is it raining outside
  • Update your Rasa model:

After making changes to your nlu.yml file, you’ll need to update your Rasa model. You can do this by running the following command in your terminal:

rasa train

Now, when a user sends a message to your Rasa chatbot, the Flask app will query the MySQL database to fetch the corresponding response based on the user’s intent (defined in the nlu.yml file). The response is then returned to the user.

Note that you’ll need to replace the KrogerFeedback MySQL connection details (db_host, db_user, db_password, db_name) with your own values. Also, make sure the atsufas1 table in your backup database has a column named nom that contains the response text.

If you have any further questions or issues, feel free to ask!

1 Like

thank you soo much for this help , but i have another question how to make liaison between the query in the code app.py file and the intent which represent the question of the user

i have some issue to make create intents to ask about the content of column email according to the content of column nom and column Prenom and make this intent related with the query of app.py file