Rasa: Issue with database and RASA connection

Created on 11 Mar 2019  路  11Comments  路  Source: RasaHQ/rasa

Rasa Core version: 0.13.0

Python version: 0.14.1

Operating system (windows, osx, ...): Linux

Issue:
I am facing some issues while trying to fetch data from MySQL database.

I am getting an error as follows:

2019-03-11 17:37:50 DEBUG rasa_core.processor - Predicted next action 'action_db' with prob 1.00.
2019-03-11 17:37:50 DEBUG rasa_core.actions.action - Calling action endpoint to run action 'action_db'.
2019-03-11 17:37:50 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 17:37:50 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 17:37:50 DEBUG rasa_core.processor - Failed to execute custom action.

Content of domain file (if used & relevant):

slots:
  personid:
    type: text
  stockid:
    type: text
  matches:
    type: unfeaturized

intents:
 - greet
 - bye
 - affirmative
 - negative
 - personid
 - stockid
 - inform

entities:
 - personid
 - stockid

templates:
  utter_greet:
    - 'Hello! How can I help?'
  utter_goodbye:
    - 'Talk to you later.'
    - 'Bye bye :('
  utter_ask_personid:
    - 'Tell me your id please'
    - 'May I know your id please?'
  utter_ask_stock:
    - 'Tell me the stock name please'
    - 'Could you please tell me the stock name?'

actions:
 - utter_greet
 - utter_goodbye
 - utter_ask_stock
 - utter_ask_personid
 - action_db

Content of action file :

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from rasa_core_sdk import Action
from rasa_core_sdk.events import SlotSet

import MySQLdb

class ActionDb(Action):
    def name(self):
        return "action_db"

    def run(self, dispatcher, tracker, domain): 
        db = MySQLdb.connect("localhost","root","root","DBforChatbot")
        cursor = db.cursor()
        PersonID = tracker.get_slot('personid')
        StockID = tracker.get_slot('stockid')
        q = "select StockName, StockBalance from Persons WHERE (PersonID = '%d' and StockName = '%s');" % (PersonID, StockID)
        try:
            cursor.execute(q)
            if cursor.execute(q)==0:
                print("Sorry, could not find any relevant data. Please contact 1234 for further assistance.")
            results = cursor.fetchall()
            not all(results)
            for row in results:
                stockname = row[0]
                stockbal = row[1]
                # Now print fetched result
                details = ("stockname=%s,stockbal=%d" % (stockname,stockbal))
                print(details)
        except:
            print ("Error: unable to fetch data")

        db.close()
        response = """The detail is as follows {}.""".format(results)

        dispatcher.utter_message(response)
        return [SlotSet('stockid',StockID)]

Content of endpoints file (if used & relevant):

action_endpoint:
  url: "http://localhost:5055/webhook/"

Content of dialogue_management_model file:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import rasa_core
from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils import EndpointConfig
from rasa_core.run import serve_application
from rasa_core import config

logger = logging.getLogger(__name__)


def train_dialogue(domain_file = 'testbot_domain.yml',
                    model_path = './models/dialogue',
                    training_data_file = './data/stories.md'):

    agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50)])
    data = agent.load_data(training_data_file)  


    agent.train(data)

    agent.persist(model_path)
    return agent

def run_test_bot(serve_forever=True):
    interpreter = RasaNLUInterpreter('./models/nlu/default/testbotnlu')
    action_endpoint = EndpointConfig(url="http://localhost:5055/webhook/")
    agent = Agent.load('./models/dialogue', interpreter=interpreter, action_endpoint=action_endpoint)
    rasa_core.run.serve_application(agent ,channel='cmdline')

    return agent

if __name__ == '__main__':
    train_dialogue()
    run_test_bot()

Hi @tmbo @akelad @EPedrotti , could you please help here. Thanks.

All 11 comments

Did you run the actions server before starting interactive mode?

python -m rasa_core_sdk.endpoint --actions actions

https://rasa.com/docs/core/customactions/#custom-actions-written-in-python

@ortsaCniveK Yes, I ran it. But, I did not run it in interactive mode. I just trained it using rasa nlu and core commands as follows:

python3 nlu_model.py

python3 -m rasa_core.train -d testbot_domain.yml -s data/stories.md -o models/dialogue/

python3 -m rasa_core.run -d models/dialogue -u models/nlu/default/testbotnlu --endpoints endpoints.yml --debug --enable_api

