Using IBM's Watson Conversation API with Cisco Spark in Node.js

IBM's Watson has a number of APIs but the one I am dealing with in this post is the Conversation API which has some interesting features. Even though IBM are clearly still adding features to this fairly new API it is pretty easy to work with once you understand how context can be used. I am not going to go over the basics of the API which if you have never worked with Watson before there are a million You Tube videos on the basic's. Seems even a 13 year old can put a video together on how to use Watson. I did watch this video BTW and its a handy starting place for the basics but one thing that is missing from all the videos is how to use the context data to get information back and forth from your bot to Watson. The video's mention it and the Watson documentation talks about it but it is a little confusing to start with.

What is Context?

Context is a pretty commonly used phrase in natural language processing(NLP) systems, it allows you to pass information back and forth between your bot and the NLP processor, in our case Watson. It does this using a JSON format which can include basically any attribute you want to put into it. Watson does have its own tracking with the conversation_id but tracking of the user and the conversation the user is currently in is up to you. I like to think of context as a cookie that gets passed back and forth. If you are working with a chat app in the browser the browser can take care of the cookie for you. But in our case with Cisco Spark you have no such luck. You as the developer have to track context which is included with every response from Watson. Watson will update the context with information from your users and information Watson uses to track the conversation. Without the mechanism to track the conversation context Watson assumes every transaction is a new conversation and the conversation_id is reset to a new value.

Building a Cisco Spark Bot with Watson

I recently built a Spark bot that started out using an array to track context but I quickly moved to MongoDB database (I wanted some persistence on restarts of node) . Seeing as Watson's context is already formatted in JSON it is easy to slip this straight into Mongo. Below is an example of some of the attributes I add to the context parameter before I send my message to Watson. Email, roomId, user and orderComplete (it is a pizza bot) are all created and added before the first message is sent to Watson. The combination of email and roomId from a Spark point of view makes every conversation unique as well as giving me some handy attributes. I, as a person can have multiple conversations with Watson in either a one-on-one room or multiple participant room so having a way to track that is important.

{ _id: '',
  email: '@cisco.com',
  roomId: '
  user: 'Christopher Norman',
  orderComplete: 'true',
  conversation_id: 'af4487c4-5543-4bf8-ab9c-f4611b3498bd',
  system: 
   { dialog_stack: [ 'node_9_1475525825835' ],
     dialog_turn_counter: 9,
     dialog_request_counter: 9 },
  pizzaType: 'vegetarian',
  hackerNumber: 5 }

PizzaType and hackerNumber are appended during the conversation by Watson, which I will cover in just a bit. Obviously there are some other attributes Watson will add like what node it is up to in your conversation with node_9 as an example. This is its own tracking system and as long as you relay this back to Watson during your conversation it will keep your place in the dialog exchange. The exception to the this is a flat dialog structure where every node in the Watson dialog is a conversation entry point. In this case you don't care where the conversation is because the dialog has no depth. A FAQ bot where every question is the start and end and there is no follow up question as an example. I was going to say data collection as well but you can still make context updates even in this more simple dialog structure.

Below is a sample of what I used to build the context request using the Node.js Watson module. You may notice this is different than the example from the module. That is because the module example doesn't show you how to pass context through your conversation, this does. Watson's Node.js module is pretty useless without a way to collect and pass context. I worked this out by looking at this code, go look its worth the time. Stefania is an IBM dev evangelist, she has written some great examples over on Github that helped me a great deal. Just remember you have to save the returned context and retrieve what you have saved to send back a reply when your user responds to Watson. Watsons NLP engine will append entity information to context as you dictate but your still need to save it.

If you are interested in what I did to write my context to a MongoDB see my gist below. I am not going to cover it blow by blow but some might find it handy so I have included it for completeness. There is nothing to complicated in there as I replace the previous context Watson sends me rather than update the one in the database. There are quite few fields that require updating in every transaction so replacing context was just simpler than updating fields in the stored JSON record. If you prefer another database go for it this is just a quick solution I put it together in a couple of days. I didn't have weeks to explore the perfect database or why one option may be better than another or even put much thought into the data structure. I am just throwing JSON data chunks into the MongoDB and retrieving them later. I only mention this after reading this article while doing some research. It is not so much the article, which is interesting (although not sure I agree with), but some of the comments. The blogger should have mentioned to check your ego at the door before writing your comment. Is it just me or are people on the Internet snarky? lol.


