Reader small image

You're reading from  Conversational AI with Rasa

Product typeBook
Published inOct 2021
PublisherPackt
ISBN-139781801077057
Edition1st Edition
Tools
Right arrow
Authors (2):
Xiaoquan Kong
Xiaoquan Kong
author image
Xiaoquan Kong

Xiaoquan is a machine learning expert specializing in NLP applications. He has extensive experience in leading teams to build NLP platforms in several Fortune Global 500 companies. He is a Google developer expert in Machine Learning and has been actively involved in contributions to TensorFlow for many years. He also has actively contributed to the development of the Rasa framework since the early stage and became a Rasa Superhero in 2018. He manages the Rasa Chinese community and has also participated in the Chinese localization of TensorFlow documents as a technical reviewer.
Read more about Xiaoquan Kong

Guan Wang
Guan Wang
author image
Guan Wang

Guan is currently working on Al applications and research for the insurance industry. Prior to that, he was a machine learning researcher at several industry Al labs. He was raised and educated in Mainland China, lived in Hong Kong for 10 years before relocating to Singapore in 2020. Guan holds BSc degrees in Physics and Computer Science from Peking University, and an MPhil degree in Physics from HKUST. Guan is an active tech blogger and community contributor to open source projects including Rasa, receiving more than10,000 stars for his own projects on Github.
Read more about Guan Wang

View More author details
Right arrow

Chapter 6: Knowledge Base Actions to Handle Question Answering

In the previous chapter, we introduced, in detail, the process of using ResponseSelector to handle chitchat and FAQs. This chapter will teach you how to deal with more complex question answering problems: referential resolution and dynamic query. Referential resolution refers to correctly parsing the pronouns (such as it, the first, and the last) into corresponding concrete objects. The dynamic query problem means that the query result might change rapidly. It might be different each time, so it is impossible to use fixed reply content, as we did in the previous chapter.

In this chapter, you will learn how to create a knowledge base that can be used for answering questions. Additionally, you will learn to customize knowledge base actions, learn how referential resolution (mapping a mention to an object) works, and how to create a knowledge base. Finally, you will develop a practical understanding of these concepts with...

Technical requirements

You can find all the files for this chapter inside the ch06 directory of the GitHub repository at https://github.com/PacktPublishing/Conversational-AI-with-RASA.

A knowledge base action is an experimental feature

At the time of writing (Rasa version 2.5), the knowledge base action that we are discussing here is still an experimental feature. The functionality might be changed, or (although unlikely, it is still possible) it can be removed in the future.

Why do we need knowledge base actions?

One of the common challenges you face when building a chatbot is that users might not refer to things using names but with pronouns such as "it," "this," and "that" or "the previous one" and "the second one." Here is an example:

User:
    Do you have any recommended songs for me?
Bot: 
    I find the following songs:
    1: Billie Jean
    2: The Shape of My Heart
    3: Like a Rolling Stone
User:
    Which album is the first song?
Bot:
    "Billie Jean" is from Michael Jackson's album "Thriller".

In the preceding example, User refers to Billie Jean as the first song. This pattern is common in spoken language, especially when the name of the item is uncommon (for example, IKEA's "FRAKATA Carrier Bag") or the name...

How do you use knowledge base actions?

To tackle the challenges that we introduced in the previous section, Rasa can be integrated with a knowledge base via a knowledge base action. A knowledge base action is a special action that has been developed to handle referential resolution and queries on objects and their properties.

In general, to use knowledge base actions, you need to do the following:

  • Create a knowledge base from where the bot can retrieve information that will be used to answer the questions that have been asked.
  • Create a knowledge base action using Rasa SDK, which will query the knowledge base according to the user's inputs and reply with relevant answers.
  • Define some Natural Language Understanding (NLU) data so that users can trigger the knowledge base action via the inputs.
  • Modify your knowledge base actions to make the responses more human-like.

Let's start by defining a knowledge base.

Creating a knowledge base

A knowledge...

How do you customize knowledge base actions?

The default knowledge base action has several disadvantages. First, the message returned to the user is not very user-friendly, the reply format is fixed, and it does not have any personality. Second, the built-in memory-based knowledge base is limited by the size of the memory and cannot support a very large-scale knowledge base. Additionally, there is no way to modify the content of the knowledge base externally in real time. In the following sections, we will solve these problems one by one.

Modifying ActionQueryKnowledgeBase to customize the behavior

Here, we introduce how to customize the output message from ActionQueryKnowledgeBase. This is especially important for Rasa developers who use multiple languages, as the default return message is always English.

Custom ways to express the object list

When a user requests the bot system to return the list of objects, utter_objects() will be called. The function of utter_objects...

Learning by doing – building a knowledge-based music query chatbot

We have designed this section to enhance your understanding of knowledge base actions. We will create a QA bot based on a homemade toy-level knowledge base. This QA robot will answer questions regarding music: songs, albums, and singers.

What are the features of our bot?

In this section, we will introduce the characteristics of our robot. Its features can be divided into two types: one is the unconstrained query, and the other is the constrained query (or with filter conditions). The former does not make any constraints or filters on the query object, such as list all singers. The latter has clear constraints, such as List all-male singers born in 1980, where born in 1980 and male are both constraints.

To begin, let's discuss the unconstrained query.

Querying the list of objects and asking for the attributes of the specified object

Example #1: Search for songs and ask for the album attributes...

Summary

In this chapter, we introduced how to use knowledge base actions to handle question answering. First, you learned how to create an in-memory knowledge base for retrieving information that can be used to answer questions. Second, you were introduced to the working principles of knowledge base actions and learned how to configure NLU and dialogue management systems so that user requests can trigger knowledge base actions. Third, you learned how to modify knowledge base actions to customize them to your business and how to create a knowledge base by using your own code. Finally, we also built a knowledge-based music query chatbot, step by step, to help you understand those concepts better.

In the next chapter, we will discuss entity roles and groups for complex Named Entity Recognition (NER).

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Conversational AI with Rasa
Published in: Oct 2021Publisher: PacktISBN-13: 9781801077057
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Xiaoquan Kong

Xiaoquan is a machine learning expert specializing in NLP applications. He has extensive experience in leading teams to build NLP platforms in several Fortune Global 500 companies. He is a Google developer expert in Machine Learning and has been actively involved in contributions to TensorFlow for many years. He also has actively contributed to the development of the Rasa framework since the early stage and became a Rasa Superhero in 2018. He manages the Rasa Chinese community and has also participated in the Chinese localization of TensorFlow documents as a technical reviewer.
Read more about Xiaoquan Kong

author image
Guan Wang

Guan is currently working on Al applications and research for the insurance industry. Prior to that, he was a machine learning researcher at several industry Al labs. He was raised and educated in Mainland China, lived in Hong Kong for 10 years before relocating to Singapore in 2020. Guan holds BSc degrees in Physics and Computer Science from Peking University, and an MPhil degree in Physics from HKUST. Guan is an active tech blogger and community contributor to open source projects including Rasa, receiving more than10,000 stars for his own projects on Github.
Read more about Guan Wang