JustinaPetr / Weatherbot_Tutorial

275 stars 447 forks source link

TypeError: <rasa_core.events.AllSlotsReset object at 0x0000019E62DFE4A8> is not JSON serializable #40

Closed Namnt3 closed 5 years ago

Namnt3 commented 5 years ago

Rasa Core version: 0.11.3

Python version: Python 3.6.5

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

Issue:

I configured a custom action to automatically reset slots and restart conversation after a story ends, but whenever I call this action, below error appears:

====Log from training script:=============

ERROR:rasa_core.actions.action:Failed to run custom action 'action_slot_reset'. Action server responded with a non 200 status code of 500. Make sure your action server properly runs actions and returns a 200 once the action is executed. Error: 500 Server Error: INTERNAL SERVER ERROR for url: http://localhost:5055/webhook ERROR:rasa_core.processor:Encountered an exception while running action 'action_slot_reset'. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code. ERROR:rasa_core.processor:Failed to execute custom action. Traceback (most recent call last): File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa_core\actions\action.py", line 313, in run response.raise_for_status() File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\models.py", line 939, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 500 Server Error: INTERNAL SERVER ERROR for url: http://localhost:5055/webhook

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa_core\processor.py", line 319, in _run_action events = action.run(dispatcher, tracker, self.domain) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa_core\actions\action.py", line 334, in run raise Exception("Failed to execute custom action.") Exception: Failed to execute custom action. 127.0.0.1 - - [2018-10-22 09:28:26] "POST /conversations/default/execute HTTP/1.1" 200 4045 1.070108 127.0.0.1 - - [2018-10-22 09:28:27] "POST /conversations/default/predict HTTP/1.1" 200 13943 0.020988

====Log from running custom action server:===

INFO:main:Starting action endpoint server... INFO:rasa_core_sdk.executor:Registered function for 'action_restarted'. INFO:rasa_core_sdk.executor:Registered function for 'action_slot_reset'. INFO:main:Action endpoint is up and running. on ('0.0.0.0', 5055) DEBUG:rasa_core_sdk.executor:Received request to run 'action_slot_reset' DEBUG:rasa_core_sdk.executor:Successfully ran 'action_slot_reset' ERROR:flask.app:Exception on /webhook [POST] Traceback (most recent call last): File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function return cors_after_request(app.make_response(f(args, kwargs))) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_compat.py", line 35, in reraise raise value File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](req.view_args) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_cors\decorator.py", line 128, in wrapped_function resp = make_response(f(args, kwargs)) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa_core_sdk\endpoint.py", line 74, in webhook return jsonify(response) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\json__init.py", line 321, in jsonify dumps(data, indent=indent, separators=separators) + '\n', File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\json\init__.py", line 179, in dumps rv = _json.dumps(obj, kwargs) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson__init__.py", line 397, in dumps **kw).encode(obj) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\encoder.py", line 291, in encode chunks = self.iterencode(o, _one_shot=True) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\encoder.py", line 373, in iterencode return _iterencode(o, 0) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\json__init__.py", line 81, in default return _json.JSONEncoder.default(self, o) File "C:\Users\namnt\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\encoder.py", line 268, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: <rasa_core.events.AllSlotsReset object at 0x0000019E62DFE4A8> is not JSON serializable 127.0.0.1 - - [2018-10-22 09:28:26] "POST /webhook HTTP/1.1" 500 412 0.036980

===actionsEVN.py:===

from rasa_core.events import AllSlotsReset
from rasa_core.events import Restarted
from rasa_core_sdk import Action

class ActionRestarted(Action):
    def name(self):
        return 'action_restarted'
    def run(self, dispatcher, tracker, domain):
        return [Restarted()]

class ActionSlotReset(Action):
    def name(self):
        return 'action_slot_reset'
    def run(self, dispatcher, tracker, domain):
        return [AllSlotsReset()]

Content of endpoint file:

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

script training online I used:

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

import logging
import json
from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.policies.fallback import FallbackPolicy
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.train import online
from rasa_core.utils import EndpointConfig

logger = logging.getLogger(__name__)

fallback = FallbackPolicy(fallback_action_name="utter_unclear",
                          core_threshold=0.2,
                          nlu_threshold=0.6)
def run_EVN_online(interpreter,
                          domain_file="EVN_domain.yml",
                          training_data_file='data/storiesEVN.md'):
    action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
    agent = Agent(domain_file,
                  policies=[fallback,
                            MemoizationPolicy(max_history=5),
                            KerasPolicy()],
                  interpreter=interpreter,
                  action_endpoint=action_endpoint)

    data = agent.load_data(training_data_file)
    agent.train(data,
                       batch_size=50,
                       epochs=30,
                       max_training_samples=500)
    online.serve_agent(agent)
    return agent

if __name__ == '__main__':
    logging.basicConfig(level="INFO")
    nlu_interpreter = RasaNLUInterpreter('./models/nlu/default/EVNHANOI')
    run_EVN_online(nlu_interpreter)
JustinaPetr commented 5 years ago

Hey @Namnt3. Are you using Rasa Core >=0.11.11? If yes, then you should import AllSlotsReset from rasa_core_sdk rather than rasa_core. In you actionsENV.py you should have:

from rasa_core_sdk.events import Restarted, AllSlotsReset
from rasa_core_sdk import Action

Not sure if that's the cause of your problem, but it's likely

Namnt3 commented 5 years ago

Yeah, thank you @JustinaPetr, I followed your suggestion and it worked.