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.

You can find a complete example in examples/knowledgebasebot ( knowledge base bot), as well as instructions for implementing this custom action below.

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.

Sample JSON Structure

{
    "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
        }
    ]
}

Define the NLU Data

To enable the bot to understand user requests about the knowledge base, a new intent called query_knowledge_base should be defined. The bot should detect various ways users might request information about the objects stored in the knowledge base.

Example of training data annotations for the bot:

## intent:query_knowledge_base
- what [restaurants](object_type:restaurant) can you recommend?
- can you name some [restaurants](object_type:restaurant)?
- what [cuisine](attribute) is [it](mention)?

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 its constructor:

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)  

Once the data is defined in a JSON file, you will be able to use this data file to create your InMemoryKnowledgeBase, which will be passed to the action that queries the knowledge base.

How It Works

ActionQueryKnowledgeBase utilizes user queries along with previously set slots to pull relevant data.

Resolve Mentions

The action can handle user references in various ways, whether through specific object names or reference terms like "the first one" or "it".