【问题标题】:How can we set value of context variable of watson assistant using java?我们如何使用 java 设置 watson 助手的上下文变量的值?
【发布时间】:2018-12-20 11:41:51
【问题描述】:

是否可以从 java.xml 设置上下文变量的值?我已经修改了节点 json,并在我想从本地数据库设置数据的结果属性中的该操作中添加了一个操作。所以我在java代码中获取动作并尝试设置动作对象的结果属性的值,下面是我正在尝试但它不起作用的代码。有人可以为此提出更好的方法吗?

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");
                }
            }
        }

在下面的上下文中设置 result_variable 的值是我的代码。

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);
                    }

以下是我重新执行助手呼叫后得到的响应。

{
  "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"
    }
  ]
}

在上面的响应中 $Loan_Number 是我从 java 代码更新的结果变量,我在输出文本中使用相同的 result_variable 返回 $Loan_Number,但在输出文本中它仍然是空白的,并且在操作中它也是还是一片空白?

【问题讨论】:

  • 更改 result_variable 值后,当我试图从响应中获取它时,我无法获取它,这就是我说它不起作用的原因。我期望当我从 java 更新结果变量值时,它应该反映在 watson assistant api 中的上下文变量中。所以我可以直接从那里将响应发送给用户。
  • 你使用的是哪个版本的api?
  • 我使用的是version1

标签: java ibm-watson watson-conversation


【解决方案1】:

您的问题的简单答案是否定的,您不能使用setResultVariable 在 JSON 模型中设置任何内容。它应该像这样工作:

1) 在您的 Dialognode 上定义一个操作

result_variable 定义了&lt;result_variable_name&gt;,例如该操作的结果应存储在对话上下文中。如果您执行一些服务器端操作,您将从指定的上下文位置读取结果。在此示例中,位置为context.result_of_actioncontext 可能会被省略。更多细节是here

2) 在 Java 客户端中处理操作

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

result_variable 就像一个。您从数据库中获取值并将其添加到上下文中:

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();

最后,Watson Assistant Dialog(或其他一些 App)可以从客户端获取结果:

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

您使用 Java API 读取的对象正在使用 Gson 从 JSON 解析,并且在构建新的 API 调用时需要重新解析回 JSON。

问题更新后编辑

在您的示例中:

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

result_variable 键告诉您应用程序中的其他模块在哪里可以找到您的操作结果。所以$Loan_Number 不应该是 value 而是会话上下文中值的 key(一种方法契约)。
假设您将值设置为 context.my_result,那么您的 Watson Assistant 应该能够在“context.my_result”下的下一个对话框节点中从 DB 访问“123”。

【讨论】:

  • 我按照建议做了,当我打印消息选项对象时,我能够看到结果,但是我如何将这个消息选项设置到我之前的响应中,所以我得到了相同的输出具有我在相同 java 代码中的 result_variable 中更新的新值的节点?
  • 我也通过再次执行服务调用检查了响应,但是上下文具有我从 result_variable 中的 java 代码更新的值,但它没有反映到响应输出中?
  • 我已按问题更新了更改,请提出建议
  • @briageus 是否有可能我可以将上下文变量值显示为链接,如果是,那么如何?
猜你喜欢
  • 2020-12-04
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 2016-03-11
相关资源
最近更新 更多