Knowledge Base Actions
Knowledge Base Actions
Warning
This feature is experimental. We introduce experimental features to get feedback from our community, so we encourage you to try it out! However, the functionality might be changed or 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 be used to store complex data structures. We suggest you get started by using the InMemoryKnowledgeBase.
To initialize an InMemoryKnowledgeBase, you need to provide the data in a json file. The following example contains data about restaurants and hotels. The json structure should contain a key for every object type, i.e. "restaurant" and "hotel". Every object type maps to a list of objects – here we have a list of 3 restaurants and a list of 3 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, called, for example, data.json, you will be able use this data file to create your InMemoryKnowledgeBase, which will be passed to the action that queries the knowledge base.
Every object in your knowledge base should have at least the "name" and "id" fields to use the default implementation. If it doesn’t, you’ll have to customize your InMemoryKnowledgeBase.
Define the NLU Data
In this section:
- we will introduce a new intent,
query_knowledge_base - we will to annotate
mentionentities so that our model detects indirect mentions of objects like “the first one” - we will use synonyms extensively
For the bot to understand that the user wants to retrieve information from the knowledge base, you need to define a new intent. We will call it query_knowledge_base.
The above example just shows examples related to the restaurant domain. You should add examples for every object type that exists in your knowledge base to the same query_knowledge_base intent.
In addition to adding a variety of training examples for each query type, you need to specify the and annotate the following entities in your training examples:
object_type: Whenever a training example references a specific object type from your knowledge base, the object type should be marked as an entity. Use synonyms to map e.g.restaurantstorestaurant, the correct object type listed as a key in the knowledge base.mention: If the user refers to an object via “the first one”, “that one”, or “it”, you should mark those terms asmention. We also use synonyms to map some of the mentions to symbols. You can learn about that in resolving mentions.attribute: All attribute names defined in your knowledge base should be identified asattributein the NLU data. Again, use synonyms to map variations of an attribute name to the one used in the knowledge base.
Create an Action to Query your Knowledge Base
To create your own knowledge base action, you need to inherit ActionQueryKnowledgeBase and pass the knowledge base to the constructor of ActionQueryKnowledgeBase.
from rasa_sdk.knowledge_base.storage import InMemoryKnowledgeBase
from rasa_sdk.knowledge_base.actions import ActionQueryKnowledgeBase
class MyKnowledgeBaseAction(ActionQueryKnowledgeBase):
def __init__(self):
knowledge_base = InMemoryKnowledgeBase("data.json")
super().__init__(knowledge_base)
Whenever you create an ActionQueryKnowledgeBase, you need to pass a KnowledgeBase to the constructor. It can be either an InMemoryKnowledgeBase or your own implementation of a KnowledgeBase (see Creating Your Own Knowledge Base). You can only pull information from one knowledge base, as the usage of multiple knowledge bases at the same time is not supported.
How It Works
ActionQueryKnowledgeBase looks at both the entities that were picked up in the request as well as the previously set slots to decide what to query for.
Query the Knowledge Base for Objects
In order to query the knowledge base for any kind of object, the user’s request needs to include the object type. Let’s look at an example:
Can you please name some restaurants?
This question includes the object type of interest: “restaurant.” The bot needs to pick up on this entity in order to formulate a query – otherwise, the action would not know what objects the user is interested in.
Query the Knowledge Base for an Attribute of an Object
If the user wants to obtain specific information about an object, the request should include both the object and attribute of interest.
Resolve Mentions
Following along from the above example, users may not always refer to restaurants by their names. Users can either refer to the object of interest by its name, e.g. “Berlin Burrito Company” (representation string of the object), or they may refer to a previously listed object via a mention, for example:
Customization
Customizing ActionQueryKnowledgeBase
You can overwrite the following two functions of ActionQueryKnowledgeBase if you’d like to customize what the bot says to the user:
utter_objects()utter_attribute_value()
Creating Your Own Knowledge Base Actions
Customizing the InMemoryKnowledgeBase
Creating Your Own Knowledge Base
If you have more data or if you want to use a more complex data structure that, for example, involves relations between different objects, you can create your own knowledge base implementation. Just inherit KnowledgeBase and implement the methods get_objects(), get_object(), and get_attributes_of_object().
A common problem in conversational AI is that users do not only refer to certain objects by their names, but also use reference terms such as “the first one” or “it”. We need to keep track of the information that was presented to resolve these mentions to the correct object.
In addition, users may want to obtain detailed information about objects during a conversation – for example, whether a restaurant has outside seating, or how expensive it is. In order to respond to those user requests, knowledge about the restaurant domain is needed. Since the information is subject to change, hard-coding the information isn’t the solution.
To handle the above challenges, Rasa can be integrated with knowledge bases. To use this integration, you can create a custom action that inherits from ActionQueryKnowledgeBase, a pre-written custom action that contains the logic to query a knowledge base for objects and their attributes.