GithubHelp home page GithubHelp logo

alyssaong1 / botframework-smalltalk Goto Github PK

View Code? Open in Web Editor NEW
66.0 9.0 37.0 61 KB

Small talk module for the Microsoft Bot Framework, ported over from Dialogflow/API.AI's small talk module. Handle basic greetings and common phrases like "hello" and "how are you".

License: ISC License

JavaScript 100.00%

botframework-smalltalk's Introduction

Small Talk Module for the Microsoft Bot Framework

Update: Now supports the npm Natural package

Ever had the problem of needing to handle countless types of conversational chatter (e.g. "hello", "good afternoon", "goodbye", "you're terrible", "what's up")? This is an easy to use, plug-n-play small talk module, using QnA maker or the open source Natural npm package. This module is also combinable with LUIS.

Note: This is a port over from API.AI/Dialogflow's small talk module. It uses the same intents and scripted replies.

How it works: An excel containing the question and intent matches is uploaded to QnA maker. In the bot, the result from QnA maker is mapped to a list of possible responses (see smalltalk.js in the lib folder), which are selected at random.

Demo

demo

Installation and Usage

First of all, clone this repo by running the following in the command line:

git clone https://github.com/alyssaong1/botframework-smalltalk.git

Now you have 2 options, either using QnA maker or the Natural package to power your bot's NLP capabilities.

Option 1: Using QnA maker

Configure QnA Maker

Create a QnA maker service for free here.

Click on "Replace knowledge base". Upload the smalltalkkb.tsv file. Publish it as a web service.

qnasetup

Populate the environment variables

Go into the .env file and replace with knowledge base id and subscription key which you can find in the settings page of the QnA maker portal.

envsetup

Running the sample

Build using npm install then run it using npm start. You can now test it in the Bot Framework emulator.

Example: calling the smalltalk module in your bot code

var QnAClient = require('../lib/client');
...
var qnaClient = new QnAClient({
    knowledgeBaseId: process.env.KB_ID,
    subscriptionKey: process.env.QNA_KEY,
    scoreThreshold: 0.5 // OPTIONAL: Default value is 0.2
});
...
// Post user's question to QnA smalltalk kb
qnaClient.post({ question: session.message.text }, function (err, res) {
    if (err) {
        console.error('Error from callback:', err);
        session.send('Oops - something went wrong.');
        return;
    }

    if (res) {
        // Send reply from QnA back to user
        session.send(res);
    } else {
        // Confidence in top result is not high enough - discard result
        session.send('Hmm, I didn\'t quite understand you there. Care to rephrase?')
    }
});

The scoreThreshold field is modifiable. It is the confidence level required for the reply to be sent back to the user. A high value means that QnA maker has to be very sure of what the user has said before sending the reply back.

Option 2: Using the npm Natural package

This is an open source option which you may like to explore. It's got a bit more setup than Option 1, especially if you would like to serve this model on a separate server to your bot.

Documentation on Natural here.

Train your classification model

First of all, restore our npm packages using the following command:

npm install

Go into natural/train-model.js. This contains the code to train your classifier using the Node Natural package. To run the code, do the following:

node natural/train-model.js

You should see the following output from running the command:

Training complete for Small talk classifier.
Small talk classifier saved as smalltalk-classifier.json.

You should see a new file under the natural folder called smalltalk-classifier.json. This contains your trained classifier model, which you will now load into the bot code and use.

Use your smalltalk classifier in your bot code

The bot/index-natural.js file contains the bot code that uses the Natural classifier you just trained (instead of QnA maker). To run this bot, go into the app.js file and modify the following line when initializing the bot:

var bot = require('./bot/index-natural.js');

Now run the bot using npm start. You can now test it in the Bot Framework emulator.\

Example: calling the smalltalk Natural classifier in your bot code

const natural = require('natural');
const smallTalkReplies = require('../lib/smalltalk');

let st_classifier = null;
natural.BayesClassifier.load('natural/smalltalk-classifier.json', null, function(err, classifier) {
   st_classifier = classifier;
});

...

bot.dialog('/', [
    (session, args) => {
        // Post user's question to classifier
        var intent = st_classifier.classify(session.message.text);
        // Obtain the response based on intent
        var botreplylist = smallTalkReplies[intent];
        var botreply = botreplylist[Math.floor(Math.random() * botreplylist.length)];
        // Send response back to user
        session.send(botreply);
    }
]);

More details on configuring the Natural classifier here.

Extensions

Integrating with your existing LUIS model

We are going to create an intent called "smalltalk" and upload all the smalltalk utterances into this intent through the LUIS API.

Go to the LUIS portal, create an intent called "smalltalk".

