<jupyter_start><jupyter_text>Building a Simple Bot with the Rasa Stack![alt text](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTaX3LNhGcAe1HnPZSuWS0oH6af0LJHXcH7If1sQgLCFAT1chNGFg) Please make a copy of this notebook first!If you're interested in building chatbots or voice apps with the [Rasa Stack](https://rasa.com/products/rasa-stack/), you've come to the right place! In this notebook you'll build a complete chatbot with Rasa Stack, without having to install anything on your computer. Using Colaboratory, everything will get installed in a cloud machine for you, so you can focus on creating an interesting bot. We'll build a relatively simple bot, that just asks you about your mood and tries to cheer you up if you're feeling a bit down. The tutorial consists of three parts:*   Part 1: We'll start with a basic bot that can handle multi-turn dialogues but not understand natural language like "I'm doing well"*   Part 2: We'll add the abilitiy to understand natural language so that the bot can generalise to similar inputs that it hasn't seen before*   Part 3: We'll give you further resources so you can build your own.  Part 1: Build a basic bot  InstallationFirst we'll have to install Rasa Core on the server running this notebook. In this first step, your bot won't understand natural language yet. This happens in Step 2. **Run the cell below, this will take a couple of minutes to complete so this is a good time to scroll through the rest of the notebook.****Make sure to install the correct version. If you want to experiment with the latest master, use**: `!pip install -U git+https://github.com/rasahq/rasa_core.git`<jupyter_code>!pip install rasa_core;

import logging, io, json, warnings
logging.basicConfig(level="INFO")
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Writing StoriesA good place to start is by writing a few stories. These are example conversations that Rasa Core will learn from. The format works like this:A story starts with `` and you can give it a name. lines that start with `*` are messages sent by the user. Although you don't write the *actual* message, but rather the intent (and the entities) that represent what the user *means*. If you don't know about intents and entities, don't worry! We will talk about them more later. Lines that start with `-` are *actions* taken by your bot. In this case all of our actions are just messages sent back to the user, like `utter_greet`, but in general an action can do anything, including calling an API and interacting with the outside world.<jupyter_code>stories_md = """
## happy path               <!-- name of the story - just for debugging -->
* greet              
  - utter_greet
* mood_great               <!-- user utterance, in format intent[entities] -->
  - utter_happy
* mood_affirm
  - utter_happy
* mood_affirm
  - utter_goodbye
  
  
## sad path 1               <!-- this is already the start of the next story -->
* greet
  - utter_greet             <!-- action the bot should execute -->
* mood_unhappy
  - utter_cheer_up
  - utter_did_that_help
* mood_affirm
  - utter_happy
  
  

## sad path 2
* greet
  - utter_greet
* mood_unhappy
  - utter_cheer_up
  - utter_did_that_help
* mood_deny
  - utter_goodbye
  
## strange user
* mood_affirm
  - utter_happy
* mood_affirm
  - utter_unclear

## say goodbye
* goodbye
  - utter_goodbye

"""

%store stories_md > stories.md<jupyter_output><empty_output><jupyter_text>Defining a DomainThe domain specifies the universe that your bot lives in. You should list all of the intents and actions that show up in your stories. This is also the place to write templates, which contain the messages your bot can send back<jupyter_code>domain_yml = """
intents:
  - greet
  - goodbye
  - mood_affirm
  - mood_deny
  - mood_great
  - mood_unhappy

actions:
- utter_greet
- utter_cheer_up
- utter_did_that_help
- utter_happy
- utter_goodbye
- utter_unclear

templates:
  utter_greet:
  - text: "Hey! How are you?"

  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

  utter_did_that_help:
  - text: "Did that help you?"

  utter_unclear:
  - text: "I am not sure what you are aiming for."
  
  utter_happy:
  - text: "Great carry on!"

  utter_goodbye:
  - text: "Bye"
