Skip to content

Commit

Permalink
Added default flag for slots and updated corresponding test cases (#1616
Browse files Browse the repository at this point in the history
)
  • Loading branch information
himanshugt16 authored Dec 6, 2024
1 parent a5f2130 commit 0d929db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions kairon/shared/data/data_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ class Slots(Auditlog):
status = BooleanField(default=True)
influence_conversation = BooleanField(default=False)
_has_been_set = BooleanField(default=False)
is_default = BooleanField(default=False)

meta = {"indexes": [{"fields": ["bot", ("bot", "status", "name")]}]}

Expand Down
11 changes: 7 additions & 4 deletions kairon/shared/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,7 @@ def add_system_required_slots(self, bot: Text, user: Text):
bot,
user,
raise_exception_if_exists=False,
is_default = True
)

for slot in [s for s in KaironSystemSlots if s.value not in non_conversational_slots]:
Expand All @@ -1688,6 +1689,7 @@ def add_system_required_slots(self, bot: Text, user: Text):
bot,
user,
raise_exception_if_exists=False,
is_default=True
)

def fetch_slots(self, bot: Text, status=True):
Expand Down Expand Up @@ -3407,6 +3409,7 @@ def add_complex_story(self, story: Dict, bot: Text, user: Text):
bot,
user,
raise_exception_if_exists=False,
is_default=True
)

return id
Expand Down Expand Up @@ -3469,6 +3472,7 @@ def add_multiflow_story(self, story: Dict, bot: Text, user: Text):
bot,
user,
raise_exception_if_exists=False,
is_default=True
)

return id
Expand Down Expand Up @@ -4728,7 +4732,7 @@ def list_all_actions(self, bot: str, with_doc_id: bool = True):
action.pop("timestamp")
yield action

def add_slot(self, slot_value: Dict, bot, user, raise_exception_if_exists=True):
def add_slot(self, slot_value: Dict, bot, user, raise_exception_if_exists=True, is_default = False):
"""
Adds slot if it doesn't exist, updates slot if it exists
:param slot_value: slot data dict
Expand Down Expand Up @@ -4766,6 +4770,7 @@ def add_slot(self, slot_value: Dict, bot, user, raise_exception_if_exists=True):
slot.max_value = slot_value.get("max_value")
slot.min_value = slot_value.get("min_value")

slot.is_default = is_default
slot.user = user
slot.bot = bot
slot_id = slot.save().id.__str__()
Expand Down Expand Up @@ -5264,9 +5269,7 @@ def get_existing_slots(bot: Text):
:param status: active or inactive, default is active
:return: list of slots
"""
excluded_slots = list(KaironSystemSlots)
query = Q(bot=bot, status=True) & Q(name__nin=excluded_slots)
for slot in Slots.objects(query):
for slot in Slots.objects(bot=bot, status=True):
slot = slot.to_mongo().to_dict()
slot.pop("bot")
slot.pop("user")
Expand Down
11 changes: 3 additions & 8 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9699,18 +9699,13 @@ def test_get_slots():
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
excluded_slots = list(KaironSystemSlots)

assert "data" in actual
assert len(actual["data"]) == 21
assert actual["success"]
assert actual["error_code"] == 0

assert len(actual["data"]) == 21 - len(excluded_slots)
returned_slot_names = [slot["name"] for slot in actual["data"]]
for excluded_slot in excluded_slots:
assert excluded_slot not in returned_slot_names

assert Utility.check_empty_string(actual["message"])
default_slots_count = sum(slot.get('is_default') for slot in actual["data"])
assert default_slots_count == 14


def test_add_slots():
Expand Down
37 changes: 26 additions & 11 deletions tests/unit_test/data_processor/data_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9348,21 +9348,36 @@ def test_get_slot(self):
processor = MongoProcessor()
slots = list(processor.get_existing_slots(bot))
expected = [
{'name': 'date_time', 'type': 'text', 'influence_conversation': True, '_has_been_set': False},
{'name': 'category', 'type': 'text', 'influence_conversation': False, '_has_been_set': False},
{'name': 'file', 'type': 'text', 'influence_conversation': False, '_has_been_set': False},
{'name': 'file_error', 'type': 'text', 'influence_conversation': False, '_has_been_set': False},
{'name': 'file_text', 'type': 'text', 'influence_conversation': False, '_has_been_set': False},
{'name': 'name', 'type': 'text', 'influence_conversation': True, '_has_been_set': False},
{'name': 'kairon_action_response', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True},
{'name': 'bot', 'type': 'any', 'initial_value': 'test', 'influence_conversation': False,
'_has_been_set': False, 'is_default': True},
{'name': 'order', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True},
{'name': 'payment', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True},
{'name': 'flow_reply', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True},
{'name': 'http_status_code', 'type': 'any', 'influence_conversation': False, '_has_been_set': False, 'is_default': True},
{'name': 'image', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True},
{'name': 'audio', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True},
{'name': 'video', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True},
{'name': 'document', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True},
{'name': 'doc_url', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True},
{'name': 'longitude', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True},
{'name': 'latitude', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True},
{'name': 'date_time', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': False},
{'name': 'category', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False},
{'name': 'file', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False},
{'name': 'file_error', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False},
{'name': 'file_text', 'type': 'text', 'influence_conversation': False, '_has_been_set': False, 'is_default': False},
{'name': 'name', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': False},
{'name': 'priority', 'type': 'categorical', 'values': ['low', 'medium', 'high', '__other__'],
'influence_conversation': True, '_has_been_set': False},
'influence_conversation': True, '_has_been_set': False, 'is_default': False},
{'name': 'ticketid', 'type': 'float', 'initial_value': 1.0, 'max_value': 1.0, 'min_value': 0.0,
'influence_conversation': True, '_has_been_set': False},
'influence_conversation': True, '_has_been_set': False, 'is_default': False},
{'name': 'age', 'type': 'float', 'max_value': 1.0, 'min_value': 0.0, 'influence_conversation': True,
'_has_been_set': False},
{'name': 'occupation', 'type': 'text', 'influence_conversation': True, '_has_been_set': False},
'_has_been_set': False, 'is_default': False},
{'name': 'occupation', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': False},
{'name': 'quick_reply', 'type': 'text', 'influence_conversation': True, '_has_been_set': False, 'is_default': True}
]
assert len(slots) == 10
assert len(slots) == 24
assert not DeepDiff(slots, expected, ignore_order=True)

def test_update_slot_add_value_intent_and_not_intent(self):
Expand Down

0 comments on commit 0d929db

Please sign in to comment.