Knowledge Base Actions
Knowledge Base Actions
Warning: This feature is experimental.
This feature introduces experimental capabilities to get feedback from the community. However, the functionality might change or be removed in the future. If you have feedback (positive or negative), please share it with us on the forum.
Using ActionQueryKnowledgeBase
Create a Knowledge Base
The data used to answer the user’s requests will be stored in a knowledge base. A knowledge base can store complex data structures. We suggest starting with the InMemoryKnowledgeBase. Once you want to work with larger datasets, you can switch to a custom knowledge base (see Creating Your Own Knowledge Base).
To initialize an InMemoryKnowledgeBase, provide the data in a JSON file. The following example contains data about restaurants and hotels.
{
"restaurant": [
{
"id": 0,
"name": "Donath",
"cuisine": "Italian",
"outside-seating": true,
"price-range": "mid-range"
},
{
"id": 1,
"name": "Berlin Burrito Company",
"cuisine": "Mexican",
"outside-seating": false,
"price-range": "cheap"
},
{
"id": 2,
"name": "I due forni",
"cuisine": "Italian",
"outside-seating": true,
"price-range": "mid-range"
}
],
"hotel": [
{
"id": 0,
"name": "Hilton",
"price-range": "expensive",
"breakfast-included": true,
"city": "Berlin",
"free-wifi": true,
"star-rating": 5,
"swimming-pool": true
},
{
"id": 1,
"name": "Hilton",
"price-range": "expensive",
"breakfast-included": true,
"city": "Frankfurt am Main",
"free-wifi": true,
"star-rating": 4,
"swimming-pool": false
},
{
"id": 2,
"name": "B&B",
"price-range": "mid-range",
"breakfast-included": false,
"city": "Berlin",
"free-wifi": false,
"star-rating": 1,
"swimming-pool": false
}
]
}
Once the data is defined in a JSON file (e.g., data.json), use this data file to create your InMemoryKnowledgeBase, which will be passed to the action querying the knowledge base.
Define the NLU Data
In this section:
- Introduce a new intent,
query_knowledge_base - Annotate
mentionentities for indirect mentions of objects. - Use synonyms extensively.
To retrieve information from the knowledge base, define a new intent called query_knowledge_base. The request can either be:
(1) obtaining a list of objects of a specific type, or (2) knowing about a specific attribute of an object:
## intent:query_knowledge_base
- what [restaurants](object_type:restaurant) can you recommend?
- list some [restaurants](object_type:restaurant)
- can you name some [restaurants](object_type:restaurant) please?
- can you show me some [restaurant](object_type:restaurant) options?
- list [German](cuisine) [restaurants](object_type:restaurant)
- do you have any [mexican](cuisine) [restaurants](object_type:restaurant)?
- do you know the [price range](attribute:price-range) of [that one](mention)?
- what [cuisine](attribute) is [it](mention)?
...
Create an Action to Query your Knowledge Base
To create your own knowledge base action, inherit ActionQueryKnowledgeBase and pass the knowledge base to its constructor:
class MyKnowledgeBaseAction(ActionQueryKnowledgeBase):
def __init__(self):
knowledge_base = InMemoryKnowledgeBase("data.json")
super().__init__(knowledge_base)
Ensure the action is added to your domain file:
actions:
- action_query_knowledge_base
How It Works
ActionQueryKnowledgeBase looks at the entities picked up in the request and previously set slots to decide what to query for.
Query the Knowledge Base for Objects
In the user's request, the object type must be included. Example:
Can you please name some restaurants?
Query the Knowledge Base for an Attribute of an Object
To get specific information about an object, both the object and attribute of interest must be included.
Resolve Mentions
Users may not always refer to objects explicitly. Two types of mentions can be resolved: ordinal mentions (e.g., “the first one”) and others (e.g., “it”).
Customization
Creating Your Own Knowledge Base Actions
If you wish to handle more complex use cases, write a custom action that inherits from KnowledgeBase.
Customizing the InMemoryKnowledgeBase
By overwriting functions, you can track objects and customize what the bot says. Functions to customize include:
get_key_attribute_of_object()get_representation_function_of_object()set_ordinal_mention_mapping()
If more data or complex structures are needed, create your own knowledge base implementation by inheriting from KnowledgeBase.