How to Add filter on the REST API Query to view the answers from QnAmaker?

我与影子孤独终老i 提交于 2020-03-03 06:25:04

问题


I'm using the following code in my chat bot (using v4 azure MS bot framework), to query the question and answers (Client side code - using plain JavaScript and J Query),

  function generateAnswer() 
  {
        var question = {
            question: "will you marry me"
        }
        $.ajax({
            type: "POST",
            url: "https://YourEndPointURL/qnamaker/knowledgebases/eb895acb-e034-4f7c-asda7c-1955458ecec6/generateAnswer&$filter=source eq 'Editorial'",
            data: JSON.stringify(question),
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization','EndpointKey c44444_Your_Endpoint_Key_4556');
            },
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                console.log(data);
                console.log(data.answers[0].answer);   
            }
        });
    }

while using this code, i"m getting the following error response

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

So please help me with the correct syntax to apply filter for my query.


回答1:


According to https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/metadata-generateanswer-usage, you need to specify filters in the body (the data property)

function generateAnswer() 
  {
        var data = {
            question: "will you marry me",
            strictFilters: [
            {
              "name": "source",
              "value": "Editorial"
            }],
        }
        $.ajax({
            type: "POST",
            url: "https://YourEndPointURL/qnamaker/knowledgebases/eb895acb-e034-4f7c-asda7c-1955458ecec6/generateAnswer",
            data: JSON.stringify(data),
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization','EndpointKey c44444_Your_Endpoint_Key_4556');
            },
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                console.log(data);
                console.log(data.answers[0].answer);   
            }
        });
    }

Moreover, you are missing 2 things:

  1. your hostname, to replace YourEndPointURL
  2. endpoint key, to replace c44444_Your_Endpoint_Key_4556


来源:https://stackoverflow.com/questions/59875337/how-to-add-filter-on-the-rest-api-query-to-view-the-answers-from-qnamaker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!