首页 > 解决方案 > 使用 Lex 运行时在 Node JS 中发布内容

问题描述

我正在尝试开发一个应用程序,它将获取音频文件并使用 aws-sdk 将其发送到 Amazon Lex,特别是 lexruntime 到 postContent。

目前,我在网络上有音频文件,我正在本地下载这些音频文件,然后尝试将该音频文件的名称放入我的 postContent 参数中。但是,lex 正在返回一个空转录和一个引出意图,这意味着它无法正确处理我的音频文件。这是我下载/发布到 lex 的方式:

var file = fs.createWriteStream("file.wav");
var request = https.get(url, function(response) {
  response.pipe(file);
});

var params = {
    botAlias: 'prod', /* required */
    botName: 'OrderFlowers', /* required */
    //inputText: `${command}`, /* required */
    contentType: 'audio/x-l16; sample-rate=16000; channel-count=1', /*required */
    inputStream: './file.wav',
    accept: 'text/plain; charset=utf-8',
    userId: 'Test'/* required */
    //requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
    //sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
  };


lexruntime.postContent(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);  
}); 

以上从 Lex 得到以下响应:

{ contentType: 'text/plain;charset=utf-8',
  message: 'I didn\'t understand you, what would you like to do?',
  messageFormat: 'PlainText',
  dialogState: 'ElicitIntent',
  inputTranscript: '',
  audioStream: <Buffer > }

我知道音频文件已正确下载并映射到正确的 Lex 意图,但我认为我没有正确发送音频文件。如果最好不要从 URL 本地下载音频文件,我可以接受这种方法。

这是亚马逊文档:https ://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/LexRuntime.html

任何帮助,将不胜感激。

标签: node.jsaws-sdkaudio-streamingchatbotamazon-lex

解决方案


我需要以不同的格式保存文件,更改内容类型并创建一个读取流以发送到 lex。这里的解决方案:

var file = fs.createWriteStream("file.pcm");
var request = https.get(url, function(response) {
  response.pipe(file);
});

var params = {
    botAlias: 'prod', /* required */
    botName: 'OrderFlowers', /* required */
    //inputText: `${command}`, /* required */
    contentType: 'audio/lpcm; sample-rate=8000; sample-size-bits=16; channel-count=1; is-big-endian=false',
    accept: 'text/plain; charset=utf-8',
    userId: 'Test'/* required */
    //requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
    //sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
  };

var lexFileStream = fs.createReadStream("file.pcm");
params.inputStream = lexFileStream;


lexruntime.postContent(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);  
}); 

推荐阅读