You will need to update the .env file in the luis folder. Obtain your LUIS app id, subscription key and version from the LUIS portal. Make sure to use the starter key for the LUIS subscription key, because the LUIS API requires this specific key.

Then cd into the luis folder, and run node uploadtoluis. Wait for all the utterances to be uploaded to LUIS (you'll see the batch request success message about ~10 times). You should see on your intents dashboard that there are 1400+ utterances in the smalltalk intent.

smalltalk-luis

Retrain and publish your LUIS model - any smalltalk from the user will now be routed to the smalltalk intent, which you can pass to the QnA maker smalltalk module in your code.

Adding a new type of smalltalk

Let's say you wanted your smalltalk module to handle when the user wants a joke. Add the following to the smalltalkkb.tsv file:

addsmalltalk

Replace your knowledge base in QnA maker with the updated one and publish.

Then, add the following to the smalltalk.js file.

[
    ...
    "smalltalk.agent.tell_joke": [
        "Standing in the park, I was wondering why a Frisbee gets larger the closer it gets. Then it hit me.",
        "I needed a password eight characters long so I picked Snow White and the Seven Dwarfs.",
        "The early bird gets the worm but the late worm gets to live."
    ]
]

Improving the smalltalk module

You may find that the current smalltalk module is still not accurate enough. Please note that even the smalltalk module on API.AI is not totally accurate - it will still require additional training and refinement on your part.

You can continue to train the smalltalk module to be more accurate or handle more types of smalltalk by adding or removing utterances in the smalltalkkb.tsv file (then reupload to QnA maker), or directly train it using the QnA maker portal.

Modifying the responses

To modify the responses by the smalltalk module, you can go into smalltalk.js to add/remove the responses for the various types of smalltalk.

Contributing

Feel free to contribute to this project! Use the following guidelines:

  1. Fork the repo on GitHub
  2. Clone the project to your own machine
  3. Commit changes to your own branch
  4. Push your work back up to your fork
  5. Submit a Pull Request on Github so that I can review your change

Additional Links

TO-DOs

  • Publish npm package for this

botframework-smalltalk's People

Contributors

alyssaong1 avatar zanechua avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

botframework-smalltalk's Issues

Leading question mark in smalltalk.dialog.sorry KB answer

Hi,

I'm playing with implementing smalltalk in Bot Framework and this has been super helpful. So thank you. :) I've just translated it to Czech for local purposes. However, one thing got me confused:

In the knowledge base, you have a leading question mark in all the answers for the sorry intent,
so it says ?smalltalk.dialog.sorry
(https://github.com/alyssaong1/botframework-smalltalk/blob/master/smalltalkkb.tsv#L943)

Was there a specific reason for this? It causes a mismatch, so it doesn't provide the correct answer if the user happens to trigger this.

Thanks

Missing License

The package.json file cites the ISC license but there is no license assigned for the repository. Should we assume ISC license for the whole repository?

Why QnAMakerRecogniser creates intent on Luis?

@luhan2017 As I can see in one of my bot project where I have used both LUIS and QnAMaker. When I built the project and run it. I have seen in Luis app, there is an intent like DeferToRecognizer_QnA_VirtualTravelAssistant.

But When I created another project, where I have added one more intent (except Greeting and Unknown) i.e. QnA Intent Recognized, then I observed that there is no LUIS created and no intent found.

Please help me to understand this.

Bot State API is deprecated

My local version fails with this error:

Server listening on port 3979
ChatConnector: message received.
ChatConnector: message received.
ChatConnector: message received.
The Bot State API is deprecated.  Please refer to https://aka.ms/I6swrh for details on how to replace with your own storage.
UniversalBot("*") routing "hi" from "emulator"
Session.beginDialog(/)
/ - waterfall() step 1 of 1
/ - Session.sendBatch() sending 0 message(s)
The Bot State API is deprecated.  Please refer to https://aka.ms/I6swrh for details on how to replace with your own storage.
/ - Session.send()
/ - Session.sendBatch() sending 1 message(s)
The Bot State API is deprecated.  Please refer to https://aka.ms/I6swrh for details on how to replace with your own storage.

Upload to LUIS is failing

When I run node uploadtoluis.js I get an error:

Unhandled rejection ReferenceError: response is not defined
    at /home/chris/botframework-smalltalk/luis/uploadtoluis.js:64:50
    at tryCatcher (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/promise.js:689:18)
    at Async._drainQueue (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/home/chris/botframework-smalltalk/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5)

After removing response.statusMessage from line 64, I get the Web request failed:

I have filled in all the .env values. I double checked the URL you are building and it seems okay according to the API...

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.