Skip to content

Latest commit

 

History

History
290 lines (234 loc) · 15.9 KB

README.md

File metadata and controls

290 lines (234 loc) · 15.9 KB

WatBot - An Android ChatBot powered by IBM Watson

Build Status

WatBot is an IBM Watson powered ChatBot running on Android and using Conversation, Text-to-Speech, Speech-to-Text Services on IBM Bluemix (an open standards, cloud platform for building, running, and managing apps and services).

Coding the app on Android Studio

Android Studio is the Official IDE for Android. Android Studio provides the fastest tools for building apps on every type of Android device.

Clone the repo and import the code in Android Studio,

git clone https://github.com/IBM-Bluemix/chatbot-watson-android.git

Config.xml

Navigate to file app/src/main/res/values/config.xml and provide Bluemix service credentials

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Watson Conversation Service Credentials -->

    <string name="workspace_id"></string>

    <string name="conversation_username"></string>

    <string name="conversation_password"></string>



    <!--Watson Speech-To-Text Service Credentials-->

    <string name="STT_username"></string>

    <string name="STT_password"></string>



    <!--Watson Text-To-Speech Service Credentials-->

    <string name="TTS_username"></string>

    <string name="TTS_password"></string>



    <!--Bluemix Mobile Analytics-->

    <string name="mobileanalytics_apikey"></string>
</resources>

Creation of Conversation Service

Watson Conversation combines a number of cognitive techniques to help you build and train a bot - defining intents and entities and crafting dialog to simulate conversation.

Watson Conversation Service Overview

Getting started

Before you can start using the Conversation service, log in to IBM® Bluemix® and create a service instance.
  1. Log in to Bluemix and navigate to the Conversation service:
    • Don’t have Bluemix account? Sign up to create a free trial account.
    • Have a Bluemix account? Use this link.
  2. In the Service name field, type a unique name for your new instance of the Conversation service. Check the “Pricing Plans” for data limits for the Conversation service
  3. Click Create. You’ll see details about your new instance in the “Service Details” page.

Creating a Workspace

You use the Conversation tool to create workspaces by either creating a new workspace from scratch, or  by importing a workspace from a [JSON](https://github.com/IBM-Bluemix/chatbot-watson-android/blob/master/sample.json) file. You can also duplicate an existing workspace within the same service instance.
  1. If the Service Details page is not already open, click your Conversation service instance on the Bluemix console. (When you create a service instance, the Service Details page displays.)
  2. On the “Service Details” page, scroll down to Conversation tooling and click Launch tool.
  3. Click Create to create a new workspace.
  4. Specify the details for the new workspace:
    • Name: A name no more than 64 characters in length. This value is required.
    • Description: A description no more than 128 characters in length.
    • Language: The language of the user input the workspace will be trained to understand. The service supports Brazilian Portuguese, English, French, Italian, and Spanish.
  5. Click Create. The new workspace is created and now appears as a tile on the Workspaces page.

Creating an intent

You use the Conversation tool to create intents. The number of intents and examples you can create in a single service instance depends on your Conversation service plan:

Create some intents.

  1. In the Conversation tool, open your workspace and then click the Intents tab.
  2. Click Create new.
  3. In the Intent name field, type a descriptive name for the intent. The intent name can contain letters (in Unicode), numbers, underscores, hyphens, and dots. Intent names cannot contain spaces and must not exceed 128 characters. The following are examples of intent names:
    • #weather_conditions
    • #pay_bill
    • #escalate_to_agent
    Tip: Don’t include the # character in the intent names when you create them in the Conversation tool.
  4. In the User example field, type the text of a user example for the intent. An example can be any string up to 1024 characters in length. The following might be examples for the #pay_bill intent:
    • I need to pay my bill.
    • Pay my account balance
    • make a payment
    Important: Intent names and example text can be exposed in URLs when an application interacts with the service. Do not include sensitive or personal information in these artifacts.

    Press Enter or click + to save the example.

  5. Repeat the same process to add more examples. Provide at least 5 examples for each intent. The more examples you provide, the more accurate your application can be.

    Screen

  6. When you have finished adding examples, click Create to finish creating the intent.

Results

The intent you created is added to the Intents tab, and the system begins to train itself on the new data.

You can click any intent in the list to open it for editing. You can make the following changes:

  • Rename the intent.
  • Delete the intent.
  • Add, edit, or delete examples.
  • Move an example to a different intent.
To move an example, select the example by clicking the check box and then click Move to.

“Screen

Testing your intents

After you have finished creating new intents, you can test the system to see if it recognizes your intents as you expect.
  1. In the Conversation tool, click the “Ask icon.
  2. In the Try it out panel, enter a question or other text string and press Enter to see which intent is recognized. If the wrong intent is recognized, you can improve your model by adding this text as an example to the correct intent.Tip: If you have recently made changes in your workspace, you might see a message indicating that the system is still retraining. If you see this message, wait until training completes before testing:

    Screen

    The response indicates which intent was recognized from your input.

    Screen

  3. If the system did not recognize the correct intent, you can correct it. To correct the recognized intent, click the displayed intent and then select the correct intent from the list. After your correction is submitted, the system automatically retrains itself to incorporate the new data.

    Screen

    If your intents are not being correctly recognized, consider making the following kinds of changes:
    • Add the unrecognized text as an example to the correct intent.
    • Move existing examples from one intent to another.
    • Consider whether your intents are too similar, and redefine them as appropriate.

Creating an entity

You use the Conversation tool to create entities. The number of entities, entity values, and synonyms you can create in a single service instance depends on your Conversation service plan:
  1. In the Conversation tool, open your workspace and then click the Entities tab.
  2. Click Create new.
  3. In the Add the entity name field, type a descriptive name for the entity.The entity name can contain letters (in Unicode), numbers, underscores, and hyphens. For example:
    • @location
    • @menu_item
    • @product
    Tips:
    • Don’t include the @ character in the entity names when you create them in the Conversation tool.
    • Entity names can’t contain spaces or be longer than 64 characters. And entity names can’t begin with the string sys-, which is reserved for system entities.
  4. In the Value field, type the text of a possible value for the intent. An entity value can be any string up to 64 characters in length.Important: Don’t include sensitive or personal information in entity names or values. The names and values can be exposed in URLs in an app.
  5. In the Synonyms field, type any synonyms for the entity value. A synonym can be any string up to 64 characters in length. Press Enter to save each synonym.Screen
  6. Click + and repeat the process to add more entity values.
  7. When you are finished adding values and synonyms, click Create.

Building a Dialog

The dialog component of the Conversation service uses the intents and entities that are identified in the user’s input to gather required information and provide a useful response. Your dialog is represented graphically as a tree; create a branch to process each intent that you define.

Post branching Intents and entities, this is how my Conversation Dialog on Bluemix looks like

Configure the App

To configure the App you need to get the Watson Conversation service Username, PassWord and WorkSpaceId

  • In the MainActivity class locate the method named sendMessage().

      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).build();
    
      MessageResponse response = service.message("Your Workspace Id", newMessage).execute();
    
  • Go to the Conversation service , and select the Service Credentials tab. Select password and username.

