예제 #1
0
class RasaTrackerStore(SinkerStoreIntf):
    def __init__(self):
        from rasa.core.tracker_store import (TrackerStore,
                                             InMemoryTrackerStore,
                                             MongoTrackerStore)
        default_domain = get_default_domain()
        self.tracker_store = MongoTrackerStore(domain=default_domain)

    def drop(self):
        self.tracker_store.conversations.drop()

    def store(self, bucket: Text, values: Dict[Text, Any], user=None):
        evts = []
        tracker = self.tracker_store.get_or_create_tracker(
            bucket_id(bucket, user))
        for slot_key, slot_val in values.items():
            evts.append(SlotSet(slot_key, slot_val))
        for e in evts:
            tracker.update(e)
        self.tracker_store.save(tracker)

    def get_bucket(self, bucket: Text, user=None):
        tracker = self.tracker_store.get_or_create_tracker(
            bucket_id(bucket, user))
        # tracker.events
        dialogue = tracker.as_dialogue()
        return dialogue.as_dict()

    def slot_values(self, bucket: Text, user=None):
        tracker = self.tracker_store.get_or_create_tracker(
            bucket_id(bucket, user))
        return tracker.current_slot_values()
 def __init__(self):
     """ Se generan los trackers y se asocian a agentes ambos para ingles y espanol """
     with open(r'parameters.conf', encoding="utf-8") as f:
         datos = json.load(f)
     self.track_store = MongoTrackerStore(
         domain=Domain.load("../Jarvis/domain.yml"),
         host=datos['url'],
         db=datos['db'],
         username=datos['username'],
         password=datos['password'])
     self.track_store_en = MongoTrackerStore(
         domain=Domain.load("../Jarvis/domain_en.yml"),
         host=datos['url'],
         db=datos['db'],
         username=datos['username'],
         password=datos['password'])
     action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
     self.agent = Agent.load(
         '../Jarvis/models/latest.tar.gz',
         action_endpoint=action_endpoint,
         tracker_store=self.track_store,
     )
     self.agent_en = Agent.load(
         '../Jarvis/models/latest_en.tar.gz',
         action_endpoint=action_endpoint,
         tracker_store=self.track_store_en,
     )
예제 #3
0
 def get_tracker_and_domain(bot: Text):
     domain = ChatHistory.mongo_processor.load_domain(bot)
     endpoint = ChatHistory.mongo_processor.get_endpoints(bot)
     tracker = MongoTrackerStore(domain=domain,
                                 host=endpoint['tracker_endpoint']['url'],
                                 db=endpoint['tracker_endpoint']['db'],
                                 username=endpoint['tracker_endpoint'].get("username"),
                                 password=endpoint['tracker_endpoint'].get("password"))
     return (domain, tracker)
예제 #4
0
 def get_local_mongo_store(bot: Text, domain: Domain):
     "create local mongo tracker"
     db_url = Utility.environment['mongo_url']
     db_name = Utility.environment['test_conversation_db']
     username, password, url = Utility.extract_user_password(db_url)
     return MongoTrackerStore(domain=domain,
                              host=url,
                              db=db_name,
                              collection=bot,
                              username=username,
                              password=password)
예제 #5
0
    async def on_trynow(self, request):
        res_data = await request.json()
        print("----------- Inside Try now --from SID {}--------------".format(res_data['sessionId']))
        result = await ExportProject.main(res_data['sessionId'], res_data['projectObjectId'], 'SESSION')
        print(result)

        if result is not None:
            return web.json_response({"status": "Error", "message": result})

        import rasa.model as model
        from rasa.core.agent import Agent
        from rasa.core.tracker_store import MongoTrackerStore
        from rasa.core.domain import Domain
        from rasa.train import train_async
        from rasa.utils.endpoints import EndpointConfig

        base_path = CONFIG.get('api_gateway', 'SESSION_MODEL_PATH')
        config = "config.yml"
        training_files = "data/"
        domain = "domain.yml"
        output = "models/"

        endpoints = EndpointConfig(url="http://action_server:5055/webhook")

        base_path = base_path + res_data['sessionId'] + "/"

        config = base_path + config
        training_files = base_path + training_files
        domain = base_path + domain
        output = base_path + output
        start_time = time.time()
        try:
            model_path = await train_async(domain, config, [training_files], output, additional_arguments={"augmentation_factor": 10})
            end_time = time.time()
            print("it took this long to run: {}".format(end_time - start_time))
            unpacked = model.get_model(model_path)
            domain = Domain.load(domain)
            _tracker_store = MongoTrackerStore(domain=domain,
                                                host=CONFIG.get('api_gateway', 'MONGODB_URL'),
                                                db=CONFIG.get('api_gateway', 'MONGODB_NAME'),
                                                username=None,
                                                password=None,
                                                auth_source="admin",
                                                collection="conversations",
                                                event_broker=None)
            print("***************  Actions Endpoint as per data ********** {}".format(endpoints.url))
            self.agent = Agent.load(unpacked, tracker_store=_tracker_store, action_endpoint=endpoints)
            return web.json_response({"status": "Success", "message": "Ready to chat"})
            #await sio.emit('chatResponse', {"status": "Success", "message": "Ready to chat"}, namespace='/trynow', room=sid)
        except Exception as e:
            print("Exception while try Now ---  "+str(e))
            #await sio.emit('chatResponse', {"status": "Error", "message": repr(e)}, namespace='/trynow', room=sid)
            return web.json_response({"status": "Error", "message": repr(e)})
