0

Goal

Connect Amazon Lex with Kendra

Problem

.query() is expected to return ResultItems but it's returning ResultItems undefined.

Progress

lambda:

    var kendra = require("aws-sdk/clients/kendra");
    var kendraClient = new kendra({apiVersion: "2019-02-03", region: "us-west-2"});
    // var AWS = require("aws-sdk");
    // var kendraClient = new AWS.Kendra();
    exports.handler = async (event) => {
        // TODO implement
        try{
            const kendraResponse = await kendraClient.query(
                        {
                            IndexId: process.env.KENDRA_INDEX, 
                            QueryText: event.inputTranscript, 
                            QueryResultTypeFilter: "QUESTION_ANSWER",
                            PageNumber: 1
                        })
            console.log("this is kendraResponse", kendraResponse);
            console.log("This is event", event);
        const response = {
                    "dialogAction":
                        {
                         "fulfillmentState":"Fulfilled",
                         "type":"Close","message":
                            {
                              "contentType":"PlainText",
                              "content": kendraResponse.ResultItems ? "Got the response": "Sorry, unable to find the results"
                            }
                        }
                    }

        return response;
        } catch (error) {
            console.log(error)
        }
    };

KendraResponse:

A very long JSON object with configuration fields

TIA

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Zain Ul Abideen
  • 1,617
  • 1
  • 11
  • 25
  • 1
    Can you please post the response JSON as well here? Is the value of 'data' in returned json defined or nil? – Saurabh Sep 03 '20 at 16:00

1 Answers1

0

See the below changes to make it work.

var kendra = require("aws-sdk/clients/kendra");
var kendraClient = new kendra({apiVersion: "2019-02-03", region: "us-west-2"});
exports.handler = function (event)  { // use function here..
    try {
        var params = {
                        IndexId: process.env.KENDRA_INDEX, 
                        QueryText: event.inputTranscript, 
                        QueryResultTypeFilter: "QUESTION_ANSWER", // make sure you have QA indexed
                        PageNumber: 1
                    };

        kendraClient.query(params, function(err, data) {
                      if (err) console.log(err, err.stack); // an error 
                      else console.log("Kendra result is", data); // successful response
                    });
};
TestingInProd
  • 349
  • 10
  • 26