So now we have covered how to build the context but what else can we do with it? Obviously I am going to use the classic pizza bot example, doesn't everyone. As you build out your dialog you can use Watson to pick out and update information in the context as well as create some more complex logic for dialog decision making. In the first dialog box even though you can not quite see it, I am checking the content of an attribute in my context using $orderComplete.contains('new'). This allows me to see if this person has ordered yet. If they had that same attribute "orderComplete" would have read true and I could have put them in a different thread.


In the first dialog box we ask the user what Pizza type they want. In the second box we are accepting their response, relaying it back and prompting for more info. But first we must record their response in our context. In our case it is the pizza selection using NLP we identify with entities(sorry I never covered this earlier. Now is the time to go read about intents and entities on IBM's Watson documentation site if you don't know WTF I am talking about).

Quick tip. If you need access to info in your context at any stage to respond to your user, using $ will get it for you. Below I added $user to grab the user name attribute from my context to add a personal touch to the interaction but this can be anything stored in the context.

Watson has a very interesting NLP engine that is actually pretty easy to work with once you get past the initial knowledge gap. Not to say there are not a load of other possible alternatives such as api.ai etc. or a node module that does NLP this is just one engine. If you have tried others feel free to comment about it. Always interested to check out new stuff especially if you have done NLP using a node module.

Thanks for reading.

VoIPNorm

Stop Bugging Me: My life Hack Continues

It has been nearly a month now since I first started my journey and I thought it was time for some reflection on my progress or lack there of (if you would like to skip all this and go down to my summary of what is working go for it). It is an interesting journey that has seen some ups and some downs to be sure but it is mostly positive. Just making a cold turkey change and ignoring all my distractions was a stupid thought I had. I gave up smoking that way but this is far more challenging. Its more akin to getting back into physically working out than giving up smoking.

The Mental Workout

I spent a number of years in the military as a much younger me where I was very physically active. I competed in triathlons, cycled competitively at least at a club level and worked out generally around 12-14 hours a week. I kept a workout journal so I know this is pretty accurate. This matched my military role where I was a electronics technician. Keeping fit is expected and encouraged. I managed to maintain that for a number of years after leaving the service but over time it dwindled. Now I probably do somewhere in the range of 3 hours a week, not including walking for golf etc. Could I go back to 12 hours a week, probably. But it's not something I could do rapidly. It would take time to build up to that level especially with any intensity. (Funny side note at the time I followed a training program put together by disgraced cyclist Lance Armstrong to build up to my 12 hours a week minus the performance enhancing substances. Ah memories.)

Why is this important? It is the same sort of buildup that I am realizing that I need for my mental work outs/focus/deep work (you choose the term). Short bursts of focus or long bursts of focus only provide marginal productivity if I slide back into my old habits which I find myself doing. Its a constant struggle of self-correction. I have been doing a lot of reading on various techniques to improve though and one which seems to be helping me is writing. Writing this blog, writing notes while reading material, writing down ideas etc. seems to be helping build focus. It is such a great creative outlet I really forgot what it can do for me.

Evolving Life Style Challenges

No screen Sundays as I have termed it have been a great success. I am rededicating myself to activities I had pretty much halted like gardening. I really look forward to Sundays and planning what I am going to do now. Whereas before it was nothing special. I think it has helped me feel more refreshed as well for the upcoming week.

Using Alex to wake me, remind me of upcoming meetings and also track my focus period lengths has proven to be very useful. Right now 35 minutes is optimal for focus periods before giving in to the distractions. Who knew 35 minutes could be so long. While I celebrate the 35 minutes which is a 15- minute improvement from my original 20, it's still pathetic. This needs to be much higher with back to back focus sessions. I am incrementally moving the needle 5 minutes at a time and its working. I recently read an interesting post by Cal Newport's Study hacks blog on taking deep breaks, something else I am looking at incorporating into how I extend my focus.

