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

131 lines (129 loc) 6.54 kB
<script type="text/javascript"> $.RedBot.registerType('chatbot-nlpjs', { category: $.RedBot.config.name, color: '#FFCC66', defaults: { name: { value: '' }, language: { value: '', required: false }, debug: { value: false }, scoreThreshold: { value: 50 } }, inputs: 1, outputs: 1, paletteLabel: 'NLP.js Process', icon: 'chatbot-listen-lexicon.png', label: function() { return this.name || 'NLP.js Process' + (this.name != null && this.name !== '' ? ' (' + this.name + ')' : ' (default)'); } }); </script> <script type="text/x-red" data-template-name="chatbot-nlpjs"> <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"> </div> <div class="form-row"> <label for="node-input-c">Score threshold</label> <input type="text" id="node-input-scoreThreshold" placeholder="Score threshold" style="width:150px;text-align:right"> <b>%</b> <span class="redbot-form-hint"> Only match intents with a score above the threshold. Lower the threshold if the node doesn't match the desidered utterances, raise it if the conflicts with other models or nodes.<br/> Enable the debug to learn more about matched intents and scores. </span> </div> <div class="form-row form-row-language"> <label for="node-input-language">Language</label> <select id="node-input-language" placeholder="Language to match"> <option value="">Use chat context or guess</option> <option value="ar">Arabic</option> <option value="bn">Bengali</option> <option value="ca">Catalan</option> <option value="cs">Czech</option> <option value="da">Danish</option> <option value="de">German</option> <option value="el">Greek</option> <option value="en">English</option> <option value="es">Spanish</option> <option value="eu">Basque</option> <option value="fa">Persian</option> <option value="fi">Finnish</option> <option value="fr">French</option> <option value="ga">Irish</option> <option value="gl">Galician</option> <option value="hi">Hindi</option> <option value="hu">Hungarian</option> <option value="hy">Armenian</option> <option value="it">Italian</option> <option value="ja">Japanese</option> <option value="nl">Dutch</option> <option value="no">Norwegian</option> <option value="pt">Portuguese</option> <option value="ro">Romanian</option> <option value="ru">Russian</option> <option value="sl">Slovenian</option> <option value="sv">Swedish</option> <option value="ta">Tamil</option> <option value="th">Thai</option> <option value="tl">Tagalog</option> <option value="tr">Turkish</option> <option value="uk">Ukrainian</option> <option value="zh">Chinese</option> </select> </div> <div class="form-row"> <label for="node-input-debug" style="width:auto;">Debug</label> <input type="checkbox" value="true" id="node-input-debug" style="width:auto;"> <span class="redbot-form-hint"> Show debug information </span> </div> </script> <script type="text/x-red" data-help-name="chatbot-nlpjs"><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 chatbot 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;, score: 0.7, isFallback: false, language: &#39;en&#39;, variables: { room: &#39;kitchen&#39; } } } </code></pre> <p>The output of the <code>NLP.js Process node</code> 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>Enable the <em>debug</em> option to have an insight of what the NLP model is doing trying to classify the user’s intent, this what is visible in the system console</p> <pre><code class="language-plain">[NLP] Processing model lights Input: &quot;Turn on lights in the kitchen&quot; (en) Score threshold: 90 % Language guessed: false Intent: switch.on Domain: default Score: 95.0 % Sentiment: positive score 50.0 % </code></pre> <p>Which means that the sentence <em>“Turn on lights in the kitchen”</em> was correctly classified in the intent <em>switch.on</em> using the language <em>en</em> with a probability of <em>95%</em>. Use the <em>threshold</em> option in the Process node to adjust the sensitivity of <em>NLP.js</em>, if it’s too low it can match sentences not relevant, is it’s too high it could miss sentences that differ a little from the ones entered in the <a href="https://www.notion.so/c91af0a6fde74dc5b575aa7230565ba8">NLPjs Intent node</a> .</p> <p>The <a href="https://www.notion.so/bbc3deb2d39a4b338fc6515eee337cd4">NLPjs Process</a> can also be used standalone to process any kind of string, just put the text in <code>msg.payload</code>. Unless specified in the configuration, the node will try to guess the language from the same text.</p> <h2 id="debugging">Debugging</h2> <p>What to do if it doesn’t match the user sentence? If the <em>NLP.js</em> is not detecting the right intent for some user input (in the example above, suppose the user entered a slightly different utterance <em>“Turn on the lights in my kitchen”</em>):</p> <ol> <li><p>first check that the context language is the same of the trained model language (trying to classify the above sentence in the <em>“it”</em> language will return nothing) </p> </li> <li><p>update the list of utterances in the <a href="https://www.notion.so/c91af0a6fde74dc5b575aa7230565ba8">NLPjs Intent node</a> , more training means more accurate classification, this is the recommended approach </p> </li> <li><p>or, check the system console and check the score if the classification, if the detected intent is right but the score is too low, lower the <em>threshold</em> value in the <code>NLP Process node</code> configuration</p> </li> </ol> </script>