Knowledge Base Actions
These docs are for version 1.x of Rasa Open Source.
User Guide
- Installation
- Tutorial: Rasa Basics
- Tutorial: Building Assistants
- Command Line Interface
- Architecture
- Messaging and Voice Channels
- Testing Your Assistant
- Setting up CI/CD
- Validate Data
- Configuring the HTTP API
- Deploying Your Rasa Assistant
- Cloud Storage
Knowledge Base Actions
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.
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.
Using ActionQueryKnowledgeBase
Create a Knowledge Base
To initialize an InMemoryKnowledgeBase, you need to 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
}
]
}
Define the NLU Data
In this section:
- We will introduce a new intent,
query_knowledge_base. - We will annotate
mentionentities so that our model detects indirect mentions of objects. - We will use synonyms extensively.
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)
How It Works
ActionQueryKnowledgeBase looks at both the entities picked up in the request as well as the previously set slots to decide what to query for.
In order to query the knowledge base for any kind of object, the user’s request needs to include the object type.
Query the Knowledge Base for Objects
When the user’s request needs to include the object type, the bot needs to pick up on this entity in order to formulate a query.
Resolving Mentions
Our action can resolve terms such as “the first one” or “it” to the actual object in the knowledge base.
Customization
You can overwrite the methods of ActionQueryKnowledgeBase if you’d like to customize what the bot says to the user.
Creating Your Own Knowledge Base Actions
If you want to tackle more complex use cases, you can write your own custom action.
Customizing the InMemoryKnowledgeBase
You can customize your InMemoryKnowledgeBase by overwriting specific functions.