Tuesday, 30 January 2018

Call IBM Watson Conversation API from Java file

Here is a simple java file which can query a Watson Conversation workspace. The below code uses main method to prompt for question and then responds by getting the answer from Watson conversation. 

For your first question, it will return the default output of greetings. The following questions will receive the expected response as the context has been set. 

1] Create a Watson Conversation service in your IBM Cloud (previously known as Bluemix) account. Refer documentation.
2] Create a workspace in the Watson Conversation service. Here is a cars sample workspace on github. You can also check out the application hosted here.
3] Download the watson-developer-cloud jar file to your local system and keep it in your classpath. I have used the java-sdk-4.2.1-jar-with-dependencies.jar file.
4] Following is the code for WatsonConversation.java file

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.ibm.watson.developer_cloud.conversation.v1.Conversation;
import com.ibm.watson.developer_cloud.conversation.v1.model.Context;
import com.ibm.watson.developer_cloud.conversation.v1.model.InputData;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;

public class WatsonConversation {
    public static void main(String[] args) {
        BufferedReader br = null;
        MessageResponse response = null;
        Context context = new Context();
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                System.out.print("Enter input : ");
                String input = br.readLine();
                if ("q".equals(input)) {
                response = conversationAPI(input,context);
                System.out.println("Watson Response:"+ response.getOutput().getText().get(0));
                context = response.getContext();
        } catch (IOException e) {

    public static MessageResponse conversationAPI(String input, Context context){
        Conversation service = new Conversation(Conversation.VERSION_DATE_2017_05_26);
        service.setUsernameAndPassword("<username>", "<password>");
        String workspaceId = "<workspace ID>";
        InputData inputData = new InputData.Builder(input).build();       
        MessageOptions msgOptions = new MessageOptions.Builder(workspaceId).input(inputData).context(context).build();
        MessageResponse response = service.message(msgOptions).execute();
        return response;


5]Update the code above with the credentials of the Watson Conversation in your dashboard.
- Open the Watson Conversation service from IBM Cloud dashboard.
- Click on the Service Credentials. Expand the service credentials in the table. You will get the username and password there. In case you do not find the credentials. Create new credentials and use them.

6] Update the code with the workspace ID.
- Launch Watson Conversation service. Your workspaces are listed there.
- Click on the three dots of your workspace and select View details.
- It will give you the workspace ID.

7] Compile WatsonConversation.java and run it from the terminal. Ensure to have the jar file in the classpath. 

8] You will get prompt - 'Enter Input : ' and you will get a welcome or greeting message.

9] If you are using the cars sample workspace, then you can try 'turn on the lights', etc.

No comments:

Post a comment