Robust NLU with Rasa

Building Chatbots in Python

Alan Nichol

Co-founder and CTO, Rasa

Rasa NLU

  • Library for intent recognition & entity extraction
  • Based on spaCy, scikit-learn, & other libraries
  • Built in support for chatbot specific tasks
Building Chatbots in Python

Rasa data format

from rasa_nlu.converters import load_data

training_data = load_data("./training_data.json")
import json print(json.dumps(data.training_examples[22], indent=2))
{ "text": "i'm looking for a place in the north of town",
  "intent": "restaurant_search",
  "entities": [
    { "start": 31,
      "end": 36,
      "value": "north",
      "entity": "location" }
  ]
}
Building Chatbots in Python

Interpreters

message = "I want to book a flight to London"

interpreter.parse(message))
{ "intent": {
    "name": "flight_search",
    "confidence": 0.9
  },
  "entities": [
    { "entity": "location",
      "value": "London",
      "start": 27,
      "end": 33
    }
  ]
}
Building Chatbots in Python

Rasa usage

# Creating a model
from rasa_nlu.config import RasaNLUConfig
from rasa_nlu.model import Trainer

config = RasaNLUConfig( cmdline_args={"pipeline": "spacy_sklearn"})
trainer = Trainer(config)
interpreter = trainer.train(training_data)
Building Chatbots in Python

Rasa pipelines

spacy_sklearn_pipeline = [
  "nlp_spacy",
  "ner_crf",
  "ner_synonyms", 
  "intent_featurizer_spacy",
  "intent_classifier_sklearn"  ]
# These two statements are identical:
RasaNLUConfig(cmdline_args={"pipeline": spacy_sklearn_pipeline})
<rasa_nlu.config.RasaNLUConfig at 0x10f60aa90>
RasaNLUConfig(cmdline_args={"pipeline": "spacy_sklearn"})
<rasa_nlu.config.RasaNLUConfig at 0x10f60aa20>
Building Chatbots in Python

Conditional random fields

  • Machine Learning model, popular for named entity recognition
    • can perform well even with small training data
Building Chatbots in Python

Handling typos

pipeline = [
    "nlp_spacy",
    "intent_featurizer_spacy",
    "intent_featurizer_ngrams",
    "intent_classifier_sklearn"
]
Building Chatbots in Python

Let's practice!

Building Chatbots in Python

Preparing Video For Download...