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)})
async def test_remote_action_multiple_events_payload( default_channel: OutputChannel, default_nlg: NaturalLanguageGenerator, default_tracker: DialogueStateTracker, domain: Domain, ): endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) response = { "events": [ { "event": "action", "name": "action_listen", "policy": None, "confidence": None, "timestamp": None, }, { "event": "slot", "name": "name", "value": None, "timestamp": None }, { "event": "user", "timestamp": None, "text": "hello", "parse_data": { "intent": { "name": "greet", "confidence": 0.99 }, "entities": [], }, }, ], "responses": [], } with aioresponses() as mocked: mocked.post("https://example.com/webhooks/actions", payload=response) events = await remote_action.run(default_channel, default_nlg, default_tracker, domain) assert isinstance(events[0], ActionExecuted) assert events[0].as_dict().get("name") == "action_listen" assert isinstance(events[1], SlotSet) assert events[1].as_dict().get("name") == "name" assert isinstance(events[2], UserUttered) assert events[2].as_dict().get("text") == "hello"
def __init__(self, model_name: Text = None, endpoint: EndpointConfig = None, project_name: Text = 'default') -> None: self.model_name = model_name self.project_name = project_name if endpoint: self.endpoint = endpoint else: self.endpoint = EndpointConfig(constants.DEFAULT_SERVER_URL)
async def test_formbot_example(): sys.path.append("examples/formbot/") p = "examples/formbot/" stories = os.path.join(p, "data", "stories.md") endpoint = EndpointConfig("https://example.com/webhooks/actions") endpoints = AvailableEndpoints(action=endpoint) agent = await train(os.path.join(p, "domain.yml"), stories, os.path.join(p, "models", "dialogue"), endpoints=endpoints, policy_config="rasa/core/default_config.yml") response = { 'events': [{ 'event': 'form', 'name': 'restaurant_form', 'timestamp': None }, { 'event': 'slot', 'timestamp': None, 'name': 'requested_slot', 'value': 'cuisine' }], 'responses': [{ 'template': 'utter_ask_cuisine' }] } with aioresponses() as mocked: mocked.post('https://example.com/webhooks/actions', payload=response, repeat=True) responses = await agent.handle_text("/request_restaurant") assert responses[0]['text'] == 'what cuisine?' response = { "error": "Failed to validate slot cuisine with action " "restaurant_form", "action_name": "restaurant_form" } with aioresponses() as mocked: # noinspection PyTypeChecker mocked.post('https://example.com/webhooks/actions', repeat=True, exception=ClientResponseError(400, "", json.dumps(response))) responses = await agent.handle_text("/chitchat") assert responses[0]['text'] == 'chitchat'
async def test_formbot_example(): sys.path.append("examples/formbot/") p = "examples/formbot/" stories = os.path.join(p, "data", "stories.md") endpoint = EndpointConfig("https://example.com/webhooks/actions") endpoints = AvailableEndpoints(action=endpoint) agent = await train( os.path.join(p, "domain.yml"), stories, os.path.join(p, "models", "dialogue"), endpoints=endpoints, policy_config="rasa/cli/default_config.yml", ) response = { "events": [ {"event": "form", "name": "restaurant_form", "timestamp": None}, { "event": "slot", "timestamp": None, "name": "requested_slot", "value": "cuisine", }, ], "responses": [{"template": "utter_ask_cuisine"}], } with aioresponses() as mocked: mocked.post( "https://example.com/webhooks/actions", payload=response, repeat=True ) responses = await agent.handle_text("/request_restaurant") assert responses[0]["text"] == "what cuisine?" response = { "error": "Failed to validate slot cuisine with action restaurant_form", "action_name": "restaurant_form", } with aioresponses() as mocked: # noinspection PyTypeChecker mocked.post( "https://example.com/webhooks/actions", repeat=True, exception=ClientResponseError(400, "", json.dumps(response)), ) responses = await agent.handle_text("/chitchat") assert responses[0]["text"] == "chitchat"
async def test_validate_slots_on_activation_with_other_action_after_user_utterance(): form_name = "my form" slot_name = "num_people" slot_value = "hi" events = [ ActionExecuted(ACTION_LISTEN_NAME), UserUttered(slot_value, entities=[{"entity": "num_tables", "value": 5}]), ActionExecuted("action_in_between"), ] tracker = DialogueStateTracker.from_events(sender_id="bla", evts=events) domain = f""" slots: {slot_name}: type: unfeaturized forms: {form_name}: {slot_name}: - type: from_text actions: - validate_{form_name} """ domain = Domain.from_yaml(domain) action_server_url = "http:/my-action-server:5055/webhook" expected_slot_value = "✅" with aioresponses() as mocked: mocked.post( action_server_url, payload={ "events": [ {"event": "slot", "name": slot_name, "value": expected_slot_value} ] }, ) action_server = EndpointConfig(action_server_url) action = FormAction(form_name, action_server) events = await action.run( CollectingOutputChannel(), TemplatedNaturalLanguageGenerator(domain.templates), tracker, domain, ) assert events == [ ActiveLoop(form_name), SlotSet(slot_name, expected_slot_value), SlotSet(REQUESTED_SLOT, None), ActiveLoop(None), ]
async def form_bot_agent(trained_async: Callable) -> Agent: endpoint = EndpointConfig("https://example.com/webhooks/actions") zipped_model = await trained_async( domain="examples/formbot/domain.yml", config="examples/formbot/config.yml", training_files=[ "examples/formbot/data/rules.yml", "examples/formbot/data/stories.yml", ], ) return Agent.load_local_model(zipped_model, action_endpoint=endpoint)
def reload(bot: Text): try: endpoint = AgentProcessor.mongo_processor.get_endpoints( bot, raise_exception=False) action_endpoint = (EndpointConfig( url=endpoint["action_endpoint"]["url"]) if endpoint and endpoint.get("action_endpoint") else None) model_path = AgentProcessor.get_latest_model(bot) agent = Agent.load(model_path, action_endpoint=action_endpoint) InMemoryAgentCache.set(bot, agent) except Exception as e: logging.info(e) raise AppException("Please train the bot first")
async def test_remote_action_runs(default_channel, default_nlg, default_tracker, default_domain): endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) with aioresponses() as mocked: mocked.post( "https://example.com/webhooks/actions", payload={ "events": [], "responses": [] }, ) await remote_action.run(default_channel, default_nlg, default_tracker, default_domain) r = latest_request(mocked, "post", "https://example.com/webhooks/actions") assert r assert json_of_latest_request(r) == { "domain": default_domain.as_dict(), "next_action": "my_action", "sender_id": "my-sender", "version": rasa.__version__, "tracker": { "latest_message": { "entities": [], "intent": {}, "text": None, "message_id": None, "metadata": {}, }, ACTIVE_LOOP: {}, "latest_action": {}, "latest_action_name": None, "sender_id": "my-sender", "paused": False, "latest_event_time": None, FOLLOWUP_ACTION: "action_listen", "slots": { "name": None, REQUESTED_SLOT: None }, "events": [], "latest_input_channel": None, }, }
async def test_remote_action_with_template_param( default_channel: OutputChannel, default_tracker: DialogueStateTracker, domain: Domain, ): endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) response = { "events": [ { "event": "form", "name": "restaurant_form", "timestamp": None }, { "event": "slot", "timestamp": None, "name": "requested_slot", "value": "cuisine", }, ], "responses": [{ "text": None, "buttons": [], "elements": [], "custom": {}, "template": "utter_ask_cuisine", "image": None, "attachment": None, }], } nlg = TemplatedNaturalLanguageGenerator( {"utter_ask_cuisine": [{ "text": "what dou want to eat?" }]}) with aioresponses() as mocked: mocked.post("https://example.com/webhooks/actions", payload=response) with pytest.warns(FutureWarning): events = await remote_action.run(default_channel, nlg, default_tracker, domain) assert events == [ BotUttered("what dou want to eat?", metadata={"utter_action": "utter_ask_cuisine"}), ActiveLoop("restaurant_form"), SlotSet("requested_slot", "cuisine"), ]
async def test_remote_action_endpoint_responds_500(default_channel, default_nlg, default_tracker, default_domain): endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) with aioresponses() as mocked: mocked.post("https://example.com/webhooks/actions", status=500) with pytest.raises(Exception) as execinfo: await remote_action.run(default_channel, default_nlg, default_tracker, default_domain) assert "Failed to execute custom action." in str(execinfo.value)
async def test_endpoint_config(): with aioresponses() as mocked: endpoint = EndpointConfig( "https://example.com/", params={"A": "B"}, headers={"X-Powered-By": "Rasa"}, basic_auth={ "username": "******", "password": "******" }, token="mytoken", token_name="letoken", type="redis", port=6379, db=0, password="******", timeout=30000, ) mocked.post( "https://example.com/test?A=B&P=1&letoken=mytoken", payload={"ok": True}, repeat=True, status=200, ) await endpoint.request( "post", subpath="test", content_type="application/text", json={"c": "d"}, params={"P": "1"}, ) r = latest_request(mocked, "post", "https://example.com/test?A=B&P=1&letoken=mytoken") assert r assert json_of_latest_request(r) == {"c": "d"} assert r[-1].kwargs.get("params", {}).get("A") == "B" assert r[-1].kwargs.get("params", {}).get("P") == "1" assert r[-1].kwargs.get("params", {}).get("letoken") == "mytoken" # unfortunately, the mock library won't report any headers stored on # the session object, so we need to verify them separately async with endpoint.session() as s: assert s._default_headers.get("X-Powered-By") == "Rasa" assert s._default_auth.login == "user" assert s._default_auth.password == "pass"
async def test_remote_action_endpoint_responds_500( default_dispatcher_collecting, default_domain): tracker = DialogueStateTracker("default", default_domain.slots) endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) with aioresponses() as mocked: mocked.post("https://example.com/webhooks/actions", status=500) with pytest.raises(Exception) as execinfo: await remote_action.run(default_dispatcher_collecting, tracker, default_domain) assert "Failed to execute custom action." in str(execinfo.value)
async def test_http_interpreter(): with aioresponses() as mocked: mocked.post("https://example.com/model/parse") endpoint = EndpointConfig("https://example.com") interpreter = RasaNLUHttpInterpreter(endpoint=endpoint) await interpreter.parse(text="message_text") r = latest_request(mocked, "POST", "https://example.com/model/parse") query = json_of_latest_request(r) response = {"text": "message_text", "token": None} assert query == response
async def test_validate_slots(validate_return_events: List[Dict], expected_events: List[Event]): form_name = "my form" slot_name = "num_people" slot_value = "hi" events = [ ActiveLoop(form_name), SlotSet(REQUESTED_SLOT, slot_name), ActionExecuted(ACTION_LISTEN_NAME), UserUttered(slot_value, entities=[{ "entity": "num_tables", "value": 5 }]), ] tracker = DialogueStateTracker.from_events(sender_id="bla", evts=events) domain = f""" slots: {slot_name}: type: any num_tables: type: any forms: {form_name}: {slot_name}: - type: from_text num_tables: - type: from_entity entity: num_tables actions: - validate_{form_name} """ domain = Domain.from_yaml(domain) action_server_url = "http:/my-action-server:5055/webhook" with aioresponses() as mocked: mocked.post(action_server_url, payload={"events": validate_return_events}) action_server = EndpointConfig(action_server_url) action = FormAction(form_name, action_server) events = await action.run( CollectingOutputChannel(), TemplatedNaturalLanguageGenerator(domain.templates), tracker, domain, ) assert events == expected_events
def test_file_broker_logs_to_file(tmpdir): fname = tmpdir.join("events.log").strpath actual = EventBroker.create(EndpointConfig(**{"type": "file", "path": fname})) for e in TEST_EVENTS: actual.publish(e.as_dict()) # reading the events from the file one event per line recovered = [] with open(fname, "r") as f: for l in f: recovered.append(Event.from_parameters(json.loads(l))) assert recovered == TEST_EVENTS
def _get_model_endpoint(model_endpoint: Optional[EndpointConfig], rasa_x_token: Text, rasa_x_url: Text) -> EndpointConfig: # If you change that, please run a test with Rasa X and speak to the bot default_rasax_model_server_url = f"{rasa_x_url}/models/tags/production" model_endpoint = model_endpoint or EndpointConfig() # Checking if endpoint.yml has existing url, if so give # warning we are overwriting the endpoint.yml file. custom_url = model_endpoint.url if custom_url and custom_url != default_rasax_model_server_url: logger.info( f"Ignoring url '{custom_url}' from 'endpoints.yml' and using " f"'{default_rasax_model_server_url}' instead.") custom_wait_time_pulls = model_endpoint.kwargs.get( "wait_time_between_pulls") return EndpointConfig( default_rasax_model_server_url, token=rasa_x_token, wait_time_between_pulls=custom_wait_time_pulls or 2, )
def test_mongo_tracker_store_raise_exception(domain: Domain, monkeypatch: MonkeyPatch): monkeypatch.setattr( rasa.core.tracker_store, "MongoTrackerStore", Mock( side_effect=OperationFailure("not authorized on logs to execute command.") ), ) with pytest.raises(ConnectionException) as error: TrackerStore.create( EndpointConfig(username="******", password="******", type="mongod"), domain, ) assert "not authorized on logs to execute command." in str(error.value)
async def test_remote_action_runs(default_dispatcher_collecting, default_domain): tracker = DialogueStateTracker("default", default_domain.slots) endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) with aioresponses() as mocked: mocked.post( "https://example.com/webhooks/actions", payload={ "events": [], "responses": [] }, ) await remote_action.run(default_dispatcher_collecting, tracker, default_domain) r = latest_request(mocked, "post", "https://example.com/webhooks/actions") assert r assert json_of_latest_request(r) == { "domain": default_domain.as_dict(), "next_action": "my_action", "sender_id": "default", "version": rasa.__version__, "tracker": { "latest_message": { "entities": [], "intent": {}, "text": None }, "active_form": {}, "latest_action_name": None, "sender_id": "default", "paused": False, "latest_event_time": None, "followup_action": "action_listen", "slots": { "name": None }, "events": [], "latest_input_channel": None, }, }
def test_file_broker_properly_logs_newlines(tmpdir): fname = tmpdir.join("events.log").strpath actual = EventBroker.create(EndpointConfig(**{"type": "file", "path": fname})) event_with_newline = UserUttered("hello \n there") actual.publish(event_with_newline.as_dict()) # reading the events from the file one event per line recovered = [] with open(fname, "r") as f: for l in f: recovered.append(Event.from_parameters(json.loads(l))) assert recovered == [event_with_newline]
async def test_http_parsing(): message = UserMessage("lunch?") endpoint = EndpointConfig("https://interpreter.com") with aioresponses() as mocked: mocked.post("https://interpreter.com/model/parse", repeat=True, status=200) inter = RasaNLUHttpInterpreter(endpoint_config=endpoint) try: await MessageProcessor(inter, None, None, None, None).parse_message(message) except KeyError: pass # logger looks for intent and entities, so we except r = latest_request(mocked, "POST", "https://interpreter.com/model/parse") assert r
async def test_project_with_model_server(zipped_nlu_model): fingerprint = 'somehash' model_endpoint = EndpointConfig('http://server.com/models/nlu/tags/latest') # mock a response that returns a zipped model with io.open(zipped_nlu_model, 'rb') as f: responses.add(responses.GET, model_endpoint.url, headers={ "ETag": fingerprint, "filename": "my_model_xyz.zip" }, body=f.read(), content_type='application/zip', stream=True) project = await load_from_server(model_server=model_endpoint) assert project.fingerprint == fingerprint
async def test_parsing_with_tracker(): tracker = DialogueStateTracker.from_dict("1", [], [Slot("requested_language")]) # we'll expect this value 'en' to be part of the result from the interpreter tracker._set_slot("requested_language", "en") endpoint = EndpointConfig("https://interpreter.com") with aioresponses() as mocked: mocked.post("https://interpreter.com/parse", repeat=True, status=200) # mock the parse function with the one defined for this test with patch.object(RasaNLUHttpInterpreter, "parse", mocked_parse): interpreter = RasaNLUHttpInterpreter(endpoint_config=endpoint) agent = Agent(None, None, interpreter) result = await agent.parse_message_using_nlu_interpreter("lunch?", tracker) assert result["requested_language"] == "en"
def test_callback_channel(): # START DOC INCLUDE from rasa.core.channels.callback import CallbackInput input_channel = CallbackInput( # URL Core will call to send the bot responses endpoint=EndpointConfig("http://localhost:5004")) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["callback_webhook.health"].startswith( "/webhooks/callback") assert routes_list["callback_webhook.webhook"].startswith( "/webhooks/callback/webhook")
async def test_file_broker_logs_to_file(tmp_path: Path): log_file_path = str(tmp_path / "events.log") actual = await EventBroker.create( EndpointConfig(**{"type": "file", "path": log_file_path}) ) for e in TEST_EVENTS: actual.publish(e.as_dict()) # reading the events from the file one event per line recovered = [] with open(log_file_path, "r") as log_file: for line in log_file: recovered.append(Event.from_parameters(json.loads(line))) assert recovered == TEST_EVENTS
def __createInstance(self, lang): if not lang in SUPPORTED_LANGS: logger.warning( f'Unsupported language: ${lang}. The default will be used: ${DEFAULT_LANG}' ) model = self.__get_model(lang) if not self.__exists_model(model): print('Model not found: "{}"'.format(model)) sys.exit(2) url = os.getenv('ACTION_ENDPOINT') print('action endpoint: "{}"'.format(url)) agent = Agent.load(model, action_endpoint=EndpointConfig(url)) return agent
async def test_remote_action_runs(default_dispatcher_collecting, default_domain): tracker = DialogueStateTracker("default", default_domain.slots) endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) with aioresponses() as mocked: mocked.post( 'https://example.com/webhooks/actions', payload={"events": [], "responses": []}) await remote_action.run(default_dispatcher_collecting, tracker, default_domain) r = latest_request(mocked, 'post', "https://example.com/webhooks/actions") assert r assert json_of_latest_request(r) == { 'domain': default_domain.as_dict(), 'next_action': 'my_action', 'sender_id': 'default', 'version': rasa.__version__, 'tracker': { 'latest_message': { 'entities': [], 'intent': {}, 'text': None }, 'active_form': {}, 'latest_action_name': None, 'sender_id': 'default', 'paused': False, 'latest_event_time': None, 'followup_action': 'action_listen', 'slots': {'name': None}, 'events': [], 'latest_input_channel': None } }
async def test_remote_action_utterances_with_none_values( default_channel, default_tracker, default_domain): endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) response = { "events": [ { "event": "form", "name": "restaurant_form", "timestamp": None }, { "event": "slot", "timestamp": None, "name": "requested_slot", "value": "cuisine", }, ], "responses": [{ "text": None, "buttons": None, "elements": [], "custom": None, "template": "utter_ask_cuisine", "image": None, "attachment": None, }], } nlg = TemplatedNaturalLanguageGenerator( {"utter_ask_cuisine": [{ "text": "what dou want to eat?" }]}) with aioresponses() as mocked: mocked.post("https://example.com/webhooks/actions", payload=response) events = await remote_action.run(default_channel, nlg, default_tracker, default_domain) assert events == [ BotUttered("what dou want to eat?"), Form("restaurant_form"), SlotSet("requested_slot", "cuisine"), ]
async def test_file_broker_properly_logs_newlines(tmp_path: Path): log_file_path = str(tmp_path / "events.log") actual = await EventBroker.create( EndpointConfig(**{"type": "file", "path": log_file_path}) ) event_with_newline = UserUttered("hello \n there") actual.publish(event_with_newline.as_dict()) # reading the events from the file one event per line recovered = [] with open(log_file_path, "r") as log_file: for line in log_file: recovered.append(Event.from_parameters(json.loads(line))) assert recovered == [event_with_newline]
def get_agent(bot: Text) -> Agent: if bot in InMemoryAgentCache.cache.keys(): return InMemoryAgentCache.cache.get(bot) else: try: endpoint = AgentProcessor.mongo_processor.get_endpoints( bot, raise_exception=False) action_endpoint = EndpointConfig( url=endpoint['action_endpoint']['url'] ) if endpoint and endpoint.get("action_endpoint") else None model_path = Utility.get_latest_file( os.path.join(DEFAULT_MODELS_PATH, bot)) agent = Agent.load(model_path, action_endpoint=action_endpoint) InMemoryAgentCache.set(bot, agent) return agent except Exception as e: logging.info(e) raise AppException("Please train the bot first")