Watson Assistant (formerly Conversation) on Android

gradle settings For android studio Add the following to gradle (App.module)

implementation 'com.ibm.watson.developer_cloud:android-sdk:0.5.0'
implementation 'com.ibm.watson.developer_cloud:java-sdk:6.14.0'

0.5.0 seems to work without it.

Since there is communication, please add the following to AndroidManifest.xml. <uses-permission android:name="android.permission.INTERNET" />

For simplicity, synchronous communication is used, so a NetworkOnMainThreadException should be issued before the communication process. To avoid it, add the following description.

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Watson Assistant has v1 and v2. The API used for each is different. First, check which version you want to use on the settings screen. The following is an explanation when operating with v1.

[API Key] Authentication is a little confusing. It seems that it used to authenticate with username and password, but now the specification has changed to IAM and it authenticates with API key. You can configure watson from the IBM Cloud dashboard, and you can find your IAM key on the target service (Assistant) configuration page. We will use this key to authenticate, so if someone else uses it, your account may be charged.

[EndPoint] EndPoint is used as a set with the IAM key. It is a URI to access, but in the case of Assitant, there are multiple candidates. If you don't know, you can check it on the settings page. If the server is Washington DC https://gateway-wdc.watsonplatform.net/assistant/api It will be.

[WorkSpace] I had a hard time finding this no matter where I looked for it. In IBM hands-on, it is supposed to be described on the setting page, but that column is missing on the current page and can not be confirmed. Go to Create a skill-> Target Instance to launch the tool that configures the Assistant's conversation. ~~ The WorkSpace ID is hidden in the URI on the browser at that time. It is located in ~ workspaces / here / build ~. ~~ Addendum: I was able to confirm it with the 3-point dot-> View API Detail on the upper right of the frame of each skill on the screen for selecting the created skill. </ font>

Simple sample

SampleAssistant.java


private static final String WORKSPACE_ID = "";
private static final String IAM_Key = "";
private static final String URI = "";

~Omission~

String sendMessage = "Hello";  //String to send to Watson
IamOptions iamOptions = new IamOptions.Builder().apiKey(IAM_Key).build();
Assistant service = new Assistant("2018-09-20", iamOptions);
service.setEndPoint(URI);

InputData input = new InputData.Builder(sendMessage).build();

MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID)
  .input(input)
  .build();

MessageResponse response = service.message(options).execute();

System.out.println(response);

If nothing is done, the reaction will always be at the dialog root node. There seem to be several ways to continue the session, but the easiest way is to pass the context received in the reply from watson to the next request.

Context watsonContext = response.getContext();  //And receive

SampleAssistant2.java


MessageOptions options;
if(watsonContext != null) {
    options = new MessageOptions.Builder(WORKSPACE_ID)
      .input(input)
      .context(watsonContext)  //Pass to the next request
      .build();
}else{
    options = new MessageOptions.Builder(WORKSPACE_ID)
    .input(input)
    .build();
}
MessageResponse response = watsonAssistant.message(options).execute();
watsonContext = response.getContext();

Each feature provided by IBM's watson is provided with explanations (English) with sample code for various languages. API Reference V1 https://console.bluemix.net/apidocs/assistant API Reference V2 https://cloud.ibm.com/apidocs/assistant-v2 https://www.ibm.com/cloud/watson-assistant/

IBM Tutorial (Japanese) https://cloud.ibm.com/docs/services/assistant?topic=assistant-getting-started#getting-started

watson is free to use within a certain range without registering a credit card. Why don't you touch it to get a feel for using AI services? In particular, Assistant is recommended because it is conceptually easy to understand and can be applied to various sites. There seems to be a module for Wordpress.

Recommended Posts

Watson Assistant (formerly Conversation) on Android
Let's go with Watson Assistant (formerly Conversation) ④ How to link with Java logic
Let's go with Watson Assistant (formerly Conversation) ② Make a hotel reservation chatbot
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
[Android] Notes on xml
Until the Google Assistant sample runs on Android Things
Customize list view on Android
Multipart transmission library on Android
Use serial communication on Android
ROS app development on Android
Use native code on Android