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/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 mock_form_unhappy_path(input_text, output_text, slot): response_error = { "error": f"Failed to extract slot {slot} 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_error)), ) responses = await agent.handle_text(input_text) assert responses[0]["text"] == output_text
async def test_remote_action_endpoint_responds_400( 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: # 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_channel, default_nlg, default_tracker, default_domain ) assert execinfo.type == ActionExecutionRejection assert "Custom action 'my_action' rejected to run" in str(execinfo.value)
async def mock_form_unhappy_path(input_text: Text, output_text: Text, slot: Optional[Text]) -> None: response_error = { "error": f"Failed to extract slot {slot} with action restaurant_form", "action_name": "restaurant_form", } with aioresponses() as mocked: # Request which rejects form execution mocked.post( "https://example.com/webhooks/actions", repeat=False, exception=ClientResponseError(400, "", json.dumps(response_error)), ) # Request after returning from unhappy path which sets next requested slot mocked.post( "https://example.com/webhooks/actions", payload=response_for_slot(slot), repeat=True, ) responses = await form_bot_agent.handle_text(input_text) assert responses[0]["text"] == output_text
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)