예제 #6
0
 def get_tracker_and_domain(bot: Text):
     """ Returns the Mongo Tracker and Domain of the bot """
     domain = ChatHistory.mongo_processor.load_domain(bot)
     try:
         endpoint = ChatHistory.mongo_processor.get_endpoints(bot)
         tracker = MongoTrackerStore(
             domain=domain,
             host=endpoint["tracker_endpoint"]["url"],
             db=endpoint["tracker_endpoint"]["db"],
             username=endpoint["tracker_endpoint"].get("username"),
             password=endpoint["tracker_endpoint"].get("password"),
         )
     except Exception:
         tracker = Utility.get_local_mongo_store(bot, domain)
     return (domain, tracker)
예제 #7
0
    def get_local_mongo_store(bot: Text, domain: Domain):
        """
        create local mongo tracker

        :param bot: bot id
        :param domain: domain data
        :return: mongo tracker
        """
        username, password, url, db_name = Utility.get_local_db()
        return MongoTrackerStore(
            domain=domain,
            host=url,
            db=db_name,
            collection=bot,
            username=username,
            password=password,
        )
예제 #8
0
파일: utils.py 프로젝트: udit-pandey/kairon
    def get_local_mongo_store(bot: Text, domain):
        """
        create local mongo tracker

        :param bot: bot id
        :param domain: domain data
        :return: mongo tracker
        """
        from rasa.core.tracker_store import MongoTrackerStore
        config = Utility.get_local_db()
        return MongoTrackerStore(
            domain=domain,
            host=config['host'],
            db=config['db'],
            collection=bot,
            username=config.get('username'),
            password=config.get('password'),
            auth_source=config['options'].get("authSource")
            if config['options'].get("authSource") else "admin")
예제 #9
0
    def get_tracker_and_domain(bot: Text):
        """
        loads domain data and mongo tracker

        :param bot: bot id
        :return: tuple domain, tracker
        """
        domain = ChatHistory.mongo_processor.load_domain(bot)
        message = None
        try:
            endpoint = ChatHistory.mongo_processor.get_endpoints(bot)
            tracker = MongoTrackerStore(
                domain=domain,
                host=endpoint["tracker_endpoint"]["url"],
                db=endpoint["tracker_endpoint"]["db"],
                username=endpoint["tracker_endpoint"].get("username"),
                password=endpoint["tracker_endpoint"].get("password"),
            )
        except Exception as e:
            logger.info(e)
            message = "Loading test conversation! " + str(e)
            tracker = Utility.get_local_mongo_store(bot, domain)

        return domain, tracker, message
예제 #10
0
 def get_tracker_and_domain_data(self, *args, **kwargs):
     domain = Domain.from_file("tests/testing_data/initial/domain.yml")
     return domain, MongoTrackerStore(
         domain, host="mongodb://192.168.100.140:27169"), None
예제 #11
0
 def __init__(self):
     from rasa.core.tracker_store import (TrackerStore,
                                          InMemoryTrackerStore,
                                          MongoTrackerStore)
     default_domain = get_default_domain()
     self.tracker_store = MongoTrackerStore(domain=default_domain)
예제 #12
0
import logging
from logs import LogHandler
from config import ConfigManager as CM

# setup logging
ErrorLogger = logging.getLogger("ErrorLogs")
InfoLogger = logging.getLogger("InfoLogs")
LogHandler.setup_logging()

domain = Domain.load('domain.yml')
modelPath = CM.rasa_model_path
action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")

agent = Agent.load(str(modelPath),
                   action_endpoint=action_endpoint,
                   tracker_store=MongoTrackerStore(domain=domain))


async def parse(text, senderId):
    global agent
    response = await agent.handle_text(text, sender_id=senderId)
    return response


def get_bot_response(userMsg, senderId):
    response = None
    if agent.is_ready():
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        response = loop.run_until_complete(parse(userMsg, senderId))
        InfoLogger.info("BOT RESPONSE ::: " + str(response))
