首页 文章

IBM Watson Assistant:如何启用用户指标

提问于
浏览
3

我们想知道每个用户从客户端应用程序调用IBM Watson Assistant服务的次数以及每个用户的帐单详细信息 . 我正在尝试基于此URL(https://console.bluemix.net/docs/services/assistant/logs_oview.html#user_id)为watson助理服务启用用户指标,并在我的node.js代码中添加了标头和元数据 . 但当我检查对话中的 Improve 标签时,它没有显示用户详细信息,其显示计数为0 .

我正在使用LITE计划,下面是代码 .

// conversation config
var conversation = new ConversationV1({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: process.env.CONVERSATION_USERNAME,
password: process.env.CONVERSATION_PASSWORD,
version_date: '2018-02-16',
version: 'v1',
context : {
        metadata : {
           "user_id": "{1234}"
           }
         },
headers: {'X-Watson-Metadata':'customer_id=user777;customer_id=xyz'}
});

app.js代码:

'use strict';

require('dotenv').config({ silent: true });

var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests

//setup watson services
var ConversationV1 = require('watson-developer-cloud/conversation/v1'); // 
watson sdk
var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');

var app = express();
// Bootstrap application settings
 app.use(express.static('./public')); // load UI from public folder
 app.use(bodyParser.json()); 
// conversation config

var conversation = new ConversationV1({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: process.env.CONVERSATION_USERNAME || 'replace with the 
username',
password: process.env.CONVERSATION_PASSWORD || 'replace with the 
password',
version_date: '2018-02-16', //set currenct date, check here 
https://www.ibm.com/watson/developercloud/conversation/api/v1/#versioning
version: 'v1',
context : {
metadata : {
   "user_id": "{1234}"
  }
 },
headers: {'X-Watson-Metadata':'customer_id=user777;customer_id=xyz'}
});
// Endpoint to be call from the client side for message
app.post('/api/message', (req, res) => {
var workspace = process.env.WORKSPACE_ID || '<workspace-id>';
if (!workspace || workspace === '<workspace-id>') {
    return res.json({
        'output': {
            'text': 'Please update the WORKSPACE_ID in your .env file with 
 your credential! If you did update, try to verify if the file are just 
with the name: .env'
        }
    });
  }
  var payload = {
    workspace_id: workspace,
    context: {},
    input: {}
 };
 if (req.body) {
    if (req.body.input) {
        payload.input = req.body.input;
    }
    if (req.body.context) {
        // The client must maintain context/state
        payload.context = req.body.context;
    }
 }
  // Send the input to the conversation service
 conversation.message(payload, function(err, data) {
    if (err) {
        return res.status(err.code || 500).json(err);
    }
    updateMessage(res, payload, data);
 });
});
function updateMessage(res, input, response) {
if (!response.output) {
    response.output = {};
} else if (response.output && response.output.text) {
    return res.json(response);
}
}

 module.exports = app;

1 回答

  • 1

    你需要在调用message API时发送add the user id information to the context . 对Watson Assistant的每个请求都可以有不同的 userid ,例如,当您拥有多租户应用程序且您的应用程序/服务器并行处理多个用户及其请求时 .

    查看the context section for the message call,它包含有关将数据放在JSON结构中的位置的信息 . 您可以将上下文更新合并到消息有效内容中:

    {
      workspace_id: '{workspace_id}',
      input: {'text': 'Hello'},
      context: {
           metadata : {
             "user_id": "{1234}"
           }
      }
    }
    

    一旦您的应用发送了不同的user_id值,您应该在 Improve 仪表板中看到以下内容(我在我的Lite计划中测试了2个用户ID) . 不同的活跃用户:

    2 active users

    每位用户的平均会话次数:
    average conversations per user

    此外,您还可以查看日志 . 在该会话中,条目是上下文元数据中的user_id字段 - 与作为消息请求的一部分发送的应用程序相同 .

相关问题