Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple utterances #1114

Merged
merged 6 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion kairon/shared/actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,14 @@ def handle_utter_bot_response(dispatcher: CollectingDispatcher, dispatch_type: s
logger.exception(e)
dispatcher.utter_message(str(bot_response))
else:
dispatcher.utter_message(str(bot_response))
if isinstance(bot_response, list):
for message in bot_response:
if isinstance(message, dict):
dispatcher.utter_message(json_message=message)
else:
dispatcher.utter_message(str(message))
else:
dispatcher.utter_message(str(bot_response))
return bot_response, message

@staticmethod
Expand Down
159 changes: 159 additions & 0 deletions tests/integration_test/action_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,165 @@ def test_pyscript_action_execution():
assert response_json['responses'][0]['custom'] == {'numbers': [1, 2, 3, 4, 5], 'total': 15, 'i': 5}


@responses.activate
def test_pyscript_action_execution_with_multiple_utterances():
import textwrap

action_name = "test_pyscript_action_execution_with_multiple_utterances"
Actions(name=action_name, type=ActionType.pyscript_action.value,
bot="5f50fd0a56b698ca10d35d2z", user="user").save()
script = """
numbers = [1, 2, 3, 4, 5]
total = 0
for i in numbers:
total += i
print(total)
type = 'text'
bot_response = [{'text': 'Hello!'}, 'How can I help you?']
"""
script = textwrap.dedent(script)
PyscriptActionConfig(
name=action_name,
source_code=script,
bot="5f50fd0a56b698ca10d35d2z",
user="user"
).save()

responses.add(
"POST", Utility.environment['evaluator']['pyscript']['url'],
json={"success": True, "data": {"bot_response": [{'text': 'Hello!'}, 'How can I help you?'],
'numbers': [1, 2, 3, 4, 5], 'total': 15, 'i': 5,
"slots": {"location": "Bangalore", "langauge": "Kannada"}, "type": "text"},
"message": None, "error_code": 0},
match=[responses.matchers.json_params_matcher(
{'source_code': script,
'predefined_objects': {'sender_id': 'default', 'user_message': 'get intents',
'slot': {'bot': '5f50fd0a56b698ca10d35d2z', 'location': 'Bangalore',
'langauge': 'Kannada'}, 'intent': 'pyscript_action', 'chat_log': [],
'key_vault': {}, 'kairon_user_msg': None, 'session_started': None}})]
)

request_object = {
"next_action": action_name,
"tracker": {
"sender_id": "default",
"conversation_id": "default",
"slots": {"bot": "5f50fd0a56b698ca10d35d2z", "location": "Bangalore", "langauge": "Kannada"},
"latest_message": {'text': 'get intents', 'intent_ranking': [{'name': 'pyscript_action'}]},
"latest_event_time": 1537645578.314389,
"followup_action": "action_listen",
"paused": False,
"events": [{"event1": "hello"}, {"event2": "how are you"}],
"latest_input_channel": "rest",
"active_loop": {},
"latest_action": {},
},
"domain": {
"config": {},
"session_config": {},
"intents": [],
"entities": [],
"slots": {"bot": "5f50fd0a56b698ca10d35d2z"},
"responses": {},
"actions": [],
"forms": {},
"e2e_actions": []
},
"version": "version"
}
response = client.post("/webhook", json=request_object)
response_json = response.json()
assert response.status_code == 200
assert len(response_json['events']) == 3
assert len(response_json['responses']) == 2
assert response_json['events'] == [
{'event': 'slot', 'timestamp': None, 'name': 'location', 'value': 'Bangalore'},
{'event': 'slot', 'timestamp': None, 'name': 'langauge', 'value': 'Kannada'},
{'event': 'slot', 'timestamp': None, 'name': 'kairon_action_response',
'value': [{'text': 'Hello!'}, 'How can I help you?']}]
assert response_json['responses'][0]['custom'] == {'text': 'Hello!'}
assert response_json['responses'][1]['text'] == 'How can I help you?'

@responses.activate
def test_pyscript_action_execution_with_multiple_integer_utterances():
import textwrap

action_name = "test_pyscript_action_execution_with_multiple_integer_utterances"
Actions(name=action_name, type=ActionType.pyscript_action.value,
bot="5f50fd0a56b698ca10d35d2z", user="user").save()
script = """
numbers = [1, 2, 3]
total = 0
for i in numbers:
total += i
print(total)
type = 'text'
bot_response = numbers
"""
script = textwrap.dedent(script)
PyscriptActionConfig(
name=action_name,
source_code=script,
bot="5f50fd0a56b698ca10d35d2z",
user="user"
).save()

responses.add(
"POST", Utility.environment['evaluator']['pyscript']['url'],
json={"success": True, "data": {"bot_response": [1, 2, 3],
'numbers': [1, 2, 3], 'total': 6, 'i': 3,
"slots": {"location": "Bangalore", "langauge": "Kannada"}, "type": "text"},
"message": None, "error_code": 0},
match=[responses.matchers.json_params_matcher(
{'source_code': script,
'predefined_objects': {'sender_id': 'default', 'user_message': 'get intents',
'slot': {'bot': '5f50fd0a56b698ca10d35d2z', 'location': 'Bangalore',
'langauge': 'Kannada'}, 'intent': 'pyscript_action', 'chat_log': [],
'key_vault': {}, 'kairon_user_msg': None, 'session_started': None}})]
)

request_object = {
"next_action": action_name,
"tracker": {
"sender_id": "default",
"conversation_id": "default",
"slots": {"bot": "5f50fd0a56b698ca10d35d2z", "location": "Bangalore", "langauge": "Kannada"},
"latest_message": {'text': 'get intents', 'intent_ranking': [{'name': 'pyscript_action'}]},
"latest_event_time": 1537645578.314389,
"followup_action": "action_listen",
"paused": False,
"events": [{"event1": "hello"}, {"event2": "how are you"}],
"latest_input_channel": "rest",
"active_loop": {},
"latest_action": {},
},
"domain": {
"config": {},
"session_config": {},
"intents": [],
"entities": [],
"slots": {"bot": "5f50fd0a56b698ca10d35d2z"},
"responses": {},
"actions": [],
"forms": {},
"e2e_actions": []
},
"version": "version"
}
response = client.post("/webhook", json=request_object)
response_json = response.json()
assert response.status_code == 200
assert len(response_json['events']) == 3
assert len(response_json['responses']) == 3
assert response_json['events'] == [
{'event': 'slot', 'timestamp': None, 'name': 'location', 'value': 'Bangalore'},
{'event': 'slot', 'timestamp': None, 'name': 'langauge', 'value': 'Kannada'},
{'event': 'slot', 'timestamp': None, 'name': 'kairon_action_response', 'value': [1, 2, 3]}]
assert response_json['responses'][0]['text'] == '1'
assert response_json['responses'][1]['text'] == '2'
assert response_json['responses'][2]['text'] == '3'


@responses.activate
def test_pyscript_action_execution_with_bot_response_none():
import textwrap
Expand Down
Loading