# 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](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#building-a-simple-faq-assistant)

- [Memoization Policy](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#memoization-policy)

- [Response Selectors](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#response-selectors)
- [Building contextual assistants](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#building-contextual-assistants)

- [Business logic](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#business-logic)

- [Handling unexpected user input](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#handling-unexpected-user-input)

- [Generic interjections](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#generic-interjections)

- [Contextual questions](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#contextual-questions)
  - [Failing gracefully](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#failing-gracefully)

- [Fallback policy](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#fallback-policy)

- [Out of scope intent](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#out-of-scope-intent)
  - [More complex contextual conversations](https://legacy-docs-v1.rasa.com/1.8.1/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 with the MemoizationPolicy
> 
> - Handling FAQs 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.

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

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

```md
## greet
* greet
  - utter_greet

## thank
* thank
  - utter_noworries

## goodbye
* bye
  - utter_bye
```

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

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

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

You can now train a first model and test the bot, by running the following commands:

```bash
rasa train
rasa shell
```

### Response Selectors

The [ResponseSelector](https://legacy-docs-v1.rasa.com/1.8.1/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.8.1/dialogue-elements/small-talk/#small-talk) and FAQ messages in a simple manner. To use the Response Selector we need to add it to the end of the expanded [supervised_embeddings](/content/docs/rasa/nlu/choosing-a-pipeline/#section-supervised-embeddings-pipeline/index.html) NLU pipeline in our `config.yml`:

```yaml
pipeline:
- name: "WhitespaceTokenizer"
- name: "ResponseSelector"
```

### Next Steps
Using the features we described in this tutorial, you can easily build a context-less assistant. When you’re ready to enhance your assistant with context, check out [Building contextual assistants](https://legacy-docs-v1.rasa.com/1.8.1/user-guide/building-assistants/#build-contextual-assistant).

## Building contextual assistants

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
> 
> - Handling unexpected user input
> 
> - Failing gracefully
> 
> - More complex contextual conversations

### Business logic

A lot of conversational assistants have user goals that involve collecting a bunch of information from the user before being able to do something for them. This is called slot filling. For this type of use case, we can use Forms and our FormPolicy.

As an example, we will build out the SalesForm from Sara. The user wants to contact our sales team and for this we need to gather the following pieces of information:

> - Their job
> 
> - Their bot use case
> 
> - Their name
> 
> - Their email
> 
> - Their budget
> 
> - Their company

```python
from rasa_sdk.forms import FormAction

class SalesForm(FormAction):
    def name(self):
        return "sales_form"
```

```yaml
slots:
  company:
    type: unfeaturized
  job_function:
    type: unfeaturized
  person_name:
    type: unfeaturized
  budget:
    type: unfeaturized
  business_email:
    type: unfeaturized
  use_case:
    type: unfeaturized
```

### Handling unexpected user input

In real situations, the user will often behave differently. In this section we’ll go through various forms of “interjections” and how to handle them within Rasa. The decision to handle these types of user input should come from reviewing real conversations.

If you have generic interjections that should always have the same single response no matter the context, you can use the [Mapping Policy](https://legacy-docs-v1.rasa.com/1.8.1/core/policies/#mapping-policy) to handle these.

### Failing gracefully

It's important to ensure your assistant fails gracefully. One common failure is low NLU confidence, which can be handled with the TwoStageFallbackPolicy.

```yaml
policies:
  - name: TwoStageFallbackPolicy
    nlu_threshold: 0.8
```

### More complex contextual conversations

For user goals that don't fall under the category of simple business logic, you will need to use stories and context to help the user achieve their goals. The AugmentedMemoizationPolicy can provide more robust handling for conversational flows.

In summary, using Rasa you can create a powerful contextual assistant by taking advantage of its features to manage both simple FAQ tasks and more complex interactions.
