Logging a Bot

If you have MongoDB or Redis installed as key-value stores, the tracker (composed of events as Bot Actions or User Inputs) is saved to the database. After using you bot, you can then extract an Dialog instance and get the lists of events which you then have to parse.

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.domain import Domain
from rasa_core.utils import EndpointConfig
from rasa_core.run import serve_application
from rasa_core.tracker_store import RedisTrackerStore
from rasa_core.tracker_store import MongoTrackerStore


logger = logging.getLogger(__name__)

domain          = Domain.load('domain.yml')
# db              = RedisTrackerStore(domain, 
#                                       host='localhost',
#                                       port=6379, db=0, password=None, 
#                                       event_broker=None,
#                                       record_exp=None )
db 			= MongoTrackerStore(
                                domain,
				host='mongodb://localhost:27017', 
				db='rasa', username=None, 
				password=None, collection='conversations', 
				event_broker=None)


def train_dialogue(domain = 'domain.yml',
				   model_path = './models/dialogue',
				   training_data_file = './data/stories.md'):
	"""Train a model"""
	nlu_interpreter = RasaNLUInterpreter('./models/nlu/default/')
	action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")				
	agent = Agent( domain = domain, 
                                policies = [MemoizationPolicy(), KerasPolicy()],
				interpreter=nlu_interpreter, 
                                generator=None, 
                                tracker_store=db, 
				action_endpoint=action_endpoint,
                                fingerprint=None)
	data = agent.load_data(training_data_file)	
	agent.train(	data,
				epochs = 100,
				batch_size = 25,
				validation_split = 0.2
                          )		
	agent.persist(model_path)
	return agent
	
if __name__ == '__main__':
	agent = train_dialogue()
	rasa_core.run.serve_application(agent , channel='cmdline')		


	#extract list of events and print its names 
	events = db.retrieve("default").as_dialogue().events
	for event in events:
		print(event.as_story_string())

This then yields something like

action_listen
inform{"location": "home"}
slot{"location": "home"}
action_weather
None
action_listen
deny
utter_goodbye
None
action_listen
restart

where None is generated from Utter (BotUttered) Actions. This list could be parsed into a story by some rules.

Unfortunately, and I would appreciate hints on that, this approach saves all conversations to one _id in MongoDB - and that even persistently, so rerunning the skript after closing the console keeps alls previous stories. Any idea?