class JanetServJarvis():
    """"Carga la URL de la localización de Jarvis del fichero 'parameters.conf'"""
    def __init__(self):
        """ Se generan los trackers y se asocian a agentes ambos para ingles y espanol """
        with open(r'parameters.conf', encoding="utf-8") as f:
            datos = json.load(f)
        self.track_store = MongoTrackerStore(
            domain=Domain.load("../Jarvis/domain.yml"),
            host=datos['url'],
            db=datos['db'],
            username=datos['username'],
            password=datos['password'])
        self.track_store_en = MongoTrackerStore(
            domain=Domain.load("../Jarvis/domain_en.yml"),
            host=datos['url'],
            db=datos['db'],
            username=datos['username'],
            password=datos['password'])
        action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
        self.agent = Agent.load(
            '../Jarvis/models/latest.tar.gz',
            action_endpoint=action_endpoint,
            tracker_store=self.track_store,
        )
        self.agent_en = Agent.load(
            '../Jarvis/models/latest_en.tar.gz',
            action_endpoint=action_endpoint,
            tracker_store=self.track_store_en,
        )

    async def handle_message_async(self, data):
        """ Dado un mensaje, estima el idioma en el que esta y lo analiza con su correspondiente tracker,
        calculando intenciones, el estado del tracker y la respuesta  """
        mensaje_de_usuario = data['message']
        idioma = detect(mensaje_de_usuario)
        if idioma == 'es':
            print('Detectado español'.encode('utf-8'), flush=True)
            resp = await self.agent.handle_message(mensaje_de_usuario,
                                                   sender_id=data['sender'])
            tracker = self.track_store.get_or_create_tracker(data['sender'])
            output = await self.agent.parse_message_using_nlu_interpreter(
                mensaje_de_usuario, tracker)
        elif idioma == 'en':
            print('Detectado inglés'.encode('utf-8'), flush=True)
            resp = await self.agent_en.handle_message(mensaje_de_usuario,
                                                      sender_id=data['sender'])
            tracker = self.track_store_en.get_or_create_tracker(data['sender'])
            output = await self.agent_en.parse_message_using_nlu_interpreter(
                mensaje_de_usuario, tracker)
        else:  # predeterminado en español
            fraseLog = 'Se ha detectado que el idioma era ' + idioma + ' pero se ha respondido en español'
            print(fraseLog.encode('utf-8'), flush=True)
            idioma = 'es'
            resp = await self.agent.handle_message(mensaje_de_usuario,
                                                   sender_id=data['sender'])
            tracker = self.track_store.get_or_create_tracker(data['sender'])
            output = await self.agent.parse_message_using_nlu_interpreter(
                mensaje_de_usuario, tracker)
        return resp, output, tracker, idioma

    def consultar(self, pregunta, id):
        """ Dado un mensaje y un id de usuario estima la respuesta teniendo en cuenta los ultimos mensajes de dicho usuario """
        contenido = pregunta
        contenido = contenido[0].lower() + contenido[1:]
        data = {'sender': id, 'message': contenido}

        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        try:
            resp, output, tracker, idioma = loop.run_until_complete(
                self.handle_message_async(data))
        except error.URLError as e:
            if isinstance(e.reason, timeout):
                msg = "Janet se encuentra en mantenimiento en estos momentos. " \
                          "Inténtelo de nuevo más tarde"
                raise error.HTTPError(self._url, 400, msg, None, None)
            if hasattr(e, 'code') and e.code == 400:
                msg = "Janet se encuentra en mantenimiento en estos momentos. " \
                          "Inténtelo de nuevo más tarde"
                raise error.HTTPError(self._url, 400, msg, None, None)
            else:
                raise error.HTTPError(self._url, 500, e.reason, None, None)
        finally:
            loop.close()

        return resp, output, tracker, idioma

    def restart(self, id):
        data = {'user_id': id, 'content': '/restart'}

        req = request.Request(self._url, data=parse.urlencode(data).encode())
        request.urlopen(req, timeout=10)
예제 #14
0
 def get_tracker_and_domain_data(self, *args, **kwargs):
     domain = Domain.from_file("tests/testing_data/initial/domain.yml")
     return domain, MongoTrackerStore(domain,
                                      host="mongodb://localhost:27019")
예제 #15
0
# Load all send methods classes
message_exec.load()

config = load_config()
host, port = '0.0.0.0', config['websocket']['port']
dashlog = Tracker(config['dashbot']['api'],
                  config['dashbot'][config["template"]["module"]]['api_key'])

action_endpoint = EndpointConfig(url=config['server']['actions_endpoint'])
nlg_endpoint = EndpointConfig(url=config['server']['nlg_endpoint'])
domain = Domain.load('domain.yml')
db_conf = config['bluelog']
mongo_tracker = MongoTrackerStore(domain,
                                  host=db_conf['host'],
                                  db=db_conf['db'],
                                  username=db_conf['username'],
                                  password=db_conf['password'],
                                  auth_source=db_conf['authsource'],
                                  collection=config['template']['module'])

agent_all = {}

try:
    if os.path.isdir('./models/nlu'):
        if os.path.isdir('./models/nlu/idn'):
            nlu_interpreter_idn = RasaNLUInterpreter('./models/nlu/idn/')
            agent_idn = Agent.load('./models/core/core.tar.gz',
                                   interpreter=nlu_interpreter_idn,
                                   action_endpoint=action_endpoint,
                                   generator=nlg_endpoint,
                                   tracker_store=mongo_tracker)