gpt4 book ai didi

c# - 如何留在机器人对话框中(机器人框架,C#)

转载 作者:行者123 更新时间:2023-12-02 23:38:02 24 4
gpt4 key购买 nike

我目前计划在我的 C# 机器人中使用对话框。我已经设计了一个完整的对话框并将其实现到我当前的解决方案中,但当我测试它时,我只能触发它的第一部分。

我的机器人配置如下:

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
string[] PostCodeDialogStartTrigger = new string[] { "trigger1", "trigger2", "trigger3" };
if (PostCodeDialogStartTrigger.Contains(turnContext.Activity.Text) /* Maybe || ConversationState != null */)
{
await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
}
else
{
// First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use.
var recognizerResult = await BotServices.Dispatch.RecognizeAsync(turnContext, cancellationToken);

// Top intent tell us which cognitive service to use.
var topIntent = recognizerResult.GetTopScoringIntent();

// Next, we call the dispatcher with the top intent.
await DispatchToTopIntentAsync(turnContext, topIntent.intent, recognizerResult, cancellationToken);
}
}

我在 PostCodeDialogStartTrigger 中有一组字符串。我希望当用户消息与触发器之一匹配时启动对话框。否则,我的常规流程应该开始,其中包括用于一维对话的 LUIS 和 QnA Maker。问题是我不能留在对话框中,因为显然下一条消息不会再次触发对话框。

有没有办法检查 ConversationState 或 UserState,如果已进行,对话框是否会继续?

最佳答案

您想要的称为中断:https://learn.microsoft.com/azure/bot-service/bot-builder-howto-handle-user-interrupt

核心机器人示例包括通过创建基础 CancelAndHelpDialog 来处理中断的有点复杂的方法。使任何对话框“可中断”的类,但有一种更简单的方法可以做到这一点。关键是默认情况下每个回合都会调用 ContinueDialogAsync,并且仅当该回合发生中断时才不会调用它。您可以将每个回合视为具有三种情况:

  1. 发生中断
  2. 没有中断,但有一个事件对话框
  3. 没有中断,也没有事件对话框

逻辑可能如下:

string[] PostCodeDialogStartTrigger = new string[] { "trigger1", "trigger2", "trigger3" };

if (PostCodeDialogStartTrigger.Contains(turnContext.Activity.Text))
{
// Case 1: Handle interruptions by starting a dialog
await dialogContext.BeginDialogAsync(DIALOG_ID);
}
else
{
// Case 2: Try to continue the dialog in case there is one
var result = await dialogContext.ContinueDialogAsync();

if (result.Status == DialogTurnStatus.Empty)
{
// Case 3: If there aren't any dialogs on the stack

// First, we use the dispatch model to determine
// which cognitive service (LUIS or QnA) to use.
// . . .
}
}

您可能会注意到我在这里没有使用 RunAsyncRunAsync 最适合与机器人的“主对话框”配合使用,该“主对话框”意味着始终位于堆栈上,如果您确实没有主对话框,则它的工作效果就不太好。 RunAsync 的最初目的是将几个方法调用合并为一个,因为对于机器人来说,调用ContinueDialogAsync 然后检查结果然后启动是一种常见的模式。如果对话框堆栈为空,则显示主对话框。您想要执行类似的操作,但您只想调用调度,而不是在对话框堆栈为空的情况下开始主对话框。

如果您好奇,这里还有一些其他 Stack Overflow 帖子,您可以在其中阅读有关中断的信息:

我应该提到你正在尝试以一种过时的方式做事。您可能已经注意到 Dispatch 已被 Orchestrator 取代,所以您确实应该使用 Orchestrator。此外,Composer 使诸如中断之类的事情变得容易。 ,无论如何,建议大家现在就使用 Composer。

关于c# - 如何留在机器人对话框中(机器人框架,C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67728877/

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