def start_core(platform_token): from rasa.core.utils import AvailableEndpoints from rasa.core.run import serve_application from rasa.core.utils import EndpointConfig _endpoints = AvailableEndpoints( # TODO: make endpoints more configurable, esp ports model=EndpointConfig( "http://localhost:5002" "/api/projects/default/models/tags/production", token=platform_token, wait_time_between_pulls=1), event_broker=EndpointConfig(**{"type": "file"}), nlg=EndpointConfig("http://localhost:5002" "/api/nlg", token=platform_token)) serve_application( "models", nlu_model=None, channel="rasa", credentials_file="credentials.yml", cors="*", auth_token=None, # TODO: configure auth token enable_api=True, endpoints=_endpoints)
async def _pull_model_and_fingerprint(model_server: EndpointConfig, fingerprint: Optional[Text] ) -> Optional[(Text, Text)]: """Queries the model server and returns the value of the response's <ETag> header which contains the model hash. return None if not new fingerprint else return created temp directory and new fingerprint """ headers = {"If-None-Match": fingerprint} logger.debug("Requesting model from server {}..." "".format(model_server.url)) async with model_server.session() as session: try: params = model_server.combine_parameters() async with session.request("GET", model_server.url, timeout=DEFAULT_REQUEST_TIMEOUT, headers=headers, params=params) as resp: if resp.status in [204, 304]: logger.debug("Model server returned {} status code, " "indicating that no new model is available. " "Current fingerprint: {}" "".format(resp.status, fingerprint)) return None elif resp.status == 404: logger.debug( "Model server didn't find a model for our request. " "Probably no one did train a model for the project " "and tag combination yet.") return None elif resp.status != 200: logger.warning( "Tried to fetch model from server, but server response " "status code is {}. We'll retry later..." "".format(resp.status)) return None model_directory = tempfile.mkdtemp() utils.unarchive(await resp.read(), model_directory) logger.debug("Unzipped model to '{}'" "".format(os.path.abspath(model_directory))) # get the new fingerprint new_fingerprint = resp.headers.get("ETag") # return new fingerprint and new tmp model directory return model_directory, new_fingerprint except aiohttp.ClientResponseError as e: logger.warning("Tried to fetch model from server, but " "couldn't reach server. We'll retry later... " "Error: {}.".format(e)) return None
async def test_callback_calls_endpoint(): from rasa.core.channels.callback import CallbackOutput with aioresponses() as mocked: mocked.post("https://example.com/callback", repeat=True, headers={"Content-Type": "application/json"}) output = CallbackOutput(EndpointConfig("https://example.com/callback")) await output.send_response("test-id", { "text": "Hi there!", "image": "https://example.com/image.jpg" }) r = latest_request(mocked, "post", "https://example.com/callback") assert r image = r[-1].kwargs["json"] text = r[-2].kwargs["json"] assert image['recipient_id'] == "test-id" assert image['image'] == "https://example.com/image.jpg" assert text['recipient_id'] == "test-id" assert text['text'] == "Hi there!"
async def test_agent_with_model_server_in_thread(model_server, tmpdir, zipped_moodbot_model, moodbot_domain, moodbot_metadata): model_endpoint_config = EndpointConfig.from_dict({ "url": model_server.make_url('/model'), "wait_time_between_pulls": 2 }) agent = Agent() agent = await rasa.core.agent.load_from_server( agent, model_server=model_endpoint_config) await asyncio.sleep(3) assert agent.fingerprint == "somehash" assert agent.domain.as_dict() == moodbot_domain.as_dict() agent_policies = { utils.module_path_from_instance(p) for p in agent.policy_ensemble.policies } moodbot_policies = set(moodbot_metadata["policy_names"]) assert agent_policies == moodbot_policies assert model_server.app.number_of_model_requests == 1 jobs.kill_scheduler()
def run_dialogue(serve_forever=True): interpreter = RasaNLUInterpreter('./models/nlu/chatter') 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
def run(serve_forever=True): interpreter = RasaNLUInterpreter("models/nlu") agent = Agent.load("models/dialogue", interpreter=interpreter) action_endpoint = EndpointConfig(url="http://localhost:5055/webhook") if serve_forever: agent.handle_channels([CmdlineInput()]) return agent
def __init__(self, domain_path='agents/prototypes/concertbot/domain.yml'): self.executor = ActionExecutor() action_package_name = 'actions.procs' self.executor.register_package(action_package_name) endpoint = EndpointConfig("http://localhost:5000") self.interpreter = RasaNLUHttpInterpreter(model_name="nlu", project_name='chinese', endpoint=endpoint) self.domain = Domain.load(domain_path)
async def test_nlg(http_nlg, default_agent_path): sender = str(uuid.uuid1()) nlg_endpoint = EndpointConfig.from_dict({"url": http_nlg}) agent = Agent.load(default_agent_path, None, generator=nlg_endpoint) response = await agent.handle_text("/greet", sender_id=sender) assert len(response) == 1 assert response[0] == {"text": "Hey there!", "recipient_id": sender}
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"
def rasa_config(): # Set the actions endpoint action_endpoint = EndpointConfig( url='https://alimentos-actions.herokuapp.com/webhook') # Load the trained RASA models #interpreter = RasaNLUInterpreter('./models/rasa-telegram-app/nlu') return Agent.load('./models/20200629-233250.tar.gz', action_endpoint=action_endpoint)
async def test_remote_action_endpoint_not_running( 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 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)
def test_tracker_store_endpoint_config_loading(): cfg = utils.read_endpoint_config(DEFAULT_ENDPOINTS_FILE, "tracker_store") assert cfg == EndpointConfig.from_dict({ "type": "redis", "url": "localhost", "port": 6379, "db": 0, "password": "******", "timeout": 30000 })
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'
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_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_wait_time_between_pulls_without_interval( model_server, monkeypatch): monkeypatch.setattr("rasa.core.agent.schedule_model_pulling", lambda *args: 1 / 0) # will raise an exception model_endpoint_config = EndpointConfig.from_dict({ "url": model_server.make_url('/model'), "wait_time_between_pulls": None }) agent = Agent() # schould not call schedule_model_pulling, if it does, this will raise await rasa.core.agent.load_from_server(agent, model_server=model_endpoint_config) jobs.kill_scheduler()
async def test_http_parsing(): message = UserMessage('lunch?') endpoint = EndpointConfig('https://interpreter.com') with aioresponses() as mocked: mocked.post('https://interpreter.com/parse', repeat=True, status=200) inter = RasaNLUHttpInterpreter(endpoint=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/parse") assert r assert json_of_latest_request(r)['message_id'] == message.message_id
async def test_http_interpreter(): with aioresponses() as mocked: mocked.post("https://example.com/parse") endpoint = EndpointConfig('https://example.com') interpreter = RasaNLUHttpInterpreter(endpoint=endpoint) await interpreter.parse(text='message_text', message_id='1134') r = latest_request( mocked, "POST", "https://example.com/parse") query = json_of_latest_request(r) response = {'project': 'default', 'q': 'message_text', 'message_id': '1134', 'model': None, 'token': None} assert query == response
async def test_remote_action_endpoint_responds_400( 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: # noinspection PyTypeChecker mocked.post('https://example.com/webhooks/actions', exception=ClientResponseError( 400, None, '{"action_name": "my_action"}')) with pytest.raises(Exception) as execinfo: await remote_action.run(default_dispatcher_collecting, tracker, default_domain) assert execinfo.type == ActionExecutionRejection assert "Custom action 'my_action' rejected to run" in str(execinfo.value)
def __init__(self, config, loop): """constructor""" super(RasaServiceLocal, self).__init__(config, loop) self.config = config self.subscribe_to = 'hermod/+/rasa/get_domain,hermod/+/rasa/set_slots' \ + ',hermod/+/dialog/ended,hermod/+/dialog/init,hermod/+/nlu/externalparse,' \ + 'hermod/+/nlu/parse,hermod/+/intent,hermod/+/intent,hermod/+/dialog/started' model_path = get_model( config['services']['RasaServiceLocal'].get('model_path')) endpoint = EndpointConfig( config['services']['RasaServiceLocal'].get('rasa_actions_url')) domain = 'domain.yml' self.tracker_store = InMemoryTrackerStore(domain) regex_interpreter = RegexInterpreter() self.text_interpreter = RasaNLUInterpreter(model_path + '/nlu') self.agent = Agent.load(model_path, action_endpoint=endpoint, tracker_store=self.tracker_store, interpreter=regex_interpreter)
def test_file_broker_logs_to_file(tmpdir): fname = tmpdir.join("events.log").strpath actual = broker.from_endpoint_config( 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 test_file_broker_properly_logs_newlines(tmpdir): fname = tmpdir.join("events.log").strpath actual = broker.from_endpoint_config( 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]
def get_agents_dict(): models_path = 'C:/Users/Gopi/Desktop/t-chatbot/models/' dirs = os.listdir(models_path) print("Dir entries::", dirs) agent_dict = {} temp = 1 for sub_dir in dirs: agent_path = models_path + sub_dir agent_tar_path = os.listdir(agent_path) for model in agent_tar_path: model_path = agent_path + "/" + model print(model_path) agent = Agent.load(model_path, action_endpoint=EndpointConfig(action_endpoint)) print(type(agent)) print(agent) agent_dict["org" + str(temp)] = agent temp = temp + 1 print("agent dic:", agent_dict) return agent_dict
def _serve_application(app, stories, finetune, skip_visualization): """Start a core server and attach the interactive learning IO.""" endpoint = EndpointConfig(url=DEFAULT_SERVER_URL) async def run_interactive_io(running_app: Sanic): """Small wrapper to shut down the server once cmd io is done.""" await record_messages(endpoint=endpoint, stories=stories, finetune=finetune, skip_visualization=skip_visualization, sender_id=uuid.uuid4().hex) logger.info("Killing Sanic server now.") running_app.stop() # kill the sanic server app.add_task(run_interactive_io) app.run(host='0.0.0.0', port=DEFAULT_SERVER_PORT, access_log=True) return app
def test_callback_channel(): with mock.patch.object(sanic.Sanic, 'run', fake_sanic_run): # START DOC INCLUDE from rasa.core.channels.callback import CallbackInput from rasa.core.agent import Agent from rasa.core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = CallbackInput( # URL Core will call to send the bot responses endpoint=EndpointConfig("http://localhost:5004")) s = agent.handle_channels([input_channel], 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.get("callback_webhook.health").startswith( "/webhooks/callback") assert routes_list.get("callback_webhook.webhook").startswith( "/webhooks/callback/webhook")
def test_nlg_endpoint_config_loading(): cfg = utils.read_endpoint_config(DEFAULT_ENDPOINTS_FILE, "nlg") assert cfg == EndpointConfig.from_dict( {"url": "http://localhost:5055/nlg"})
def test_load_custom_broker_name(): config = EndpointConfig(**{"type": "rasa.core.broker.FileProducer"}) assert broker.from_endpoint_config(config)
# -*- coding: utf-8 -*- """ Created on Thu May 7 11:40:40 2020 @author: hung.td170078 """ from rasa.core.channels.facebook import FacebookInput from rasa.core.agent import Agent from rasa.core.interpreter import RasaNLUInterpreter import os from rasa.core.utils import EndpointConfig # load your trained agent interpreter = RasaNLUInterpreter("models/nlu/") MODEL_PATH = "models/20200521-171510.tar.gz" action_endpoint = EndpointConfig(url="https://lunachatbot-prj2-actions.herokuapp.com/webhook") agent = Agent.load(MODEL_PATH, interpreter=interpreter, action_endpoint=action_endpoint) input_channel = FacebookInput( fb_verify="jackfrost", # you need tell facebook this token, to confirm your URL fb_secret="4ad8cdf285aeef2548b23c130cd6f56c", # your app secret fb_access_token="EAASmSto8E9IBANlkhOp0pcoMXdWJanepzZBBAmQ9ZBsm07CYlwG7qqIgb5ccUvGWGdZCJRS6U72W4j1MctoZBUhACihyuAhEUp5wAxsPBSkiMXADiKHWPA8LPErMz5WywZBWg043qnsBJZBr42HpPdPxcoAnWYJKQfc2DNANgERwZDZD" # token for the page you subscribed to ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], int(os.environ.get('PORT',5004)))
from rasa.core.channels.facebook import FacebookInput from rasa.core.agent import Agent from rasa.core.interpreter import RasaNLUInterpreter import os from rasa.core.utils import EndpointConfig # load your trained agent #interpreter = RasaNLUInterpreter("models/nlu/default/horoscopebot/") MODEL_PATH = "./models/20200820-074654.tar.gz" action_end_point = EndpointConfig(url="http://localhost:5055/webhook") agent = Agent.load(MODEL_PATH, action_endpoint=action_end_point) input_channel = FacebookInput( fb_verify="asdf;lkj", # you need tell facebook this token, to confirm your URL fb_secret="99deccebeaed0c908a974c12f901176d", # your app secret fb_access_token= "EAARixYEP3lQBAEe6DUj8snZBEcN2EHS1xeN5SEtP6Y0ZBDdKMdFcaXNg8tbffmEx5ZALgWEVzCFhILiIrlKISFiEZAGCJu3SMEnN8YExcyO0tNRwQuDru9BoikZAdDq1qvncnfYl5LWhIYAoEP4dXD7cOJ1tfE9ahY2cqE5JM6IizDCFttoVq" ) s = agent.handle_channels([input_channel], int(os.environ.get('PORT', 5004)))
def test_load_non_existent_custom_broker_name(): config = EndpointConfig(**{"type": "rasa.core.broker.MyProducer"}) assert broker.from_endpoint_config(config) is None