/rivescript-brain

💬 NN-Based intent detection and middleware support for RiveScript

Primary LanguageJavaScriptMIT LicenseMIT

rivescript-brain

Overview

rivescript-brain combines the power of AIML-based RiveScript with Brain.js to add neural-network based intent detection ability - prevalent in modern chatbot design - to the former.

The following documentation assumes basic knowledge of RiveScript and the concept of classification.

Installation

npm i rivescript-brain

📢 Facing difficulties while installing the package? This is likely due to the brain.js dependency.

If you are on Windows, this can be resolved by installing windows-build-tools from npm with administrative privligies:
npm install --global --production windows-build-tools

If you are using another operating system or don't have admin privligies on Windows, please refer to this guide.

Usage

rivescript-brain offers all the same functionality as rivescript with added intent detection and middleware functionality.

📚 Language(s) Supported: English.

Getting Started

const rs = require('rivescript-brain');

const bot = new rs({utf8:true});

bot.loadDirectory("./dir/").then(async() => {
    bot.sortReplies();
    // Classifier Setup
    bot.classifier.add('Hello, how are you?', 'casual'); // i.e. ([sample utterance], [intent])
    bot.classifier.train();
    // Getting Reply
    console.log(await bot.reply('username','Hello'));

}).catch((e) => {
    console.trace("Could not load Rive files.", e);
});

The Classifier

In rivescript-brain, a feed-forward neural network is used to classify phrases.

Expected Structure of Rive Files

For the classifier to work as intended, it is expected that all conversations in the RiveScript files, are sorted by topic. These topics must be the same as the intents used to train the classifier. To learn more about topics in RiveScript, please click here.

Saving Image

To save a JSON image of a trained classifier:

// Assuming 'bot' is already trained
await bot.classifier.save('./myFilePath/image.json');

Restoring Image

To restore trained classifier from a saved JSON image:

await bot.classifier.restore('./myFilePath/image.json');

Training & Retraining

To train a classifier, data must be added first. Note that when retraining, previously added data is retained and does not need to be added again.

bot.classifier.add('Hello, how do you do?', 'casual');

Classifier training can be initiated as follows:

bot.classifier.train();

Classifying

let result = bot.classifier.classify('Hello, how do you do?');

Managing Discussions

To prevent rivescript-brain's classifier from interfering in short discussions, it is important that there start and end points are marked as folows:

+ Hello
- Hi, how are you? <set discussion=true>

+ *
% Hi, how are you?
- May I know your name please?

+ *
% May I know your name please?
- Thanks! How can I help you today? <set name=<star>> <set discussion=false>

The Reply Function

rivescript-brain modifies the stock rivescript reply function to incoperate classifcation and middleware functionality. There is no need to classify and set the convsersation topic seperately.
Simply use:

await bot.reply('username','Hello');

Middleware

Middleware allows for triggering a script based on the response recevied from the rivescript dialog engine. This is suitable for applications such as entity detection.

Writing Middleware

Adding a middleware function is simple:

bot.middleware.myFunction = (input, output) => {
    return new Promise((resolve) => {

        //Do Something...

        resolve(output);
    })
}

Here, input refers to the phrase entered by the user and output refers to the output generated by the RiveScript engine. All middleware functions must accept input and output as arguments and return a promise that ultimalely resolves to output. The output returned by the middleware is what the reply(input) function will return.

Calling Middleware

Calling middleware from a RiveScript file is very simple. Simply set the event variable to the name of the middleware function that you'd like to call:

+ tell me a fact
- Here you go: <set event=myFunction>