# Tutorial: Building Assistants

After following the basics of setting up an assistant in the [Rasa Tutorial](/content/docs/rasa/user-guide/rasa-tutorial/index.html), we’ll now walk through building a basic FAQ chatbot and then build a bot that can handle contextual conversations.

- **Building a simple FAQ assistant**:
  - [Memoization Policy](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#memoization-policy)
  - [Response Selectors](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#response-selectors)
- **Building a contextual assistant**:
  - [Handling business logic](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#handling-business-logic)
  - [Handling unexpected user input](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#handling-unexpected-user-input)
    - [Generic interjections](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#generic-interjections)
    - [Contextual questions](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#contextual-questions)
  - [Failing gracefully](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#failing-gracefully)
    - [Fallback policy](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#fallback-policy)
    - [Out of scope intent](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#out-of-scope-intent)
  - [More complex contextual conversations](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#more-complex-contextual-conversations)

## Building a simple FAQ assistant

FAQ assistants are the simplest assistants to build and a good place to get started. These assistants allow the user to ask a simple question and get a response. We’re going to build a basic FAQ assistant using features of Rasa designed specifically for this type of assistant.

In this section we’re going to cover the following topics:

- [Responding to simple intents](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/respond-with-memoization-policy) with the MemoizationPolicy
- [Handling FAQs](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/faqs-response-selector) using the ResponseSelector

You should first install Rasa using the [Step-by-step Installation Guide](/content/docs/rasa/user-guide/installation/#step-by-step-installation-guide/index.html) and then follow the [Rasa Tutorial](/content/docs/rasa/user-guide/rasa-tutorial/index.html) to make sure you know the basics.

To prepare for this tutorial, we’re going to create a new directory and start a new Rasa project.

```bash
mkdir rasa-assistant
rasa init
```

Let’s remove the default content from this bot, so that the `nlu.md`, `stories.md` and `domain.yml` files are empty.

### Memoization Policy

The MemoizationPolicy remembers examples from training stories for up to a `max_history` of turns. The number of “turns” includes messages the user sent, and actions the assistant performed. For the purpose of a simple, context-less FAQ bot, we only need to pay attention to the last message the user sent, and therefore we’ll set that to `1`.

You can do this by editing your `config.yml` file as follows:

```yaml
policies:
- name: MemoizationPolicy
  max_history: 1
- name: MappingPolicy
```

**Note**: The MappingPolicy is there because it handles the logic of the `/restart` intent, which allows you to clear the conversation history and start fresh.

Now that we’ve defined our policies, we can add some stories for the `goodbye`, `thank` and `greet` intents to the `stories.md` file:

```markdown
## greet
* greet
  - utter_greet

## thank
* thank
  - utter_noworries

## goodbye
* bye
  - utter_bye
```

We’ll also need to add the intents, actions and responses to our `domain.yml` file in the following sections:

```yaml
intents:
  - greet
  - bye
  - thank

responses:
  utter_noworries:
    - text: No worries!
  utter_greet:
    - text: Hi
  utter_bye:
    - text: Bye!
```

Finally, we’ll copy over some NLU data from Sara into our `nlu.md` file:

```markdown
## intent:greet
- Hi
- Hey
- Hi bot
- Hey bot
- Hello
- Good morning
- hi again
- hi folks

## intent:bye
- goodbye
- goodnight
- good bye
- good night
- see ya
- toodle-oo
- bye bye
- gotta go
- farewell

## intent:thank
- Thanks
- Thank you
- Thank you so much
- Thanks bot
- Thanks for that
- cheers
```

You can now train a first model and test the bot:

```bash
rasa train
rasa shell
```

This bot should now be able to reply to the intents we defined consistently, and in any order.

### Response Selectors

The [ResponseSelector](https://legacy-docs-v1.rasa.com/1.9.0/nlu/components/#response-selector) NLU component is designed to make it easier to handle dialogue elements like [Small Talk](https://legacy-docs-v1.rasa.com/1.9.0/dialogue-elements/small-talk/#small-talk) and FAQ messages in a simple manner. By using the ResponseSelector, you only need one story to handle all FAQs, instead of adding new stories every time you want to increase your bot’s scope.

Next, we’ll need to define the responses associated with these FAQs in a new file called `responses.md` in the `data/` directory:

```markdown
## ask channels
* faq/ask_channels
  - We have a comprehensive list of [supported connectors](/content/docs/core/connectors/index.html), but if you don't see the one you're looking for, you can always create a custom connector by following [this guide](/content/docs/rasa/user-guide/connectors/custom-connectors/index.html).

## ask languages
* faq/ask_languages
  - You can use Rasa to build assistants in any language you want!

## ask rasa x
* faq/ask_rasax
  - Rasa X is a tool to learn from real conversations and improve your assistant. Read more [here](/content/docs/rasa-x/index.html)
```

Now we can define the NLU training data for FAQs in the `nlu.md` file as shown:

```markdown
## intent: faq/ask_channels
- What channels of communication does rasa support?
- what channels do you support?
- what chat channels does rasa uses
- channels supported by Rasa

## intent: faq/ask_languages
- what language does rasa support?
- which language do you support?
- which languages supports rasa
- can I use rasa also for another laguage?
- languages supported

## intent: faq/ask_rasax
- I want information about rasa x
- i want to learn more about Rasa X
- what is rasa x?
- Can you tell me about rasa x?
- Tell me about rasa x
- tell me what is rasa x
```

The ResponseSelector should already be at the end of the NLU pipeline in our `config.yml`:

```yaml
language: en
pipeline:
  - name: WhitespaceTokenizer
  - name: RegexFeaturizer
  - name: LexicalSyntacticFeaturizer
  - name: CountVectorsFeaturizer
  - name: DIETClassifier
    epochs: 100
  - name: EntitySynonymMapper
  - name: ResponseSelector
    epochs: 100
```

After all of the changes are done, train a new model and test the modified FAQs:

```bash
rasa train
rasa shell
```

## Building a contextual assistant
Whether you’ve just created an FAQ bot or are starting from scratch, the next step is to expand your bot to handle contextual conversations. In this tutorial we’re going to cover a variety of topics:

- [Handling business logic](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#handling-business-logic)
- [Handling unexpected user input](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#handling-unexpected-user-input)
- [Failing gracefully](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#failing-gracefully)
- [More complex contextual conversations](https://legacy-docs-v1.rasa.com/1.9.0/user-guide/building-assistants/#more-complex-contextual-conversations)

You should check out detailed sections for specific implementations tailored to your needs.
