Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Need of Context in a Chatbot Conversation

DZone's Guide to

Need of Context in a Chatbot Conversation

Developers must keep in mind while building a chatbot that context is important to maintain the state of a conversation.

Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

A conversation (or chat) is a chain of statements exchanged between two or more individuals. Mostly, conversations happen on a particular topic or in a situation. Whatever the topic or situation is, context is very important to maintain the state of a conversation.

Conversation Chatbot

In the world of cognitive computing, computers are conversing with humans as ChatBots, or in short, bots. As Alan Turing famously quoted, "A computer would deserve to be called intelligent if it could deceive a human into believing that it was human."

So, even computers need the so-called context to keep the conversation flowing and to deceive us as humans.

Why am I so contextual today? Looks like chatting with a WatBot for a week took a toll on me, or reading the below line from the Watson Conversation documentation had its impact:

"State information for your conversation is maintained using the context. The context is a JSON object that is passed back and forth between your application and the Conversation service. It is the responsibility of your application to maintain the context from one turn of the conversation to the next."

Technically, a context includes a unique identifier for each conversation with a user, as well as a counter that is incremented with each turn of the conversation. If we don’t preserve the context,  each round of input appears to be the start of a new conversation. We can fix that by saving the context and sending it back to the conversation service each time. 

Code To Achieve Context Persistence

In Javascript:

// maintains state. var prompt = require('prompt-sync')(); var ConversationV1 = require('watson-developer-cloud/conversation/v1'); // Set up Conversation service. var conversation = new ConversationV1({   username: 'USERNAME', // replace with username from service key   password: 'PASSWORD', // replace with password from service key   path: { workspace_id: 'WORKSPACE_ID' }, // replace with workspace ID   version_date: '2016-07-11' }); // Start conversation with empty message. conversation.message({}, processResponse); // Process the conversation response. function processResponse(err, response) {   if (err) {     console.error(err); // something went wrong     return;   }   // If an intent was detected, log it out to the console.   if (response.intents.length > 0) {     console.log('Detected intent: #' + response.intents[0].intent);   }      // Display the output from dialog, if any.   if (response.output.text.length != 0) {       console.log(response.output.text[0]);   }   // Prompt for the next round of input.     var newMessageFromUser = prompt('>> ');     // Send back the context to maintain state.     conversation.message({       input: { text: newMessageFromUser },       context : response.context,     }, processResponse) }

In Android, using Watson Developer Cloud Java SDK:

private Map < String, Object > context = new HashMap < > ();
ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
service.setUsernameAndPassword("Your Watson service UserName", "Your watson service PassWord");
MessageRequest newMessage = new MessageRequest.Builder().inputText(inputmessage).context(context).build();
MessageResponse response = service.message("Your Workspace Id", newMessage).execute(); //Passing Context of last conversation     if(response.getContext() !=null)       {         context.clear();         context = response.getContext();      }

Check the complete Android code –  WatBot Repo on Github.

In Swift 3.0,  using Swift Watson SDK:

import Conversation
let username = "your-username-here"
let password = "your-password-here"
let version = "YYYY-MM-DD" // use today's date for the most recent version let conversation = Conversation(username: username, password: password, version: version) let workspaceID = "your-workspace-id-here" let failure = { (error: RestError) in print(error) } var context: Context? // save context to continue conversation conversation.message(workspaceID, failure: failure) { response in     print(response.output.text)     context = response.context }

In addition to maintaining our place in the conversation, the context can also be used to store any other data you want to pass back and forth between your application and the conversation service. This can include persistent data you want to maintain throughout the conversation (such as a customer’s name or account number), or any other data you want to track (such as the current status of option settings).

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
mobile ,watson ,chatbots

Published at DZone with permission of Vidyasagar Machupalli, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}