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

OSS-668: Rasa Knowledge base actions are unable to query about a certain attribute of an object unless the user first asks to obtain a list of objects of a specific type. #922

Merged
merged 175 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
175 commits
Select commit Hold shift + click to select a range
cd1485f
modified logic in run, slot management in _query_objects and _query_a…
Gajithra Jan 10, 2023
751bc1f
added get_object_types function
Gajithra Jan 10, 2023
c611f0e
added get_object_type_dynamic function
Gajithra Jan 10, 2023
bfa920c
updated changelog
Gajithra Jan 17, 2023
1b1e4fd
updated changelog
Gajithra Jan 17, 2023
b600b88
Merge pull request #1 from RasaHQ/main
Gajithra Jan 18, 2023
348d898
Merge branch 'main' into OSS-668
Gajithra Jan 25, 2023
d3c4968
Merge pull request #4 from RasaHQ/main
Gajithra Feb 2, 2023
826803e
modified changelog name and content
Gajithra Feb 6, 2023
9cedec0
uncommented code snippet that was previously commented to experiment …
Gajithra Feb 6, 2023
ba32581
added comprehensive typing hint
Gajithra Feb 6, 2023
ca2f13b
renamed to
Gajithra Feb 6, 2023
cbb38df
replaced the 'get_object_type_dynamic' function name with the new fun…
Gajithra Feb 6, 2023
e5f8d56
replaced the 'get_object_type_dynamic' function name with the new fun…
Gajithra Feb 6, 2023
0644efb
modified sentences based on PR review comments
Gajithra Feb 7, 2023
4e4e24b
updated the 'get_object_type_dynamic' with new name
Gajithra Feb 7, 2023
ea37728
Merge pull request #7 from rootcodelabs/RAOC-5
Gajithra Feb 7, 2023
47163e8
Merge pull request #6 from rootcodelabs/RAOC-10
Gajithra Feb 7, 2023
34dea76
directly return entity without assigning it to an unused variable as …
Gajithra Feb 7, 2023
22d7098
modified line 184 to safeguard against KeyErrors as suggested in PR r…
Gajithra Feb 7, 2023
a77350b
added suggestion to code change in line 185 as mentioned in the PR
Gajithra Feb 7, 2023
7e6352c
added suggestion to code change in line 185 as mentioned in the PR
Gajithra Feb 7, 2023
1658e99
modified after running grammerly
Gajithra Feb 9, 2023
85b022b
Added TrackerKnowledgeBase class in tracker.py to set object type slot
Gajithra Feb 13, 2023
ac01e97
modified class name
Gajithra Feb 13, 2023
f781e24
removed passing object_type value as an argument in functions
Gajithra Feb 13, 2023
13e21f6
imported TrackerKnowledgeBase class | modified function based on chan…
Gajithra Feb 13, 2023
262d01e
Merge pull request #9 from rootcodelabs/RAOC-3
Gajithra Feb 13, 2023
b876db4
Merge pull request #10 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Feb 13, 2023
6d39ec7
Merge pull request #8 from rootcodelabs/RAOC-6
Gajithra Feb 13, 2023
d8b83b5
resolved merge conflict #11
Gajithra Feb 13, 2023
5ae97ba
Merge pull request #14 from RasaHQ/main
Gajithra Feb 13, 2023
e5cc58f
Merge pull request #15 from rootcodelabs/OSS-668
Gajithra Feb 13, 2023
2fe0ca5
Merge pull request #16 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Feb 13, 2023
47a3e77
Merge pull request #17 from rootcodelabs/OSS-668-PR-CHANGES
Dilanka96 Feb 14, 2023
639b91b
Merge pull request #18 from rootcodelabs/RAOC-9
Dilanka96 Feb 14, 2023
893c450
removed get_object_types from KnowledgeBase class | removed async fro…
Gajithra Feb 28, 2023
2374ab6
removed await in line 136 after convering get_object_types function f…
Gajithra Feb 28, 2023
df07755
Merge branch 'RAOC-9' of https://github.com/rootcodelabs/rasa-sdk int…
Gajithra Feb 28, 2023
92769dd
modified code to set the object type slot temporarily using the metho…
Gajithra Feb 28, 2023
adfb4f4
removing tracker.py which contained a method to temporarily set the o…
Gajithra Feb 28, 2023
c77c4b1
removed the import from tracker.py
Gajithra Feb 28, 2023
a05abd5
removed code that gets current_object_type from the slot
Gajithra Feb 28, 2023
206b419
converted match_extracted_entities_to_object_types function from asyn…
Gajithra Feb 28, 2023
6b8082d
removed await for match_extracted_entities_to_object_types function
Gajithra Feb 28, 2023
7656a3f
modified action logic after assigning extracted object type to an obj…
Gajithra Feb 28, 2023
8fc0f35
Merge pull request #19 from RasaHQ/main
Gajithra Feb 28, 2023
2f1ff42
Merge pull request #20 from rootcodelabs/OSS-668
Gajithra Feb 28, 2023
69acb73
Merge pull request #21 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Feb 28, 2023
57097a3
Merge pull request #12 from rootcodelabs/RAOC-9
Gajithra Mar 1, 2023
4d25a7a
Merge pull request #22 from rootcodelabs/OSS-668-PR-CHANGES
Dilanka96 Mar 1, 2023
f79ed4b
Unit test functions
Dilanka96 Mar 1, 2023
0374bb6
Merge pull request #23 from rootcodelabs/RAOC-1
Gajithra Mar 1, 2023
f755e38
Resolved merge conflict
Dilanka96 Mar 1, 2023
e2d71b5
Merge pull request #5 from rootcodelabs/RAOC-2
Gajithra Mar 1, 2023
27adc46
Formatted with black
Dilanka96 Mar 1, 2023
4dac65c
Merge branch 'OSS-668-PR-CHANGES' of https://github.com/rootcodelabs/…
Dilanka96 Mar 1, 2023
8e0536a
Added line
Dilanka96 Mar 1, 2023
6be53a9
Removed tab
Dilanka96 Mar 1, 2023
6ea5907
Resolved all linting issues - flake8
Dilanka96 Mar 2, 2023
8e268a5
resolving merge conflicts
Gajithra Mar 2, 2023
9bd91a3
Fixed Errors
Dilanka96 Mar 2, 2023
e6d7b4c
Merge pull request #25 from rootcodelabs/RAOC-1
Gajithra Mar 6, 2023
e8fde63
Merge pull request #26 from RasaHQ/main
Gajithra Mar 6, 2023
8b5b80a
Merge pull request #27 from rootcodelabs/OSS-668
Gajithra Mar 6, 2023
0436f77
formatted with black
Gajithra Mar 10, 2023
3e2cd7d
Merge pull request #24 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Mar 10, 2023
dea181c
Merge pull request #28 from RasaHQ/main
Gajithra Mar 22, 2023
d30fe8c
removed entity:attribute appearing twice
Gajithra Mar 31, 2023
10ef8c5
removed white space to meet max line charac requirement
Gajithra Mar 31, 2023
3091b1a
entered variables into new line to meet max line charac requirements
Gajithra Mar 31, 2023
4bb6842
removed print statement
Gajithra Mar 31, 2023
27b5ba6
reverting the line indentation
Gajithra Mar 31, 2023
32e5925
Merge pull request #30 from RasaHQ/main
Gajithra Mar 31, 2023
1e67a2f
Merge pull request #31 from rootcodelabs/OSS-668
Gajithra Mar 31, 2023
3d321da
formatted code with black
Gajithra Mar 31, 2023
2b89b8f
Merge pull request #29 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Mar 31, 2023
f3cc64e
improved language and fixed typos
Gajithra Apr 2, 2023
bace082
added get_object_types method in KnowledgeBase parent class
Gajithra Apr 2, 2023
4771ff4
renamned entities_values to entity_names in line 188
Gajithra Apr 2, 2023
618b883
removed duplicated line in line 148
Gajithra Apr 2, 2023
892f161
modified return statement in function docstring
Gajithra Apr 2, 2023
2e610e3
updated the match_extracted_entities_to_object_types doc string to me…
Gajithra Apr 2, 2023
af65873
Merge pull request #32 from rootcodelabs/OSS-668
Gajithra Apr 2, 2023
4ae858b
renamed match_extracted_entities_to_object_types to match_extracted_e…
Gajithra Apr 2, 2023
c062dff
renamed match_extracted_entities_to_object_types to match_extracted_e…
Gajithra Apr 2, 2023
bfe9d87
reverting a change that wasn't made
Gajithra Apr 2, 2023
3e9a314
improved doc string
Gajithra Apr 2, 2023
3acfd96
Merge pull request #38 from RasaHQ/main
Gajithra Apr 4, 2023
c8bb933
Merge pull request #39 from rootcodelabs/OSS-668
Gajithra Apr 4, 2023
e6a2f54
Merge pull request #33 from rootcodelabs/RAOC-15
Gajithra Apr 6, 2023
4ab7c1e
resolved merge conflict
Gajithra Apr 6, 2023
e277d98
Merge pull request #35 from rootcodelabs/RAOC-18
Gajithra Apr 6, 2023
b899216
Merge pull request #42 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Apr 6, 2023
3c68735
Merge pull request #36 from rootcodelabs/RAOC-24
Gajithra Apr 6, 2023
a18b00a
Merge pull request #43 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Apr 6, 2023
c2c45db
Merge pull request #37 from rootcodelabs/RAOC-16
Gajithra Apr 6, 2023
eff43c8
Merge pull request #44 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Apr 6, 2023
9cacb65
Merge pull request #45 from rootcodelabs/RAOC-17
Gajithra Apr 6, 2023
fe5458f
Merge pull request #46 from RasaHQ/main
Gajithra Apr 6, 2023
684c474
Merge pull request #47 from rootcodelabs/OSS-668
Gajithra Apr 6, 2023
5b4d380
changed to
Gajithra Apr 11, 2023
f7e9703
added comment on slot object type resetting
Gajithra Apr 11, 2023
f358fb8
Merge pull request #48 from RasaHQ/main
Gajithra Apr 18, 2023
f3d239d
Merge pull request #49 from RasaHQ/main
Gajithra Apr 19, 2023
5195f59
Merge pull request #50 from rootcodelabs/OSS-668
Gajithra Apr 19, 2023
61a0844
modified if condition in run to check whether latest message attribute
Gajithra Apr 19, 2023
2f21f12
Merge pull request #51 from rootcodelabs/RAOC-28
Gajithra Apr 19, 2023
1ca093a
Merge pull request #52 from rootcodelabs/RAOC-27
Dilanka96 Apr 19, 2023
0c952d7
Resolved test_actions failures
Dilanka96 Apr 26, 2023
5856971
Merge pull request #53 from RasaHQ/main
Gajithra Apr 26, 2023
dfe7869
Merge pull request #54 from rootcodelabs/OSS-668
Gajithra Apr 26, 2023
795f9c8
Added unit test assertion
Dilanka96 Apr 27, 2023
f575f56
Modified test_actions.py and actions.py
Dilanka96 Apr 27, 2023
7f8b10a
Merge pull request #55 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Apr 27, 2023
0e490b5
Merge pull request #56 from rootcodelabs/RAOC-23
Dilanka96 Apr 27, 2023
85ebd90
Merge pull request #58 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Apr 28, 2023
0835ee2
modified code logic to include in one line
Gajithra Apr 28, 2023
5ce1658
formatted code with black
Gajithra Apr 28, 2023
9182e11
Merge pull request #59 from rootcodelabs/RAOC-27
Gajithra May 3, 2023
d63e0ed
Merge pull request #57 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 3, 2023
3e447ad
Merge pull request #60 from RasaHQ/main
Gajithra May 3, 2023
0ec440f
Fixed formatting
Dilanka96 May 3, 2023
1cf9263
added comment about slot_object_type resetting
Gajithra May 3, 2023
f4549e9
Merge pull request #61 from rootcodelabs/RAOC-22
Gajithra May 3, 2023
be730a3
Merge pull request #62 from rootcodelabs/OSS-668
Gajithra May 3, 2023
363a101
Merge pull request #63 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 3, 2023
433991a
formatted code with black
Gajithra May 3, 2023
32d89ac
fixed typos
Gajithra May 3, 2023
6b57413
Merge pull request #64 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 3, 2023
c9fae11
Reformatted actions file
Dilanka96 May 3, 2023
5ef720e
Merge pull request #65 from rootcodelabs/OSS-668
Gajithra May 5, 2023
17b1f40
resolve lint code issue caused by inverted comma
Gajithra May 5, 2023
185f788
Merge pull request #66 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 5, 2023
cd62d6b
Merge pull request #67 from rootcodelabs/OSS-668
Gajithra May 5, 2023
aa0b218
added a comment on replacing new_request with has_attribute_in_latest
Gajithra May 5, 2023
10fc38d
Merge pull request #68 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 8, 2023
b414d03
modified doc string in run
Gajithra May 8, 2023
8abd6c0
Merge pull request #69 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 8, 2023
35faf2b
Fixed trailing whitespace issue
Dilanka96 May 8, 2023
7c87564
removed keys from entities payload that are not needed for test_utils.py
Gajithra May 8, 2023
473e76e
Merge pull request #70 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 8, 2023
9aac3d2
Merge pull request #71 from RasaHQ/main
Gajithra May 11, 2023
e6b880f
added function docstring in get_object_types() to look up from parent…
Gajithra May 17, 2023
8a846d7
removed repeated info in docstring
Gajithra May 17, 2023
2cac3cb
modified comment on resetting object type slot based on review sugges…
Gajithra May 17, 2023
f402633
replaced has_attribute_in_latest with has_attribute_in_latest_message
Gajithra May 17, 2023
3dd9c55
Merge pull request #72 from rootcodelabs/OSS-668
Gajithra May 17, 2023
097de25
Fixed linting issues
Dilanka96 May 17, 2023
ffb5818
Reformatted using black
Dilanka96 May 17, 2023
3048e5b
added a rough code on testing the tracker events
Gajithra May 17, 2023
30afc38
Merge pull request #73 from RasaHQ/main
Gajithra May 17, 2023
523319c
Merge pull request #74 from rootcodelabs/OSS-668
Gajithra May 17, 2023
b0a7f9a
Merge branch 'OSS-668-PR-CHANGES' of https://github.com/rootcodelabs/…
Gajithra May 17, 2023
7bacf54
removed import logging
Gajithra May 18, 2023
afa3b48
added print statements for testing
Gajithra May 18, 2023
ff1a1c4
resolved python mutable issue
Gajithra May 19, 2023
acfe716
removed print statements and cleaned the code
Gajithra May 19, 2023
0527396
removed import copy
Gajithra May 19, 2023
7d7a4e8
removed trailing white space in line 157
Gajithra May 19, 2023
6442349
removed trailing white space in line 157
Gajithra May 19, 2023
118b4be
added spaces between code lines
Gajithra May 19, 2023
7e7bda0
added spaces between code lines
Gajithra May 19, 2023
f4e88d7
added space
Gajithra May 19, 2023
e057fc0
Merge pull request #75 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra May 19, 2023
e653150
Fixed formatting and lint code
Dilanka96 May 19, 2023
f8ce8ea
Merge pull request #76 from RasaHQ/main
Gajithra May 29, 2023
1c0bf05
Merge pull request #77 from rootcodelabs/OSS-668
Gajithra Jun 5, 2023
592b90d
made a copy of slots using copy.deepcopy() | modified assertion to ch…
Gajithra Jun 5, 2023
b1a5489
format with black
Gajithra Jun 5, 2023
f9bcdee
Merge pull request #79 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Jun 5, 2023
7547763
added comments in tracker event assertion code block
Gajithra Jun 5, 2023
29adf9c
Merge pull request #81 from rootcodelabs/OSS-668
Gajithra Jun 5, 2023
f8ac1bf
formatted code
Gajithra Jun 5, 2023
13a766a
Merge pull request #80 from rootcodelabs/OSS-668-PR-CHANGES
Gajithra Jun 5, 2023
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
29 changes: 29 additions & 0 deletions changelog/922.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Problem
Rasa knowledge base actions cannot infer the object type of an object directly from the user message without the user first asking to list all objects related to that object type. This prevents action_query_knowledge from providing a suitable response when a user asks for a certain attribute of an object even though the knowledge base has the relevant information. That is, the knowledge base actions require the slot `object_type` to be set to one of the primary key values in the knowledge base for it to search through the objects. Here is an example:
```
Your input -> what is the price range of Berlin Burrito Company?
Sorry, I'm not sure I understand. Can you rephrase?
Your input -> list some restaurants
Found the following objects of type 'restaurant':
1: Gong Gan
2: I due forni
3: Pfefferberg
4: Lụa Restaurant
5: Donath
Your input -> what is the price range of Berlin Burrito Company?
'Berlin Burrito Company' has the value 'cheap' for attribute 'price-range'.
```

