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

Knowledge base actions enable you to handle the following kind of conversations:

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.

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.

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. Here is an example containing 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
        }
    ]
}

Every object in your knowledge base should have at least the "name" and "id" fields to use the default implementation.

Define the NLU Data

In this section:

To specify that the user wants to retrieve information from the knowledge base, define a new intent called query_knowledge_base with variations of requests:

## 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)?
- do you know what [cuisine](attribute) the [last one](mention:LAST) has?
- does the [first one](mention:1) have [outside seating](attribute:outside-seating)?
- what is the [price range](attribute:price-range) of [Berlin Burrito Company](restaurant)?
- what about [I due forni](restaurant)?
- can you tell me the [price range](attribute) of [that restaurant](mention)?
- what [cuisine](attribute) do [they](mention) have?

The example shows related to the restaurant domain. Add examples for every object type in your knowledge base to the query_knowledge_base intent.

Create an Action to Query your Knowledge Base

To create your own knowledge base action, inherit ActionQueryKnowledgeBase and pass the knowledge base to the constructor:

class MyKnowledgeBaseAction(ActionQueryKnowledgeBase):
    def __init__(self):
        knowledge_base = InMemoryKnowledgeBase("data.json")
        super().__init__(knowledge_base)

This action will enable querying the knowledge base when the intent query_knowledge_base is detected.