gpt4 book ai didi

c# - 使用 C# 在一个机器人中使用多个 QnA 服务

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:10 25 4
gpt4 key购买 nike

我有 3 QnA 服务。我希望它们同时在单个 BOT 中使用。这如何使用 C# 实现。我最初的想法是将 KB ID 和 Sub Key 放入一个数组中(如何实现或者数组可以工作?)。我在 Node.JS 中看到了一些代码,但我不知道如何在 C# 中转换代码.

public class QnaDialog : QnAMakerDialog
{
public QnaDialog() : base(
new QnAMakerService(new QnAMakerAttribute(ConfigurationManager.AppSettings["QnaSubscriptionKey1"],
ConfigurationManager.AppSettings["QnaKnowledgebaseId1"], "Hmm, I wasn't able to find an article about that. Can you try asking in a different way?", 0.5)),

new QnAMakerService(new QnAMakerAttribute(ConfigurationManager.AppSettings["QnaSubscriptionKey2"],
ConfigurationManager.AppSettings["QnaKnowledgebaseId2"], "Hmm, I wasn't able to find an article about that. Can you try asking in a different way?", 0.5)),

new QnAMakerService(new QnAMakerAttribute(ConfigurationManager.AppSettings["QnaSubscriptionKey3"],
ConfigurationManager.AppSettings["QnaKnowledgebaseId4"], "Hmm, I wasn't able to find an article about that. Can you try asking in a different way?", 0.5))
)
{
}
}

最佳答案

您可以使用多个 QnAMaker通过在属性中提供多种服务,在单个机器人中构建知识库。

使用 QnAMakerDialog 的基本实现来自 Nuget 包 BotBuilder.CognitiveServices会是:

[Serializable]
[QnAMaker("QnaSubscriptionKey1", "QnaKnowledgebaseId1", "Hmm, I wasn't able to find an article about that. Can you try asking in a different way?", 0.50, 3)]
[QnAMaker("QnaSubscriptionKey2", "QnaKnowledgebaseId2", "Hmm, I wasn't able to find an article about that. Can you try asking in a different way?", 0.5, 3)]
[QnAMaker("QnaSubscriptionKey3", "QnaKnowledgebaseId3", "Hmm, I wasn't able to find an article about that. Can you try asking in a different way?", 0.5, 3)]
public class RootDialog : QnAMakerDialog
{
}

BUT(是的,有一个“但是”)在某些情况下,您可能会在处理消息的过程中遇到异常。作为QnAMakerDialog是开源的(来源是 here )您可以很容易地发现问题出在服务调用返回的实现中,在 MessageReceivedAsync 中:

var sendDefaultMessageAndWait = true;
qnaMakerResults = tasks.FirstOrDefault(x => x.Result.ServiceCfg != null)?.Result;
if (tasks.Count(x => x.Result.Answers?.Count > 0) > 0)
{
var maxValue = tasks.Max(x => x.Result.Answers[0].Score);
qnaMakerResults = tasks.First(x => x.Result.Answers[0].Score == maxValue).Result;

if (qnaMakerResults != null && qnaMakerResults.Answers != null && qnaMakerResults.Answers.Count > 0)
{
if (this.IsConfidentAnswer(qnaMakerResults))
{
await this.RespondFromQnAMakerResultAsync(context, message, qnaMakerResults);
await this.DefaultWaitNextMessageAsync(context, message, qnaMakerResults);
}
else
{
feedbackRecord = new FeedbackRecord { UserId = message.From.Id, UserQuestion = message.Text };
await this.QnAFeedbackStepAsync(context, qnaMakerResults);
}

sendDefaultMessageAndWait = false;
}
}

if (sendDefaultMessageAndWait)
{
await context.PostAsync(qnaMakerResults.ServiceCfg.DefaultMessage);
await this.DefaultWaitNextMessageAsync(context, message, qnaMakerResults);
}

在此代码中,如果并非您的所有服务都可以回答您的问题(即:如果您的至少一个 QnAMaker 知识库没有回答您的问题),这行代码将会中断

tasks.Max(x => x.Result.Answers[0].Score);

解决方法:您可以通过获取源代码并修复方法来实现自己的 QnAMakerDialog,例如:

public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;

if (message != null && !string.IsNullOrEmpty(message.Text))
{
var tasks = this.services.Select(s => s.QueryServiceAsync(message.Text)).ToArray();
await Task.WhenAll(tasks);

if (tasks.Any())
{
var sendDefaultMessageAndWait = true;
qnaMakerResults = tasks.FirstOrDefault(x => x.Result.ServiceCfg != null)?.Result;

var qnaMakerFoundResults = tasks.Where(x => x.Result.Answers.Any()).ToList();
if (qnaMakerFoundResults.Any())
{
var maxValue = qnaMakerFoundResults.Max(x => x.Result.Answers[0].Score);
qnaMakerResults = qnaMakerFoundResults.First(x => x.Result.Answers[0].Score == maxValue).Result;

if (qnaMakerResults?.Answers != null && qnaMakerResults.Answers.Count > 0)
{
if (this.IsConfidentAnswer(qnaMakerResults))
{
await this.RespondFromQnAMakerResultAsync(context, message, qnaMakerResults);
await this.DefaultWaitNextMessageAsync(context, message, qnaMakerResults);
}
else
{
feedbackRecord = new FeedbackRecord { UserId = message.From.Id, UserQuestion = message.Text };
await this.QnAFeedbackStepAsync(context, qnaMakerResults);
}

sendDefaultMessageAndWait = false;
}
}

if (sendDefaultMessageAndWait)
{
await context.PostAsync(qnaMakerResults.ServiceCfg.DefaultMessage);
await this.DefaultWaitNextMessageAsync(context, message, qnaMakerResults);
}
}
}
}

关于c# - 使用 C# 在一个机器人中使用多个 QnA 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50132206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com