ערוך

שתף באמצעות


Filter Responses with Metadata

QnA Maker lets you add metadata, in the form of key and value pairs, to your pairs of questions and answers. You can then use this information to filter results to user queries, and to store additional information that can be used in follow-up conversations.

Note

The QnA Maker service is being retired on the 31st of March, 2025. A newer version of the question and answering capability is now available as part of Azure AI Language. For question answering capabilities within the Language Service, see question answering. Starting 1st October, 2022 you won’t be able to create new QnA Maker resources. For information on migrating existing QnA Maker knowledge bases to question answering, consult the migration guide.

Store questions and answers with a QnA entity

It's important to understand how QnA Maker stores the question and answer data. The following illustration shows a QnA entity:

Illustration of a QnA entity

Each QnA entity has a unique and persistent ID. You can use the ID to make updates to a particular QnA entity.

Use metadata to filter answers by custom metadata tags

Adding metadata allows you to filter the answers by these metadata tags. Add the metadata column from the View Options menu. Add metadata to your knowledge base by selecting the metadata + icon to add a metadata pair. This pair consists of one key and one value.

Screenshot of adding metadata

Filter results with strictFilters for metadata tags

Consider the user question "When does this hotel close?", where the intent is implied for the restaurant "Paradise."

Because results are required only for the restaurant "Paradise", you can set a filter in the GenerateAnswer call on the metadata "Restaurant Name". The following example shows this:

{
    "question": "When does this hotel close?",
    "top": 1,
    "strictFilters": [ { "name": "restaurant", "value": "paradise"}]
}

Filter by source

In case you have multiple content sources in your knowledge base and you would like to limit the results to a particular set of sources, you can do that using the reserved keyword source_name_metadata as shown below.

"strictFilters": [
    {
        "name": "category",
        "value": "api"
    },
   {
        "name": "source_name_metadata",
        "value": "boby_brown_docx"
    },
   {
        "name": "source_name_metadata",
        "value": "chitchat.tsv"
   }
]

Logical AND by default

To combine several metadata filters in the query, add the additional metadata filters to the array of the strictFilters property. By default, the values are logically combined (AND). A logical combination requires all filters to matches the QnA pairs in order for the pair to be returned in the answer.

This is equivalent to using the strictFiltersCompoundOperationType property with the value of AND.

Logical OR using strictFiltersCompoundOperationType property

When combining several metadata filters, if you are only concerned with one or some of the filters matching, use the strictFiltersCompoundOperationType property with the value of OR.

This allows your knowledge base to return answers when any filter matches but won't return answers that have no metadata.

{
    "question": "When do facilities in this hotel close?",
    "top": 1,
    "strictFilters": [
      { "name": "type","value": "restaurant"},
      { "name": "type", "value": "bar"},
      { "name": "type", "value": "poolbar"}
    ],
    "strictFiltersCompoundOperationType": "OR"
}

Metadata examples in quickstarts

Learn more about metadata in the QnA Maker portal quickstart for metadata:

Use question and answer results to keep conversation context

The response to the GenerateAnswer contains the corresponding metadata information of the matched question and answer pair. You can use this information in your client application to store the context of the previous conversation for use in later conversations.

{
    "answers": [
        {
            "questions": [
                "What is the closing time?"
            ],
            "answer": "10.30 PM",
            "score": 100,
            "id": 1,
            "source": "Editorial",
            "metadata": [
                {
                    "name": "restaurant",
                    "value": "paradise"
                },
                {
                    "name": "location",
                    "value": "secunderabad"
                }
            ]
        }
    ]
}

Next steps