I want to integrate a database and look up values based on an entity the user gave me. How is this possible? Is there a detailed guide anywhere to follow?
I am pretty new in the whole chatbot space. Sorry if this might seem like a naive question.
Hi,
I’m actually looking at the same thing. I didn’t find any detailed guides.
You have to do it using custom actions. Make APIs calls to access your database. I found some links which people have implemented. Try looking at them:
Hey,
thank you for your reply. Yeah I am having a bit troubles finding the correct API commands in order to retrieve Info from the database. I will have a look at the links maybe I can find something to find some orientation.
Thank you
I don’t think this is really Rasa-related since you can use pymysql (for MySQL obviously ) & others, or an ORM such as SQLAlchemy to do this. For example, for returning the first name of a person in a database from an “ID” entity, things could look like this:
def read(id_person: int) -> Person:
db = pymysql.connect(SERVER, USER, PASSWORD, DB)
cursor = db.cursor()
sql = "SELECT * FROM PERSON WHERE ID = '%d'" % (id_person)
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
first_name = row[1]
last_name = row[2]
age = row[3]
sex = row[4]
income = row[5]
person = Person(id_person, first_name, last_name, age, sex, income)
except:
print("Error fetching data.")
finally:
db.close()
return person
If the ID is in a slot, you can easily pass it to the read function using the tracker.get_slot function and then get the person object in return. Then using the dispatcher you could simply pass person.first_name in the response.
Hi @nico,
I have used APIs to access my database.
My data is in MongoDB and I used Flask and PyMongo to make my APIs. I am successfully able to access my database from the custom actions.
Like @alecigne said, you can directly access your database using pymysql or pymongo or any other. For small scale projects this menthod is fine, but if you plan to scale the it up, it is always better to use APIs to access your database.
I am a complete newbie, when programming with python. I am trying to implement a basic code to access the database like @alecigne suggested. I am having some troubles with the whole person = Person(…) part. I need to create a object for that somewhere right?
HI @nico, I am trying to build a demo chatbot using MySQL server. But, I am getting connectivity issues. Could you please help me out if you have already implemented it.
Below is error I am getting with RASA:
2019-03-11 11:50:13 DEBUG rasa_core.processor - Predicted next action ‘action_db’ with prob 1.00.
2019-03-11 11:50:13 DEBUG rasa_core.actions.action - Calling action endpoint to run action ‘action_db’.
2019-03-11 11:50:13 ERROR rasa_core.actions.action - Failed to run custom action ‘action_db’. Action server responded with a non 200 status code of 404. Make sure your action server properly runs actions and returns a 200 once the action is executed. Error: 404 Client Error: NOT FOUND for url: http://localhost:5055/webhook/
2019-03-11 11:50:13 ERROR rasa_core.processor - Encountered an exception while running action ‘action_db’. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code.
2019-03-11 11:50:13 DEBUG rasa_core.processor - Failed to execute custom action.
Hi @nico, I believe i’m in the same shoes as you were. I am working on a college project and want to integrate the database with a chatbot i am working on. Will you please share your experience it would be a great help.
Hii @alecigne i am using sql alachemy to store it in mysql db
import sys
import pymysql
from sqlalchemy import create_engine
user = ‘root’
password = ‘root’
host = ‘localhost’
database = ‘dab’