gpt4 book ai didi

javascript - Watson Conversation - Oracle 数据库集成

转载 作者:行者123 更新时间:2023-12-03 02:12:04 24 4
gpt4 key购买 nike

大家早上好/下午好,我正在尝试让 Watson 返回从我们的 Oracle 数据库手动设置的响应。

我使用异步顺序访问数据库并返回响应,因为我遇到的第一个问题是数据库查询只有在 Watson 返回响应之后才会发生。 Async.waterfall 修复了这个问题。

我当前的问题:在控制台中,我看到所有内容都正确记录。查询数据库,正确设置response.output.text,然后返回到Watson,但我的聊天中没有出现任何响应。如果我在异步之前设置response.output.text =“asdfa”,则asdfa将按预期返回.

我在试图解决这个问题时不知所措,所以我感谢任何和所有的帮助。如果需要更多信息,请告诉我。

  // Send the input to the conversation service
conversation.message(payload, function (err, data) {
if (err) {
return res.status(err.code || 500).json(err);
}
return res.json(updateMessage(payload, data));
});
});

function updateMessage(input, response) {
var responseText = null;
if (!response.output) {
response.output = {};
} else {
// checkNames check
if (response.output.nodes_visited[0] === 'slot_11_1519333387192' && response.entities[0].entity === 'confirm') {

/* This code actually returns asdfa as a response from Watson.. */
// response.output.text = "asdfa";
// return response;
async.waterfall([
// this function queries the database
// TODO: module out the Oracle connection parts once POC is completed
function queryDB(callback) {
console.log('Starting queryDB');
var query = "SELECT column_name FROM table@prod WHERE column_id = '" + response.context.VSUID + "'";
oracledb.getConnection('hr',
function (err, connection) {
var conn = oracleGetConnection(err, connection);
conn.execute(query, {}, {
outFormat: oracledb.OBJECT
},
function (err, result) {

console.log('result from Oracle: ', result);
// pass a null error and the result of the query

callback(null, result.rows[0]);
});
});
},
// this function formats the result of the query
// TODO: this should not be it's own function. This can happen at the same time the db gets the row
function formatName (arg1, callback) {
console.log('this should happen after query..');
console.log('arg1: ', arg1);

var r = JSON.stringify(arg1);
r = r.substring((r.indexOf(':') + 1) + 1, r.length - 2);
console.log('Name is: ', r);
// pass a null error and the formatted name
callback(null, r);
}
],
// Final function to be ran after the two above have completed
function finalFunction (err, result) {
if (err) {
console.log('uh oh async err: ', err);
} else {
console.log('This is final Function');
// set output text
response.output.text = 'Is your name ' + result + '?';
// response.context.dbResponse = 'Is your name ' + result + '?';
// response.output.text = "asdfasdfasd";
// console.log('This is the value of response\n\n', response);
// var resp = returnResponse(input, response);
response.context.dbResponse = response.output.text[0];
return returnResponse(input, response);
// return response;
}
});
// response.output.text = "asdfa";
console.log('This is response.output.text ', response.output.text);
return response;
} else {
//If no special if case to query the db just run Watson Conversation stock
return returnResponse(input, response);
}

} }

这是一个示例控制台日志。

This logs the Input from the user:
name 111111111
This logs the Response from Watson:
Is 111111111correct?
This logs the intent recognized, if any:
nameCheck
This logs the entity recognized, if any:
VSUID
This logs the text that is being returned to the user: [ 'Is 111111111correct?'
]
Starting queryDB
Connected to database
result from Oracle: { outBinds: undefined,
rowsAffected: undefined,
metaData: [ { name: 'TABLE_FIRST_NAME' } ],
rows: [ [ 'Tyler' ], [ 'Tyler' ] ],
resultSet: undefined }
this should happen after query..
arg1: [ 'Tyler' ]
Name is: "Tyler
This is final Function
This logs the Input from the user:
yes
This logs the Response from Watson:
Is your name "Tyler?
This logs the entity recognized, if any:
confirm
This logs the text that is being returned to the user: Is your name "Tyler?

最佳答案

编写如下代码将使您面临 SQL 注入(inject)漏洞(也可能出现性能问题):

var query = "SELECT column_name FROM table@prod WHERE column_id = '" + response.context.VSUID + "'";

请阅读section of the documentation on bind variables .

关于你的问题...

您将 updateMessage 视为同步函数,但它正在执行异步工作。执行异步工作的函数需要异步 API,例如 Node.js 风格的回调、Promises 或 AsyncFunctions (async/await)。

如果您看到您提供的代码的第 73 行,则表示您正在“返回”响应对象,但它不在 async.waterfall 调用之外。由于 Node.js 的异步特性,甚至第 67 行的 return 也不起作用。

这是我最近尝试描述这一切是如何工作的: https://www.youtube.com/watch?v=iAdeljxq_hs

您可以在此处访问幻灯片和示例代码: https://www.dropbox.com/s/quu7oxiug0gh6ua/Understanding%20Async%20Processing%20and%20Patterns%20in%20Node.js.zip?dl=0

在示例代码的 code > header-detail 目录中,您将看到 5 个不同的文件,它们以 header-detail-with- 开头,但其名称为您可以做出不同的 API 选择。您必须对 updateMessage API 做出类似的选择。

要运行测试,请使用ddl.sql文件创建目标表,然后根据您的环境需要编辑db-config.js,最后运行来自该目录中终端的 node test.js 1。您可以更改末尾的数字来运行不同的测试文件。

关于javascript - Watson Conversation - Oracle 数据库集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com