## Proposed solution
- The improvement requires changes to the classes ActionQueryKnowledgeBase and InMemoryKnowledgeBase under rasa-sdk.
- The `object_type` can be inferred by utilizing the entity extraction (DIET) where object types are used as entities to annotate object names.
This also requires changes to be made to slot management to enable dynamic inference of `object_type`.
- The scope of the suggested solution is limited to user queries where they ask for an attribute of a given object without mentioning the object type and without needing to first ask for a list of options of the corresponding object type.
- E.g.: If the user asks for ‘price range of Berlin Burrito Company’, then rasa will extract and set attribute slot value to ‘price-range’ and hotel slot value to ‘Berlin Burrito Company’. From this, it can be inferred that the user is talking about the object type ‘hotel’.

## Summary of Changes
- To enable the inference of `object_type` using the entities the following changes were made to the existing code base:
- Extract the list of object_types from our knowledge base using a new method `get_object_types()` in `storage.py` for the `InMemoryKnowledgeBase` class.
- A new method named `match_extracted_entities_to_object_type()` was added in `utils.py` to infer the object type of a given object using the entities and list of object types
- The relevant logic was added in `actions.py` to infer the object type using the above functionalities when the object type slot is not set.
- To enable dynamic inference of `object_type`, changes to slot management are also required. Currently, the change is to reset the `object_type` slot to `None` after every conversation turn.
60 changes: 41 additions & 19 deletions rasa_sdk/knowledge_base/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
SLOT_LISTED_OBJECTS,
get_object_name,
get_attribute_slots,
match_extracted_entities_to_object_type,
)
from rasa_sdk import utils
from rasa_sdk.executor import CollectingDispatcher
Expand Down Expand Up @@ -112,13 +113,12 @@ async def run(
tracker: Tracker,
domain: "DomainDict",
) -> List[Dict[Text, Any]]:
"""Executes this action.

If the user ask a question about an attribute,
"""
Executes this action. If the user ask a question about an attribute,
the knowledge base is queried for that attribute. Otherwise, if no
attribute was detected in the request or the user is talking about a new
object type, multiple objects of the requested type are returned from the
knowledge base.
attribute was detected in the latest request it assumes user is talking
about a new object type and, multiple objects of the requested type are
returned from the knowledge base.