Having a variety of reading material on hand has been very helpful. I have a pretty good selection of reading material and am reading around 4 different books at the same time. I slip focus sessions in between meetings on different material as often as I can. I find studying a broad set of topics is keeping me more motivated than just focusing on one. This may be in part because of how I have previously managed my time. I do find activities such as building a new bot where I can stay focused for hours which is great but that is not going to expand my base knowledge to enjoy greater success.

My mind still wonders but I am finding it easier to pull it back on track. Seems like this has been my whole existence throughout my life I just didn't realize it. I have read a lot of different material on why things bore us. I have come down to two reasons for myself. One which comes from my reading is not challenging the creative side of our minds, obvious. Secondly is my own theory of, am I really bored by the material or task or has a distracted life style persuaded me to think anything that takes extended concentration as boring. Classifying it as boring makes it easy for me to justify not doing it and continue the skimming the web lifestyle. Truly challenging your intellect becomes a tiresome task which is easy to classify as boring. Producing something worthwhile also takes time and focus. It would be easy for me not to triple read my blog post for errors but no one wants to read something carelessly put together (I really had to make sure that there were no errors in that last sentence, it could have been super embarrasing (yes the spelling mistake in embarrassing was on purpose, it is a joke (please don't point out other errors in this post I am a work in progress ( fuck, I am lost in all my brackets)))) .

I found the biggest hurdle to overcoming the boredom challenge is finding where to start. So many of us feel overwhelmed by the volume of a topic that it's so hard to find where to start you just don't start at all. Funny thing is its not until you engross yourself in the environment that you find the right paths to follow. It is okay to not know where to start. Its the acting of beginning the endeavor that will lead you along to find the right path. BTW the person that said follow your passion is full of horse shit. No one ever got their dream job because of passion. Most people don't even know what their dream job is untill they are in it and are experts at said job.

Things I Need to Change

Planned focus time on the morning. Seems most people want to meet earlier rather than later. I just cannot escape that morning routine to clear my inbox either. I do not find this overwhelmingly tiresome but it is not a good habit either. Having filters on my email has certainly helped a great deal. Anything CC'd is in a separate folder and in my mind I have already categorized this as less important so therefor spend less time looking at it. I glance at it and move on typically rarely responding. Probably this is one of the best changes I have made. I spend less than 25% of my time responding to email and messages. This is down by at least 20%.

 I have found my early evening time works best (note - I have no kids) for free time but it is also the lowest energy I have during the day. Afternoon naps are certainly not out of the question but then I find I lose to much time getting functional again after the nap if its to long. So I haven't found the right solution yet.

Realizations and Improvement -

Thinking I could just become some mind ninja overnight - It is a constant struggle to do what is not easy.

Finding the right time to focus - Still working on this part. Is it early morning, late at night or a combo? I haven't found my rhythm yet.

Linking my success to lifestyle change - I have made all these changes when do I see the pay off.

Better planning - I started off doing a good job of this but it seems to have fallen off. I need to better plan my daily schedule. I have fallen back into some old habits of checking email etc. when I could be pursuing something more worthwhile.

Successes -

Writing - I have a bunch of note pads around I am continually scratching in but I have a special note pad for ideas and planning. I just feel good writing. Side effect, my spelling and grammar is improving immensely in general correspondence.

Alexa time management - Now I understand why having a personal assistant is so cool. Thank you Amazon.

Email and messaging management - Getting this under control is key.

Screen free Sunday - Turning off all screens for a day is something to enjoy.

Blogging - Although related to writing, its helping me formalize my plan and ensure continued commitment. Hopefully you will see me add some interesting topics in upcoming posts, that is not to say you will read them but interesting to me at least.

Thanks for reading about my life hack.

VoIPNorm

Working with the Cisco Spark Flint Framework .hears Method

