如何将元数据传递给 QnAMaker 请求

How to pass metadata to QnAMaker Request

我正在尝试将元数据过滤器传递给我的 QnAMakerDialog 请求,但不知道如何操作。我已尝试将值添加到转发的对话框中,但这没有用。

LuisDialog.cs 调用 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);
        }
    }
}

QnAMakerServiceAttribute 包括元数据过滤器,但它是一个列表,我的理解是您不能向属性添加列表。

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;
        }

   }
}

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

您可以参考以下代码片段为您的QnADialog指定MetadataFilter

var qnaDialog = new QnADialog()
{
    MetadataFilter = new List<Metadata>()
    {
        new Metadata()
        {
            Name = "filter",
            Value = filterValue
        }
    }
};

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

上面的代码对我有用,如果使用fiddler抓取请求,我们可以发现请求体包含“strictFilters”: