应该如何在机器人框架版本 4 中编写版本 3 机器人代码中的 LUIS 意图方法?
How should the LUIS intent methods in version 3 bot code be written in bot framework version 4?
我正在尝试按照 Microsoft Docs 中的 this 文章将我们的版本 3 代码迁移到版本 4。
但是,我不确定如何重写 Luis 对话框。必须做什么?
我在onturnasync中添加了下面的代码,现在不知道如何重写AfterFAQ resume方法。
请帮助我重写这些现有的 Luis 方法:
//The LUIS dialog service call the back the method if the conversation is part of Greeting intent
[LuisIntent("Greetings")]
public async Task Greetings(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
needMoreInformation = false;
qnaInvalidMessageCount = 0;
var messageToForward = await activity;
string[] supportList = { "HELP", "FEEDBACK", "SUPPORT", "ESCALATE", "AGENT" };
string qnaAnswer;
if (messageToForward.Text == null || supportList.Any(x => x == messageToForward.Text.ToUpper()))
{
await context.PostAsync("Please reach out to ...");
context.Wait(MessageReceived);
}
else if (GreetingColl.TryGetValue(messageToForward.Text.Trim().ToLower(), out qnaAnswer))
{
await context.PostAsync(qnaAnswer);
context.Wait(MessageReceived);
}
else
{
await context.Forward(new QnAGreetingsDialog(), AfterFAQDialog, messageToForward, CancellationToken.None);
}
}
修改后的代码:
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
...
var luisResults = await botServices.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);
var topScoringIntent = luisResults?.GetTopScoringIntent();
var topIntent = topScoringIntent.Value.intent;
// Continue the current dialog
var dialogResult = await dc.ContinueDialogAsync();
// if no one has responded,
if (!dc.Context.Responded)
{
// examine results from active dialog
switch (dialogResult.Status)
{
case DialogTurnStatus.Empty:
switch (topIntent)
{
case NoneIntent:
case GreetingsIntent:
await dc.BeginDialogAsync(nameof(QnAGreetingsDialog));
break;
case CredentialsIntent:
case ContactusIntent:
await LuisVar.Feedback(turnContext);
break;
case FeedbackIntent:
await LuisVar.Feedback(turnContext);
break;
default:
// No intent identified, provide some help to the user
await dc.Context.SendActivityAsync("I didn't understand what you just said to me.");
break;
}
break;
case DialogTurnStatus.Waiting:
// The active dialog is waiting for a response from the user, so do nothing.
break;
case DialogTurnStatus.Complete:
await dc.EndDialogAsync();
break;
default:
await dc.CancelAllDialogsAsync();
break;
}
}
}
}
如果您的问题是关于 Bot Framework core v4,PFB 获取意图的步骤:
- 首先您需要在 bot 框架中使用密钥的服务中注入 LUIS 服务。
- 使用以下代码获取识别器结果对象
var luisResults = await services.LuisServices[LuisKey].RecognizeAsync(turnContext, default(CancellationToken));
LUIS 密钥是注入 LUIS 服务时使用的密钥。
- 这是使用 RecognizerResult 对象获取意图的方法。
luisResults.GetTopIntent(luisThresholdScore).intent;
我正在尝试按照 Microsoft Docs 中的 this 文章将我们的版本 3 代码迁移到版本 4。
但是,我不确定如何重写 Luis 对话框。必须做什么?
我在onturnasync中添加了下面的代码,现在不知道如何重写AfterFAQ resume方法。
请帮助我重写这些现有的 Luis 方法:
//The LUIS dialog service call the back the method if the conversation is part of Greeting intent
[LuisIntent("Greetings")]
public async Task Greetings(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
needMoreInformation = false;
qnaInvalidMessageCount = 0;
var messageToForward = await activity;
string[] supportList = { "HELP", "FEEDBACK", "SUPPORT", "ESCALATE", "AGENT" };
string qnaAnswer;
if (messageToForward.Text == null || supportList.Any(x => x == messageToForward.Text.ToUpper()))
{
await context.PostAsync("Please reach out to ...");
context.Wait(MessageReceived);
}
else if (GreetingColl.TryGetValue(messageToForward.Text.Trim().ToLower(), out qnaAnswer))
{
await context.PostAsync(qnaAnswer);
context.Wait(MessageReceived);
}
else
{
await context.Forward(new QnAGreetingsDialog(), AfterFAQDialog, messageToForward, CancellationToken.None);
}
}
修改后的代码:
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
...
var luisResults = await botServices.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);
var topScoringIntent = luisResults?.GetTopScoringIntent();
var topIntent = topScoringIntent.Value.intent;
// Continue the current dialog
var dialogResult = await dc.ContinueDialogAsync();
// if no one has responded,
if (!dc.Context.Responded)
{
// examine results from active dialog
switch (dialogResult.Status)
{
case DialogTurnStatus.Empty:
switch (topIntent)
{
case NoneIntent:
case GreetingsIntent:
await dc.BeginDialogAsync(nameof(QnAGreetingsDialog));
break;
case CredentialsIntent:
case ContactusIntent:
await LuisVar.Feedback(turnContext);
break;
case FeedbackIntent:
await LuisVar.Feedback(turnContext);
break;
default:
// No intent identified, provide some help to the user
await dc.Context.SendActivityAsync("I didn't understand what you just said to me.");
break;
}
break;
case DialogTurnStatus.Waiting:
// The active dialog is waiting for a response from the user, so do nothing.
break;
case DialogTurnStatus.Complete:
await dc.EndDialogAsync();
break;
default:
await dc.CancelAllDialogsAsync();
break;
}
}
}
}
如果您的问题是关于 Bot Framework core v4,PFB 获取意图的步骤:
- 首先您需要在 bot 框架中使用密钥的服务中注入 LUIS 服务。
- 使用以下代码获取识别器结果对象
var luisResults = await services.LuisServices[LuisKey].RecognizeAsync(turnContext, default(CancellationToken));
LUIS 密钥是注入 LUIS 服务时使用的密钥。
- 这是使用 RecognizerResult 对象获取意图的方法。
luisResults.GetTopIntent(luisThresholdScore).intent;