Conversation Credentials

Add the `password` and `username` in config.xml ,

service.setUsernameAndPassword("Your Watson service UserName", "Your watson service PassWord");

  • Next is to get the workspace Id.

Launch the conversation service workspace and from the options select the View details.

Get the Workspace ID: and add it in config.xml,

MessageResponse response = service.message("Your Workspace Id", newMessage).execute();
  • Build and Run your app.

Enable Text to Speech

  • Create a Watson Text to Speech(TTS) service on Bluemix
  • Navigate to Service Credentials tab and click on "View Credentials".
  • Add the credentials to config.xml
  • Build and Run your app.

Now when you TAP on any message, the text will be heard via a Voice (Voice.EN_LISA). You can change the voice formats in the code (Lines 82-84 0f MainActivity.java)

Note: The required gradle entries for TTS is already included in the build.gradle file

compile 'com.ibm.watson.developer_cloud:text-to-speech:3.5.3'
compile 'com.ibm.watson.developer_cloud:android-sdk:0.2.3'

Enable Speech to Text

  • Create a Watson Speech-To-Text (STT) service on Bluemix
  • Navigate to Service Credentials tab and click on "View Credentials".
  • Add the credentials to config.xml
  • Build and Run your app.

Note: The required gradle entries for STT is already included in the build.gradle file

compile 'com.ibm.watson.developer_cloud:speech-to-text:3.5.3'
compile 'com.ibm.watson.developer_cloud:android-sdk:0.2.3'
compile 'com.squareup.okhttp3:okhttp-ws:3.4.2'

Mobile Analytics

  • Create a Mobile Analytics service on Bluemix.
  • Click on Service Credentials tab and add the apikey to config.xml.
  • Add the below entry to build.gradle(module:app) under dependencies
compile 'com.ibm.mobilefirstplatform.clientsdk.android:analytics:1.+'
  • Add the below code to MainActivity.Java
//Bluemix Mobile Analytics
        BMSClient.getInstance().initialize(getApplicationContext(), BMSClient.REGION_US_SOUTH);
        //Analytics is configured to record lifecycle events.
        Analytics.init(getApplication(), "WatBot", analytics_APIKEY, false, Analytics.DeviceEvent.ALL);
        //Analytics.send();
        myLogger = Logger.getLogger("myLogger");
        // Send recorded usage analytics to the Mobile Analytics Service
        Analytics.send(new ResponseListener() {
            @Override
            public void onSuccess(Response response) {
                // Handle Analytics send success here.
            }

            @Override
            public void onFailure(Response response, Throwable throwable, JSONObject jsonObject) {
                // Handle Analytics send failure here.
            }
        });

        // Send logs to the Mobile Analytics Service
        Logger.send(new ResponseListener() {
            @Override
            public void onSuccess(Response response) {
                // Handle Logger send success here.
            }

            @Override
            public void onFailure(Response response, Throwable throwable, JSONObject jsonObject) {
                // Handle Logger send failure here.
            }
        });

Chat with your own WatBot

If you have followed all the above instructions, you should be happily chatting with your Wat(son)Bot.

** Remember your bot will be talking to your Conversation Service (Intents, Entities and Dialog).**

Don't stop here!!! Keep coding and using Bluemix

License

See License.txt for license information.