【问题标题】:Using Lex Runtime to Post Content in Node JS使用 Lex 运行时在 Node JS 中发布内容
【发布时间】:2021-08-23 22:45:27
【问题描述】:

我正在尝试开发一个应用程序,它将获取音频文件并使用 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.js aws-sdk audio-streaming chatbot amazon-lex


    【解决方案1】:

    我需要以不同的格式保存文件,更改内容类型并创建读取流以发送到 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);  
    }); 
    

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 1970-01-01
      • 2013-09-05
      • 2019-05-30
      • 2018-08-04
      • 1970-01-01
      • 2022-06-21
      • 2018-03-06
      • 2020-04-11
      相关资源
      最近更新 更多