UNPKG

node-red-contrib-chatbot

Version:

REDBot a Chat bot for a full featured chat bot for Telegram, Facebook Messenger and Slack. Almost no coding skills required

72 lines (68 loc) 5.71 kB
<script type="text/javascript"> $.RedBot.registerType('chatbot-nlpjs-train', { category: $.RedBot.config.name, color: '#FFCC66', defaults: { name: { value: '', validate: $.RedBot.validate.nlpModel }, debug: { value: false } }, inputs: 1, outputs: 1, paletteLabel: 'NLP.js Train', icon: 'chatbot-listen-lexicon.png', label: function() { return 'NLP.js Train' + (this.name != null && this.name !== '' ? ' (' + this.name + ')' : ' (default)'); } }); </script> <script type="text/x-red" data-template-name="chatbot-nlpjs-train"> <div class="form-row"> <label for="node-input-name"><i class="icon-tag"></i> Model Name</label> <input type="text" id="node-input-name" placeholder="Model name"> <span class="redbot-form-hint"> Leave blank if you don't need to use more than one NLP model in the chatbot. Only use letters and numbers, no spaces. </span> </div> <div class="form-row"> <label for="node-input-debug">Debug</label> <input type="checkbox" value="true" id="node-input-debug"> <span class="redbot-form-hint"> Show debug information while training the model </span> </div> </script> <script type="text/x-red" data-help-name="chatbot-nlpjs-train"><p>The <code>NLP.js nodes</code> uses <a href="https://github.com/axa-group/nlp.js">NLP.js</a> for Natural Language Processing.</p> <p>In order to work it requires a properly trained model using <code>NLP.js Train</code>, <a href="https://www.notion.so/c91af0a6fde74dc5b575aa7230565ba8">NLPjs Intent node</a> and <a href="https://www.notion.so/f73d1e0200804467b8f03e077584fb16">NLPjs Entity</a> . An <em>intent</em> is a string representation of the abstract meaning of the user sentence, it requires an arbitrary name (that will be likely used in a <a href="https://www.notion.so/4113636f565d4ff4af08bc61a644206b">Rules node</a> to control the chatbot flow) and one or more utterances (a set of all different way of expressing the same actions).</p> <p>An <em>entity</em> is the variable part of the utterance, it can be detected and stored in a context variable and used later in the flow. Some entities are predefined (like numbers, dates, etc) while other can be defined, for example a chatbot that is able to understand a sentence like <code>I want to buy 3 apples</code> should have an intent with the utterance <code>I want to by %number% %fruit%</code>. In this example the entity <em>number</em> is built-it, while the entity <em>fruit</em> should be defined with the <a href="https://www.notion.so/f73d1e0200804467b8f03e077584fb16">NLPjs Entity</a> and should include all kind of fruits with optional aliases (apple, orange, grape, etc).</p> <p>All this intents and entities nodes must be chained to a <code>NLP.js Train</code> node which creates a precompiled model used to detect the sentences, it’s generally stored in a <em>JSON</em> file using the <a href="https://www.notion.so/4f99d9bfe1ee4683acbee70c02eb24d2">NLPjs Save</a> and loaded when the flow is started (the train process could be long and can be avoided if the intents and entities are not changed).</p> <p>The typical use in a flow is</p> <p><img src="https://raw.githubusercontent.com/guidone/node-red-contrib-chatbot/master/docs/images/nlp-train.png" alt="Train model"></p> <p>The scenario is a chatbot able to understand sentences like <em>“switch on the lights in the kitchen”</em>, <em>“turn on lights in dining room”</em>. The first <code>Inject node</code> is used to train the NLP.js node and store locally the trained model every time an intent or entity is changed. The second <code>Inject node</code> runs automatically at startup and loads the trained model.</p> <p>The <a href="https://www.notion.so/c91af0a6fde74dc5b575aa7230565ba8">NLPjs Intent node</a> looks like</p> <p><img src="https://raw.githubusercontent.com/guidone/node-red-contrib-chatbot/master/docs/images/nlp-intent.png" alt=""></p> <p>Where the <code>%room%</code> placeholder is an entity defined in the <code>NLP.js Entity node</code>, if there’s a match the <a href="https://www.notion.so/bbc3deb2d39a4b338fc6515eee337cd4">NLPjs Process</a> will return the intent (in this case <code>switch.on</code>) and the extract variables for each placeholder (like <code>%room%</code>).</p> <p><img src="https://raw.githubusercontent.com/guidone/node-red-contrib-chatbot/master/docs/images/nlp-entity.png" alt=""></p> <p>When the model is trained and loaded into the flow, <code>NLP.js Process</code> can be used to analyse the sentences from the users</p> <p><img src="https://raw.githubusercontent.com/guidone/node-red-contrib-chatbot/master/docs/images/nlp-process.png" alt="Train model"></p> <p>If, for example, the user writes <em>“Turn on lights in the kitchen”</em>, the output of the <a href="https://www.notion.so/bbc3deb2d39a4b338fc6515eee337cd4">NLPjs Process</a> will be</p> <pre><code class="language-javascript">{ payload: { type: &#39;intent, intent: &#39;switch.on&#39;, isFallback: false, score: 0.7, language: &#39;en&#39;, variables: { room: &#39;kitchen&#39; } } } </code></pre> <p>The output of the <a href="https://www.notion.so/bbc3deb2d39a4b338fc6515eee337cd4">NLPjs Process</a> can be chained directly to a <a href="https://www.notion.so/24a646bff58b4edfb249f1d27384230f">Context node</a> to store the extracted variables in the chat context.</p> <p>If not specified, the name of the module will be set to <em>“default”</em>, this is useful only if the chatbots needs more than one models in the same flow.</p> </script>