Search code examples
c#botframeworkazure-qna-maker

How to pass metadata to QnAMaker Request


I am trying pass metadata filter to my QnAMakerDialog request but cannot figure out how to do it. I have tried adding the values to the dialog that is forwarded but this did not work.

LuisDialog.cs method that calls QnAMaker

public List<Metadata> _metadataFilter { get; set; }

[LuisIntent("")]
[LuisIntent("None")]
public async Task NoneIntent(IDialogContext context, IAwaitable<IMessageActivity> message, LuisResult result)
{
    string filterValue = context.UserData.GetValue<string>("filter");
    var messageToForward = await message;
    var qnaDialog = new QnADialog();
    _metadataFilter = new List<Metadata>()
    {
        new Metadata()
        {
            Name = "filter",
            Value = filterValue
        }
    };

    await context.Forward(qnaDialog, AfterQnADialog, messageToForward, CancellationToken.None);
}

QnAMakerDialog.cs

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using QnAMakerDialog;
using QnAMakerDialog.Models;

namespace ChatBot.Dialogs
{

    [Serializable]
    [QnAMakerService("","","")]
    public class QnADialog : QnAMakerDialog <bool>
    {

        public override async Task NoMatchHandler(IDialogContext context, string originalQueryText)
        {
            await context.PostAsync($"Sorry, I don't know '{originalQueryText}' yet");
            context.Done(this);
        }

        public override async Task DefaultMatchHandler(IDialogContext context, string originalQueryText, QnAMakerResult result)
        {
            // Posting top QnA Maker result to user
            var messageActivity = (result.Answers.First().Answer);
            await context.PostAsync(messageActivity);
            context.Done(this);
        }
    }
}

The QnAMakerServiceAttribute includes the metadata filter, however it is a list and my understanding is you cannot add a list to attribute.

QnAMakerServiceAttribute.cs

using QnAMakerDialog.Models;
using System;
using System.Collections.Generic;

namespace QnAMakerDialog
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)]
    [Serializable]
    public class QnAMakerServiceAttribute : Attribute
    {
        public string BaseUri { get; set; }

        public string EndpointKey { get; set; }

        public string KnowledgeBaseId { get; set; }

        public int MaxAnswers { get; set; }

        public List<Metadata> MetadataBoost { get; set; }

        public List<Metadata> MetadataFilter { get; set; } 

        public QnAMakerServiceAttribute(string baseUri, string endpointKey, string knowledgeBaseId, int maxAnswers = 5)
        {
            this.BaseUri = baseUri;
            this.MaxAnswers = maxAnswers;
            this.EndpointKey = endpointKey;
            this.KnowledgeBaseId = knowledgeBaseId;
        }

   }
}

Solution

  • trying pass metadata filter to my QnAMakerDialog request but cannot figure out how to do it.

    You can refer to the following code snippet to specify MetadataFilter for your QnADialog.

    var qnaDialog = new QnADialog()
    {
        MetadataFilter = new List<Metadata>()
        {
            new Metadata()
            {
                Name = "filter",
                Value = filterValue
            }
        }
    };
    
    await context.Forward(qnaDialog, AfterQnADialog, messageToForward, CancellationToken.None);
    

    The above code work for me, and if use fiddler to capture request, we can find the request body contains "strictFilters":

    enter image description here