UNPKG

rivescript-brain

Version:

šŸ’¬ NN-Based intent detection and middleware support for RiveScript.

128 lines (99 loc) • 5.26 kB
# rivescript-brain <a href="https://imgur.com/KR3z73r"><img src="https://i.imgur.com/KR3z73r.png" title="What's new" height="175px"/></a> ## Overview `rivescript-brain` combines the power of [AIML](http://www.aiml.foundation/index.html)-based [RiveScript](https://www.rivescript.com/) with [Brain.js](https://brain.js.org/) to add neural-network based [intent](https://chatbotsmagazine.com/chatbot-vocabulary-10-chatbot-terms-you-need-to-know-3911b1ef31b4) detection ability - prevalent in modern chatbot design - to the former. The following documentation assumes basic knowledge of [RiveScript](https://www.rivescript.com/) and the concept of [classification](https://towardsdatascience.com/machine-learning-classifiers-a5cc4e1b0623). **ℹ Facing issues or have feedback? Click <a href="https://github.com/dhruv-tech/rivescript-brain/issues">here</a>.** ## Installation ```javascript npm i rivescript-brain ``` <b>šŸ“¢ Facing difficulties while installing the package?</b> 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:<br/> `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](https://brain.js.org/#/getting-started). ## Usage `rivescript-brain` offers all the same functionality as rivescript with added intent detection and middleware functionality. šŸ“š Language(s) Supported: English. ### Getting Started ```javascript 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 <a href="https://www.rivescript.com/docs/tutorial#labeled-sections">here</a>.<br/> #### Saving Image To save a JSON image of a trained classifier: ```javascript // Assuming 'bot' is already trained await bot.classifier.save('./myFilePath/image.json'); ``` #### Restoring Image To restore trained classifier from a saved JSON image: ```javascript 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. ```javascript bot.classifier.add('Hello, how do you do?', 'casual'); ``` Classifier training can be initiated as follows: ```javascript bot.classifier.train(); ``` #### Classifying ```javascript let result = bot.classifier.classify('Hello, how do you do?'); ``` ### Managing Discussions To prevent rivescript-brain's classifier from interfering in <a href = "https://www.rivescript.com/docs/tutorial#short-discussions">short discussions</a>, 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. <br/> Simply use: ```javascript 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](https://chatbotsmagazine.com/chatbot-vocabulary-10-chatbot-terms-you-need-to-know-3911b1ef31b4). #### Writing Middleware Adding a middleware function is simple: ```javascript 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> ```