# 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](https://forum.rasa.com/).

- [Using `ActionQueryKnowledgeBase`](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#using-actionqueryknowledgebase)
  - [Create a Knowledge Base](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#create-a-knowledge-base)
  - [Define the NLU Data](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#define-the-nlu-data)
  - [Create an Action to Query your Knowledge Base](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#create-an-action-to-query-your-knowledge-base)
- [How It Works](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#how-it-works)
  - [Query the Knowledge Base for Objects](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#query-the-knowledge-base-for-objects)
  - [Query the Knowledge Base for an Attribute of an Object](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#query-the-knowledge-base-for-an-attribute-of-an-object)
  - [Resolve Mentions](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#resolve-mentions)
- [Customization](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#customization)
  - [Customizing `ActionQueryKnowledgeBase`](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#customizing-actionqueryknowledgebase)
  - [Creating Your Own Knowledge Base Actions](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#creating-your-own-knowledge-base-actions)
  - [Customizing the `InMemoryKnowledgeBase`](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#customizing-the-inmemoryknowledgebase)
  - [Creating Your Own Knowledge Base](https://legacy-docs-v1.rasa.com/1.7.3/core/knowledge-bases/#creating-your-own-knowledge-base)

## Using `ActionQueryKnowledgeBase`

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`. Once you want to start working with a large amount of data, you can switch to a custom knowledge base.

### 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. 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
        }
    ]
}
```

### Define the NLU Data

In this section:

- we will introduce a new intent, `query_knowledge_base`

- we will to annotate `mention` entities so that our model detects indirect mentions of objects like “the first one”

- we will use [synonyms](https://legacy-docs-v1.rasa.com/1.7.3/nlu/training-data-format/#entity-synonyms) extensively.

## Creating Your Own Knowledge Base Actions

`ActionQueryKnowledgeBase` should allow you to easily get started with integrating knowledge bases into your actions. However, the action can only handle two kind of user requests:

- the user wants to get a list of objects from the knowledge base
- the user wants to get the value of an attribute for a specific object

### 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()`

Note

There is a [tutorial](https://blog.rasa.com/integrating-rasa-with-knowledge-bases/) on our blog about how to use knowledge bases in custom actions.
