Search code examples
javaibm-watsonwatson-conversation

How can we set value of context variable of watson assistant using java?


Is it possible to set the value of context variable from java. I have modified the node json and added a action in that action in the result property I want to set data from my local database. So I am getting the action in java code and trying to set the value of result property of action object, below is the code which i am trying but it is not working. can some one suggest a better approach for this.

if(response.getActions()!=null) {
            System.out.println("actions : "+response.getActions());
            for (int i = 0; i < response.getActions().size(); i++) {
                System.out.println(" i : "+response.getActions().get(i).getName());
                if(response.getActions().get(i).getName()=="Apply_For_Loan") {
                    response.getActions().get(i).setResultVariable("123");
                }
            }
        }

For setting value of result_variable in to the context below is my code.

if(response.getActions().get(i).getName().equals("Apply_For_Loan")) {
                        System.out.println("in action");
                        Assistant service = new Assistant("2018-12-12");
                        service.setUsernameAndPassword(userId, password);
                        service.setEndPoint(endPoint);
                        String result=response.getActions().get(i).getResultVariable();
                        Context context = response.getContext();
                        context.put(result, "123");
                        MessageOptions msg=new MessageOptions.Builder(watsonId)
                              .input(new InputData.Builder("Apply For Loan").build())
                              .context(context)
                              .build();
                        response=service.message(msg).execute();
                        System.out.println("msg : "+response);
                    }

Below is the response which i am getting after re-executing the assistant call.

{
  "output": {
    "generic": [
      {
        "response_type": "text",
        "text": "Hello RSR, you loan application of 5420 is created. Please note the Loan Number for future use "
      }
    ],
    "text": [
      "Hello RSR, you loan application of 5420 is created. Please note the Loan Number for future use "
    ],
    "nodes_visited": [
      "node_1_1544613102320",
      "node_1_1544613102320"
    ],
    "log_messages": []
  },
  "input": {
    "text": "Apply For Loan"
  },
  "intents": [
    {
      "intent": "ApplyForLoan",
      "confidence": 1.0
    }
  ],
  "entities": [],
  "context": {
    "number": 9.971070056E9,
    "$Loan_Number": "123",
    "system": {
      "initialized": true,
      "dialog_stack": [
        {
          "dialog_node": "node_1_1544613102320"
        }
      ],
      "dialog_turn_counter": 3.0,
      "dialog_request_counter": 3.0,
      "_node_output_map": {
        "node_1_1544613102320": {
          "0": [
            0.0
          ]
        }
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    },
    "Mail_Id": "Email_Id",
    "conversation_id": "b59c7a02-2cc6-4149-ae29-602796ab22e1",
    "person": "RSR",
    "rupees": 5420.0
  },
  "actions": [
    {
      "name": "Apply_For_Loan",
      "type": "client",
      "parameters": {
        "pername": "RSR",
        "loanamount": 5420.0
      },
      "result_variable": "$Loan_Number"
    }
  ]
}

In above response $Loan_Number is the result variable which i have updated from java code and the same result_variable i am using in the output text to return the $Loan_Number, but in output text it is still coming blank and in actions also it is still coming blank?


Solution

  • The simple answer to your question is no, you can't set anything in the JSON Model using setResultVariable. It should work like this:

    1) Define an action on your Dialognode enter image description here

    The result_variable defines the <result_variable_name>, e.g. where the result of that action should be stored in the conversation context. If you execute some server side action, you read the result from the specified context location. In this example, the location is context.result_of_action, the context might be omitted. More details are here.

    2) Process the action in Java Client

    DialogNodeAction action = response.getActions().get(0);
    final String result_variable = action.getResultVariable();
    

    The result_variable is like a key. You obtain the value from your DB and add it to the context:

    Context context = response.getContext();
    context.put(result_variable, "value from DB");
    new MessageOptions.Builder("Your ID")
          .input(new InputData.Builder("Your response").build())
          .context(context) // setting context
          .build();
    

    Finally the Watson Assistant Dialog (or some other App) can get the result from the Client:

    "context": {
       "result_of_action": "value from DB",
    

    The Objects you read using the Java API are being parsed from JSON using Gson and need to be re-parsed back to JSON when construction a new API Call.

    Edit after question update

    In your example:

     "actions": [
    {
      "name": "Apply_For_Loan",
      "type": "client",
      "parameters": {
        "pername": "RSR",
        "loanamount": 5420.0
      },
      "result_variable": "$Loan_Number"
    }
    

    the key result_variable tells other modules in your app where to find the result of your action. So $Loan_Number should not be the value but the key to the value in the session context (a kind of method contract).
    Let's say you set the value to context.my_result, then your Watson Assistant should be able to access the "123" from DB in the next dialog node under "context.my_result".