You'll want to have the actions endpoint running in order for interactive mode to establish connection to it, which will allow for action_db to work. That's why it's 404'ing.

so running inteactive mode would be likely similar to below from here:

python -m rasa_core_sdk.endpoint --actions actions& # runs action server in the background

python -m rasa_core.train \
  interactive -o models/dialogue \
  -d domain.yml -c policy_config.yml \
  -s data/stories.md \
  --nlu models/default/testbotnlu \
  --endpoints endpoints.yml # starts interactive mode, pointing to the running action server

I have executed all the commands which you have mentioned (though in my case, it was:

python3 -m rasa_core_sdk.endpoint --actions action, as my action file's name is action.py).

I am not using interactive training to train the dialogue model. I am running the below commands to train and run code:

python3 -m rasa_core.train -d testbot_domain.yml -s data/stories.md -o models/dialogue/

python3 -m rasa_core.run -d models/dialogue -u models/nlu/default/testbotnlu --endpoints endpoints.yml --debug --enable_api

I do not know, if I am right or not, but my rasa server is running on port 5055 and my sql server is running on port 3306. I have specied rasa's port in endpoints.yml file but do I have to specify sql's port as well?

Thanks

Below are my story and training data (for reference):

Stories:

## Generated Story 1
* greet
    - utter_greet
* inform
    - utter_ask_personid
* personid{"personid": "1"}
    - utter_ask_stock
* stockid{"stockid": "SB123"}
    - action_db
* bye
    - utter_goodbye

data.json (training data):

    {
        "rasa_nlu_data": {
            "regex_features": [],
            "entity_synonyms": [],
            "common_examples": [
                {
                    "text": "hello",
                    "intent": "greet",
                    "entities": []
                },
                {
                    "text": "howdy",
                    "intent": "greet",
                    "entities": []
                },
                {
                    "text": "hola",
                    "intent": "greet",
                    "entities": []
                },
                {
                    "text": "adieu",
                    "intent": "bye",
                    "entities": []
                },
                {
                    "text": "goodbye",
                    "intent": "bye",
                    "entities": []
                },
                {
                    "text": "gtg",
                    "intent": "bye",
                    "entities": []
                },
                {
                    "text": "nope",
                    "intent": "negative",
                    "entities": []
                },
                {
                    "text": "no",
                    "intent": "negative",
                    "entities": []
                },
                {
                    "text": "no, thanks",
                    "intent": "negative",
                    "entities": []
                },
                {
                    "text": "si",
                    "intent": "affirm",
                    "entities": []
                },
                {
                    "text": "ok",
                    "intent": "affirm",
                    "entities": []
                },
                {
                    "text": "right",
                    "intent": "affirm",
                    "entities": []
                },
                {
                    "text": "hola ]I would like to know the total stk bal for the stocks ST4053.",
                    "intent": "inform",
                    "entities": [
                        {
                            "end": 66,
                            "entity": "stockid",
                            "start": 60,
                            "value": "ST4053"
                        }
                    ]
                },
                {
                    "text": "Tell me the stock balance for stocks SB123",
                    "intent": "inform",
                    "entities": [
                        {
                            "end": 42,
                            "entity": "stockid",
                            "start": 37,
                            "value": "SB123"
                        }
                    ]
                },
                {
                    "text": "I would like to know the total stk blnc for the stock AB1233.",
                    "intent": "inform",
                    "entities": [
                        {
                            "end": 60,
                            "entity": "stockid",
                            "start": 54,
                            "value": "AB1233"
                        }
                    ]
                },
                {
                    "text": "thank you",
                    "intent": "thanks",
                    "entities": []
                },
                {
                    "text": "Thank you",
                    "intent": "thanks",
                    "entities": []
                },
                {
                    "text": "Thanks",
                    "intent": "thanks",
                    "entities": []
                },
                {
                    "text": "1",
                    "intent": "personid",
                    "entities": [
                        {
                            "end": 1,
                            "entity": "personid",
                            "start": 0,
                            "value": "1"
                        }
                    ]
                },
                {
                    "text": "its 2",
                    "intent": "personid",
                    "entities": [
                        {
                            "end": 5,
                            "entity": "personid",
                            "start": 4,
                            "value": "2"
                        }
                    ]
                },
                {
                    "text": "it is 3",
                    "intent": "personid",
                    "entities": [
                        {
                            "end": 7,
                            "entity": "personid",
                            "start": 6,
                            "value": "3"
                        }
                    ]
                },
                {
                    "text": "it is ST4053",
                    "intent": "stockid",
                    "entities": [
                        {
                            "end": 12,
                            "entity": "stockid",
                            "start": 6,
                            "value": "ST4053"
                        }
                    ]
                },
                {
                    "text": "its AB1233",
                    "intent": "stockid",
                    "entities": [
                        {
                            "end": 10,
                            "entity": "stockid",
                            "start": 4,
                            "value": "AB1233"
                        }
                    ]
                },
                {
                    "text": "ST4053",
                    "intent": "stockid",
                    "entities": [
                        {
                            "end": 6,
                            "entity": "stockid",
                            "start": 0,
                            "value": "ST4053"
                        }
                    ]
                },
                {
                    "text": "hi",
                    "intent": "greet",
                    "entities": []
                },
                {
                    "text": "bye",
                    "intent": "bye",
                    "entities": []
                },
                {
                    "text": "no thanks",
                    "intent": "negative",
                    "entities": []
                },
                {
                    "text": "yes to that",
                    "intent": "affirm",
                    "entities": []
                },
                {
                    "text": "I wanna know about the total number of stock AB1233 in my account.",
                    "intent": "inform",
                    "entities": [
                        {
                            "end": 51,
                            "entity": "stockid",
                            "start": 45,
                            "value": "AB1233"
                        }
                    ]
                },
                {
                    "text": "tnxs",
                    "intent": "thanks",
                    "entities": []
                },
                {
                    "text": "it is 2",
                    "intent": "personid",
                    "entities": [
                        {
                            "end": 7,
                            "entity": "personid",
                            "start": 6,
                            "value": "2"
                        }
                    ]
                },
                {
                    "text": "its SB-123",
                    "intent": "stockid",
                    "entities": [
                        {
                            "end": 10,
                            "entity": "stockid",
                            "start": 4,
                            "value": "SB-123"
                        }
                    ]
                }
            ]
        }
    }

MySQL database and table (for reference):

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| DBforChatbot       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use DBforChatbot;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from Persons;
+----------+----------+-----------+-----------+--------------+
| PersonID | LastName | FirstName | StockName | StockBalance |
+----------+----------+-----------+-----------+--------------+
|        2 | Anand    | Utsav     | SB-123    |       231045 |
|        1 | Anand    | Ujjwal    | SB123     |       231095 |
|        1 | Anand    | Ujjwal    | sb123     |       231095 |
+----------+----------+-----------+-----------+--------------+
3 rows in set (0.00 sec)

Can you post the log of the action server?

Hi @akelad , thanks for quick response.

Below are the logs of action server (now I am getting error 500):

 python3 -m rasa_core_sdk.endpoint --actions action
2019-03-12 14:33:54 INFO     __main__  - Starting action endpoint server...
2019-03-12 14:33:54 INFO     rasa_core_sdk.executor  - Registered function for 'action_db'.
2019-03-12 14:33:54 INFO     __main__  - Action endpoint is up and running. on ('0.0.0.0', 5055)
[2019-03-12 14:35:52,091] ERROR in app: Exception on /webhook [POST]
Traceback (most recent call last):
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
    resp = make_response(f(*args, **kwargs))
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/rasa_core_sdk/endpoint.py", line 86, in webhook
    response = executor.run(action_call)
  File "/home/xmplar/anaconda3/lib/python3.6/site-packages/rasa_core_sdk/executor.py", line 177, in run
    events = action(dispatcher, tracker, domain)
  File "/home/xmplar/xfact/ChatBot/RasaBot/examples/testbot/action.py", line 73, in run
    q = "select StockName, StockBalance from Persons WHERE (PersonID = '%d' and StockName = '%s');" % (PersonID, StockID)
TypeError: %d format: a number is required, not str
127.0.0.1 - - [2019-03-12 14:35:52] "POST /webhook HTTP/1.1" 500 412 0.012302


Thanks!

Thanks a lot @akelad ! After I posted the logs, I noticed that, it was because of type conversion, my bad!

Hi Ujjwalsas9 can you elaborate what you fixed to get this resolved.

Can @Ujjwalsas9 please tell me what you fixed in this? I am doing similar kind of thing.

Was this page helpful?
0 / 5 - 0 ratings