我一直在尝试使用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 -
答案 0 :(得分:1)
我遇到的问题是:&#34;对象引用未设置为对象的实例。&#34;
根据您提供的代码,我可以重现该问题。您似乎使用问题和答案(c#)模板创建Bot服务并从Azure门户下载源代码,然后修改代码以使RootDialog继承QnAMakerDialog
类。正如Eric Dahlvang在评论中提到的,如果你检查QnAMakerDialog
的定义,你会发现RootDialog的实现是无效的。
此外,根据我的测试,如果您使用 GA QnAMaker服务的 QnAKnowledgebaseId 来启动您的QnAMakerDialog,但不提供< strong> QnAEndpointHostName ,异常将是thorwn。
注意: 有关GA QnAMaker服务的详细信息,请参阅this blog。
答案 1 :(得分:0)
我无法理解你在哪里得到错误。
只是建议您也可以尝试这样做
using Microsoft.Bot.Builder.CognitiveServices.QnAMaker;
namespace Test.Qna
{
[Serializable]
[QnAMaker(authKey: "AuthKey", knowledgebaseId: "KnowledgebaseId", defaultMessage: "please rephrase, I could not understand.", scoreThreshold: 0.5, top: 1, endpointHostName: "https://yourAccount.azurewebsites.net/qnamaker")]
public class QnADialog : QnAMakerDialog
{
}
}
答案 2 :(得分:0)
如果要在方法之外创建任何对象,请尝试在方法内部创建对象。例如
class1 myclass1 = new class1();
public string void MyMethod()
{
}
//above line might be an error.
//do it as
public string void MyMethod()
{
class1 myclass1 = new class1();
}