[英]IBM Watson Assistant: How to enable user metrics
我們想知道每個用戶從客戶端應用程序調用 IBM Watson Assistant 服務的次數以及每個用戶的賬單詳細信息。 我正在嘗試根據此 URL( https://console.bluemix.net/docs/services/assistant/logs_oview.html#user_id )為 watson assistant 服務啟用用戶指標,並在我的 node.js 代碼中添加標頭和元數據. 但是當我在對話中檢查“改進”選項卡時,它沒有顯示用戶詳細信息,它顯示計數為 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;
您需要將用戶 ID 信息添加到調用消息 API時發送的上下文中。 對 Watson Assistant 的每個請求都可以有不同的userid ,例如,當您有一個多租戶應用程序並且您的應用程序/服務器並行處理多個用戶及其請求時。
查看消息調用的上下文部分,它包含有關將數據放置在 JSON 結構中的位置的信息。 您可以將上下文更新合並到消息有效負載中:
{
workspace_id: '{workspace_id}',
input: {'text': 'Hello'},
context: {
metadata : {
"user_id": "{1234}"
}
}
}
一旦您的應用程序發送了不同的 user_id 值,您應該會在“改進”儀表板中看到以下內容(我在我的 Lite 計划中使用 2 個用戶 ID 進行了測試)。 不同的活躍用戶:
此外,您可以檢查日志。 在該對話條目中,上下文元數據中的 user_id 字段 - 與您的應用程序作為消息請求的一部分發送的字段相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.