c# - 将 QnAMaker 示例与 QnAMakerDialog 一起使用时出错
问题描述
我一直在尝试使用带有 QnAMaker API 的 Microsoft Cognitive 和 AI 工具包创建一个类的构造函数,以创建一个简单的聊天机器人。
虽然我的普通 qnaMakerAi 聊天机器人工作正常,但在我尝试增强它的功能并包含构造函数时出现了问题。在模拟器中运行代码时出现以下错误:
消息:对不起,我的机器人代码有问题。POST:500 directline.postActivity
我遇到的问题是:“对象引用未设置为对象的实例。”
请检查并提出建议。
根据用户评论,这里是 RootDialog 的代码 - `
命名空间 Microsoft.Bot.Sample.QnABot {
[Serializable]
public class RootDialog : QnAMakerDialog //IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
/* Wait until the first message is received from the conversation and call MessageReceviedAsync
* to process that message. */
context.Wait(this.MessageReceivedAsync);
}
public RootDialog() : base(new QnAMakerService(new QnAMakerAttribute(ConfigurationManager.AppSettings["QnASubscriptionKey"], ConfigurationManager.AppSettings["QnAKnowledgebaseId"], "Sorry, I couldn't find an answer for that")))
{
}
protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults result)
{
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
/* When MessageReceivedAsync is called, it's passed an IAwaitable<IMessageActivity>. To get the message,
* await the result. */
var message = await result;
// var qnaAuthKey = Utils.GetAppSetting("QnAAuthKey");
var qnaAuthKey = GetSetting("QnAAuthKey");
// var qnaKBId = Utils.GetAppSetting("QnAKnowledgebaseId");
// var endpointHostName = Utils.GetAppSetting("QnAEndpointHostName");
var qnaKBId = ConfigurationManager.AppSettings["QnAKnowledgebaseId"];
var endpointHostName = ConfigurationManager.AppSettings["QnAEndpointHostName"];
// QnA Subscription Key and KnowledgeBase Id null verification
if (!string.IsNullOrEmpty(qnaAuthKey) && !string.IsNullOrEmpty(qnaKBId))
{
// Forward to the appropriate Dialog based on whether the endpoint hostname is present
if (string.IsNullOrEmpty(endpointHostName))
await context.Forward(new BasicQnAMakerPreviewDialog(), AfterAnswerAsync, message, CancellationToken.None);
else
await context.Forward(new BasicQnAMakerDialog(), AfterAnswerAsync, message, CancellationToken.None);
}
else
{
await context.PostAsync("Please set QnAKnowledgebaseId, QnAAuthKey and QnAEndpointHostName (if applicable) in App Settings. Learn how to get them at https://aka.ms/qnaabssetup.");
}
}
private async Task AfterAnswerAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
// wait for the next user message
context.Wait(MessageReceivedAsync);
}
public static string GetSetting(string key)
{
var value = ConfigurationManager.AppSettings[key];
if (String.IsNullOrEmpty(value) && key == "QnAAuthKey")
{
value = ConfigurationManager.AppSettings["QnASubscriptionKey"]; // QnASubscriptionKey for backward compatibility with QnAMaker (Preview)
}
return value;
}
}
// Dialog for QnAMaker Preview service
[Serializable]
public class BasicQnAMakerPreviewDialog : QnAMakerDialog
{
// Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
// Parameters to QnAMakerService are:
// Required: subscriptionKey, knowledgebaseId,
// Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
public BasicQnAMakerPreviewDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), ConfigurationManager.AppSettings["QnAKnowledgebaseId"], "No good match in FAQ.", 0.5)))
{ }
}
// Dialog for QnAMaker GA service
[Serializable]
public class BasicQnAMakerDialog : QnAMakerDialog
{
// Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
// Parameters to QnAMakerService are:
// Required: qnaAuthKey, knowledgebaseId, endpointHostName
// Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
public BasicQnAMakerDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), ConfigurationManager.AppSettings["QnAKnowledgebaseId"], "No good match in FAQ.", 0.5, 1, ConfigurationManager.AppSettings["QnAEndpointHostName"])))
{ }
}}
`
对于 RootDialog -
解决方案
The issue I'm having is: "Object reference not set to an instance of an object."
Based on the code that you provided, I can reproduce the issue. It seems that you create Bot service with Question and Answer(c#) template and download the source code from Azure portal, and then you modify the code to make the RootDialog inherit QnAMakerDialog
class. As Eric Dahlvang mentioned in comment, if you check the definition of QnAMakerDialog
, you will find the implementation of your RootDialog is invalid.
Besides, based on my test, if you use QnAAuthKey and QnAKnowledgebaseId of GA QnAMaker service to initate your QnAMakerDialog, but not provide QnAEndpointHostName, the exception will be thorwn.
Note: For more information about GA QnAMaker service, you can refer to this blog.
推荐阅读
- java - 如何在 javafx 中调用 onAction(ActionEvent event) 方法
- angular - 错误类型错误:无法读取角度 6 中未定义的属性“modElasticity”
- java - redisson java客户端可以检测到集群拓扑中失败的主副本对的重启吗?
- android - 在 Android 中以编程方式添加 FilterChips
- symfony - 作曲家警告:版本约束没有上限
- python - 从 Django Rest 框架序列化器中查找模型名称
- mysql - 将忽略 NULL 值的 MySQL 中的行聚合为一行
- symfony - Liip想象捆绑不工作 - Symfony -lighttpd
- powershell - 使用 PowerShell 批量删除文件名开头的字符时出现问题
- windows - 当我为它提供 socket_listener 输入配置时,Telegraf 进程意外终止