Args:
dispatcher: the dispatcher
Expand All @@ -131,22 +131,37 @@ async def run(
object_type = tracker.get_slot(SLOT_OBJECT_TYPE)
last_object_type = tracker.get_slot(SLOT_LAST_OBJECT_TYPE)
attribute = tracker.get_slot(SLOT_ATTRIBUTE)
has_mention = tracker.get_slot(SLOT_MENTION) is not None

new_request = object_type != last_object_type
# check if attribute entity is found in latest user message. This is used
# to track whether the request is to query objects or query attributes
has_attribute_in_latest_message = any(
entity.get("entity") == "attribute"
for entity in tracker.latest_message["entities"]
)

if not object_type:
# object type always needs to be set as this is needed to query the
# knowledge base
dispatcher.utter_message(response="utter_ask_rephrase")
return []

if not attribute or new_request:
# sets the object type dynamically from entities if object_type is not
# found in user query
object_types = self.knowledge_base.get_object_types()
object_type = match_extracted_entities_to_object_type(tracker, object_types)
set_object_type_slot_event = [SlotSet(SLOT_OBJECT_TYPE, object_type)]
tracker.add_slots(
set_object_type_slot_event
) # temporarily set the `object_type_slot` to extracted value

if object_type and not has_attribute_in_latest_message:
return await self._query_objects(dispatcher, object_type, tracker)
elif attribute:
elif object_type and attribute:
ancalita marked this conversation as resolved.
Show resolved Hide resolved
return await self._query_attribute(
dispatcher, object_type, attribute, tracker
)

if last_object_type and has_mention and attribute:
return await self._query_attribute(
dispatcher, last_object_type, attribute, tracker
ancalita marked this conversation as resolved.
Show resolved Hide resolved
)

dispatcher.utter_message(response="utter_ask_rephrase")
return []

Expand All @@ -167,15 +182,13 @@ async def _query_objects(
object_attributes = await utils.call_potential_coroutine(
self.knowledge_base.get_attributes_of_object(object_type)
)

# get all set attribute slots of the object type to be able to filter the
# list of objects
attributes = get_attribute_slots(tracker, object_attributes)
# query the knowledge base
objects = await utils.call_potential_coroutine(
self.knowledge_base.get_objects(object_type, attributes)
)

await utils.call_potential_coroutine(
self.utter_objects(dispatcher, object_type, objects)
)
Expand All @@ -189,8 +202,13 @@ async def _query_objects(

last_object = None if len(objects) > 1 else objects[0][key_attribute]

# To prevent the user to first ask to list the objects for an object type,
# the object type has to be extracted while the action is executed.
# Therefore we need to reset the SLOT_OBJECT_TYPE to
# None to enable this functionality.

slots = [
SlotSet(SLOT_OBJECT_TYPE, object_type),
SlotSet(SLOT_OBJECT_TYPE, None),
ancalita marked this conversation as resolved.
Show resolved Hide resolved
SlotSet(SLOT_MENTION, None),
SlotSet(SLOT_ATTRIBUTE, None),
SlotSet(SLOT_LAST_OBJECT, last_object),
Expand Down Expand Up @@ -219,7 +237,6 @@ async def _query_attribute(

Returns: list of slots
"""

object_name = get_object_name(
tracker,
self.knowledge_base.ordinal_mention_mapping,
Expand Down Expand Up @@ -258,8 +275,13 @@ async def _query_attribute(
)
)

# To prevent the user to first ask to list the objects for an object type,
# the object type has to be extracted while the action is executed.
# Therefore we need to reset the SLOT_OBJECT_TYPE to
# None to enable this functionality.

slots = [
SlotSet(SLOT_OBJECT_TYPE, object_type),
SlotSet(SLOT_OBJECT_TYPE, None),
ancalita marked this conversation as resolved.
Show resolved Hide resolved
SlotSet(SLOT_ATTRIBUTE, None),
SlotSet(SLOT_MENTION, None),
SlotSet(SLOT_LAST_OBJECT, object_identifier),
Expand Down
12 changes: 10 additions & 2 deletions rasa_sdk/knowledge_base/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

class KnowledgeBase:
def __init__(self) -> None:

self.ordinal_mention_mapping = {
"1": lambda lst: lst[0],
"2": lambda lst: lst[1],
Expand Down Expand Up @@ -110,6 +109,12 @@ async def get_object(
"""
raise NotImplementedError("Method is not implemented.")

def get_object_types(self) -> List[Text]:
"""
Returns a list of object types from knowledge base data.
"""
raise NotImplementedError("Method is not implemented.")


class InMemoryKnowledgeBase(KnowledgeBase):
def __init__(self, data_file: Text) -> None:
Expand Down Expand Up @@ -206,7 +211,6 @@ async def get_object(
return None

objects = self.data[object_type]

key_attribute = await utils.call_potential_coroutine(
self.get_key_attribute_of_object(object_type)
)
Expand Down Expand Up @@ -242,3 +246,7 @@ async def get_object(
return None

return objects_of_interest[0]

def get_object_types(self) -> List[Text]:
ancalita marked this conversation as resolved.
Show resolved Hide resolved
"""See parent class docstring."""
return list(self.data.keys())
31 changes: 29 additions & 2 deletions rasa_sdk/knowledge_base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def resolve_mention(
listed_items = tracker.get_slot(SLOT_LISTED_OBJECTS)
last_object = tracker.get_slot(SLOT_LAST_OBJECT)
last_object_type = tracker.get_slot(SLOT_LAST_OBJECT_TYPE)
current_object_type = tracker.get_slot(SLOT_OBJECT_TYPE)

if not mention:
return None
Expand All @@ -95,7 +94,9 @@ def resolve_mention(
# for now we just assume that if the user refers to an object, for
# example via "it" or "that restaurant", they are actually referring to the last
# object that was detected.
if current_object_type == last_object_type:
# Since object type slot is reset to 'None' value, it is sufficient to only check
# whether the last_object_type is not None.
if last_object_type:
return last_object

return None
Expand Down Expand Up @@ -164,3 +165,29 @@ def reset_attribute_slots(
slots.append(SlotSet(attr, None))

return slots


def match_extracted_entities_to_object_type(
tracker: "Tracker",
object_types: List[Text],
) -> Optional[Text]:
"""
If the user ask a question about an attribute using an object name and
without specifying the object type, then this function searches the
corresponding object type. (e.g: when user asks'price range of B&B', this
function extracts the object type as 'hotel'). Here we assume that the user
message contains reference only to one object type in the knowledge base.

Args:
tracker: the tracker
object_types: list of object types in the knowledge base

Returns: the name of the object type if found, otherwise `None`.
"""
entities = tracker.latest_message.get("entities", [])
entity_names = [entity.get("entity") for entity in entities]
for entity in entity_names:
if entity in object_types:
return entity
ancalita marked this conversation as resolved.
Show resolved Hide resolved

return None
7 changes: 4 additions & 3 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def is_valid_version_number(v: Text) -> bool:
str(current_version.next_release_candidate("major")),
]
version = questionary.select(
f"Which {version} do you want to release?", choices=choices,
f"Which {version} do you want to release?",
choices=choices,
).ask()

if version:
Expand Down Expand Up @@ -255,8 +256,8 @@ def next_version(args: argparse.Namespace) -> Version:
def generate_changelog(version: Version) -> None:
"""Call towncrier and create a changelog from all available changelog entries."""
check_call(
["towncrier", "build", "--yes", "--version", str(version)], cwd=str(project_root())

["towncrier", "build", "--yes", "--version", str(version)],
cwd=str(project_root()),
)


Expand Down
Loading