If you don’t like looking at code then you probably wont like this post much. There is a bit of a art of working with the .hears method in the Flint framework. Nick Marus has done a fantastic job of documenting its many uses but I thought it might be helpful to give some coded examples using JavaScript. In saying that of course with code there are a million ways to do this. I am not saying I have done this the right way or wrong way its just the way I worked out how to do it. Feel free to send me your own examples. I am always looking for more ways to do things.

Hears Function Filtering Process


 Flint processes each of the hears function calls sequentially, testing the received web hook messages against each of your .hears functions. So there are a few ways to handle processing the incoming web hooks. I like to break out my hears functions into individual pieces of code. I have heard of others and tried my self using the switch command to derive the different commands being feed to the bot to lower the amount of code. Personally I like to see each command broken out for processing. I also like to mix my bots with natural language processing(NLP) and slash commands. While users may like to use NLP to ask questions and make requests, as an admin I like the ease of a slash command to perform tasks like broadcasting to all the rooms the bot is in.

Bot Welcome Message


 This is a great way to introduce your users to your bot. This is not using the hears function but actually the spawn event I thought it was worth a mention.  As the room is spawned and your bot enters this provides a welcome message to your users or a way they can get help for your bot. I have tried more extensive bot introductions but users complained it was to much. Just a quick hello here is how you get help is enough. Seeing as your bot may be in one on one or group rooms you may want to address each a little different. Remember bot accounts in a room only receive a web hook message if @mentioned.


Slash Commands


This is the bread and butter for a bot. Slash commands are actually pretty easy to handle. Nick has already taken care of removing the @mention when a /text is seen by the bot. This makes this type of command pretty simple to take care of. If the command itself is the only thing being processed you are ready to provide your bot logic and return your response using bot.say function. If you actually have text following the command you may want to strip the command out before more processing. The broadcast command example further along in the post provides an example of removing the base slash command. The example below also introduces the use of the trigger object to extract information from the incoming message. In this case the trigger.roomId. This is maybe something that your users are not to interested in but its a great way to quick grab your roomId when doing some development and testing.


Simple Reply


This example is basically the "hello world' for a bot. But it does show another important attribute of the trigger object. personDisplayName and personEmail are two handy attributes for dealing with bot hear function requests. We will take a look at the use of the personEmail attribute a little further on.


Broadcast and Restricted Commands


 This is a really handy bot command for event bots. Now you may be wondering what I am talking about. I have been to a number of conferences recently where bots have been used to provide information and encourage attendees to get involved with whats going on. I personally used a very simple bot at the TechCrunch hackathon to work with attendees and let them know when pizza was served. The thing with this command is you don't want everyone to have access to it. Last thing you need is some prankster letting loose with something obscene message to every room the bot is in. In my case I am using email to restrict the use of this command but you could take this further and include roomId as well. Another thing you can also use this method for is to restrict down who can interact with your bot to a domain. So if I use Cisco as an example I can build a bot just for internal use and say anyone with a cisco.com email address is good to go and everyone else gets a unauthorized access message.


NLP Processing


The NLP hears function I like to be the last hears function as a catch all after all the slash commands and everything else has been tested. The trick to this function though is that if you don't have a method to remove previously processed hears functions it will process them all over again.  I like to use the match function to test if commands had previously been used. Although this leads to having to edit a hears command function and this catchall function every time you add a new slash command I am sure a smart developer can work out how to fix that. This is just the way I worked out how to get around it. The whole idea of this function is to be able to send the request to a NLP engine for further processing and then use the response from Watson or api.ai etc. to send back your response to your user. Now if you do use the NLP response or do something else with the response from an NLP engine is totally up to you but this is the best way I found to capture the needed info from the user.


Posting Markdown and Files


Not really a hears function but still important is you want to make use of markdown or posting files using the Flint Framework using the bot.say function. Nick has changed this method a little between versions added the markdown JSON object. Pretty easy to understand once you see the example below.

Please comment if you have found more inventive ways to use this function.
VoIPNorm