"""

%store domain_yml > domain.yml<jupyter_output><empty_output><jupyter_text>Training your Dialogue ModelNow comes the fun part! We're going to show Rasa Core the stories we wrote above, and train a model on these examples. In this case, the model is a neural network implemented in Keras which learns to predict which action to take next.<jupyter_code>from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.agent import Agent

agent = Agent('domain.yml', policies=[KerasPolicy()])
training_data = agent.load_data('stories.md')
agent.train(
        training_data,
        validation_split=0.0,
        epochs=400
)

agent.persist('models/dialogue')<jupyter_output><empty_output><jupyter_text>Starting up the botLets start up the bot!<jupyter_code>from rasa_core.agent import Agent
agent = Agent.load('models/dialogue')<jupyter_output><empty_output><jupyter_text>Talking to the bot (no NLU)In this step, we just trained the dialogue model - so basically the conversational flow. So the bot will only understand **structured** input and **no** natural language yet. Go try it out with typing  "/" + one of the intents from your domain before, e.g.:/greet/mood_affirm/mood_deny<jupyter_code>print("Your bot is ready to talk! Type your messages here or send 'stop'")
while True:
    a = input()
    if a == 'stop':
        break
    responses = agent.handle_message(a)
    for response in responses:
        print(response["text"])<jupyter_output><empty_output><jupyter_text>Part 2: Add natural language understandingSo far, our bot doesn't understand regular sentences with natural language yet.To do that, we're going to build a language understanding model with Rasa NLU.   Installing NLUWe have to install a couple of more things in this notebook here. **Run the cell below** - in the meantime, you can check out the rest of the code.<jupyter_code>!apt-get -qq install -y graphviz libgraphviz-dev pkg-config && pip install pygraphviz;
!pip install rasa_nlu[spacy];
!python -m spacy download en;<jupyter_output><empty_output><jupyter_text>Language UnderstandingLets create some training data here, grouping user messages by their `intent`s. The intent describes what the messages *mean*.<jupyter_code>nlu_md = """
## intent:greet
- hey
- hello
- hi
- hello there
- good morning
- good evening
- moin
- hey there
- let's go
- hey dude
- goodmorning
- goodevening
- good afternoon

## intent:goodbye
- cu
- good by
- cee you later
- good night
- good afternoon
- bye
- goodbye
- have a nice day
- see you around
- bye bye
- see you later

## intent:mood_affirm
- yes
- indeed
- of course
- that sounds good
- correct

## intent:mood_deny
- no
- never
- I don't think so
- don't like that
- no way
- not really

## intent:mood_great
- perfect
- very good
- great
- amazing
- feeling like a king
- wonderful
- I am feeling very good
- I am great
- I am amazing
- I am going to save the world
- super
- extremely good
- so so perfect
- so good
- so perfect

## intent:mood_unhappy
- my day was horrible
- I am sad
- I don't feel very well
- I am disappointed
- super sad
- I'm so sad
- sad
- very sad
- unhappy
- bad
- very bad
- awful
- terrible
- not so good
- not very good
- extremly sad
- so saad
- so sad
"""

%store nlu_md > nlu.md<jupyter_output><empty_output><jupyter_text>Train the Rasa NLU ModelWe're going to train a model to recognise these intents, so that when we send a message like "hello" to our bot, it will recognise this as a "greet" intent.<jupyter_code>from rasa_nlu.training_data import load_data
from rasa_nlu.config import RasaNLUModelConfig
from rasa_nlu.model import Trainer

training_data = load_data('nlu.md')
pipeline = [{"name": "nlp_spacy"}, 
            {"name": "tokenizer_spacy"}, 
            {"name": "intent_featurizer_spacy"},  
            {"name": "intent_classifier_sklearn"}]
trainer = Trainer(RasaNLUModelConfig({"pipeline": pipeline}))
interpreter = trainer.train(training_data)
model_directory = trainer.persist('./projects/default/')<jupyter_output><empty_output><jupyter_text>Starting up the bot (with NLU)Now that we've trained the dialogue **and** language understanding models and saved them, we can start up an `Agent` which will handle conversations for us.<jupyter_code>from rasa_core.agent import Agent
agent = Agent.load('models/dialogue', interpreter=model_directory)<jupyter_output><empty_output><jupyter_text>Talking to the Bot (with NLU)We can start talking to the bot in natural language.<jupyter_code>print("Your bot is ready to talk! Type your messages here or send 'stop'")
while True:
    a = input()
    if a == 'stop':
        break
    responses = agent.handle_message(a)
    for response in responses:
        print(response["text"])<jupyter_output><empty_output><jupyter_text>Pro Tip: Visualising the Training DataYou can visualise the stories to get a sense of how the conversations go. This is usually a good way to see if there are any stories which don't make sense<jupyter_code>from rasa_core.agent import Agent
from IPython.display import Image

agent = Agent.load('models/dialogue')
agent.visualize("stories.md", "story_graph.png", max_history=2)
Image(filename="story_graph.png")<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup