c# - 如何根据每个不同的 luis 意图启动不同的对话?
问题描述
我正在尝试设置一个简单的聊天机器人,用户可以在其中说出不同的意图,并基于此会有不同的对话。目前我有 2 个可能的意图及其相应的对话框:“listBots”和“runBot”。
我设置我的机器人从 Luis 获取意图,然后使用 switch on intent 来确定它应该运行哪个对话框,这是我尝试执行此操作的代码:
public class MainChatbot : ActivityHandler
{
private readonly IOptions<Models.Configurations> _mySettings;
protected readonly IRecognizer _recognizer;
protected readonly BotState _conversationState;
public MainChatbot(ConversationState conversationState, IOptions<Models.Configurations> mySettings, ChatbotRecognizer recognizer)
{
_mySettings = mySettings ?? throw new ArgumentNullException(nameof(mySettings));
_recognizer = recognizer;
_conversationState = conversationState;
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var luisResult = await _recognizer.RecognizeAsync<Models.ChatbotIntent>(turnContext, cancellationToken);
Models.ChatbotIntent.Intent TopIntent = luisResult.TopIntent().intent;
await turnContext.SendActivityAsync(MessageFactory.Text($"Your Intention Is: {TopIntent.ToString()}"), cancellationToken);
switch (TopIntent)
{
case Models.ChatbotIntent.Intent.RunBot:
var RunBotOptions = new Models.RunBotOptions();
Dialog RunBotDialog = new RunBotDialog();
await RunBotDialog.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
break;
case Models.ChatbotIntent.Intent.ListBots:
Dialog ListBotDialog = new ListBotDialog();
await ListBotDialog.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
break;
default:
break;
}
return;
}
基本上在我的 OnMessageActivityAsync 中,它只是简单地调用 Luis 从用户输入中获取意图,然后打开意图,根据情况,它会创建一个不同的对话框并启动它。至少在理论上。
在我的 startup.cs 中,我依赖注入了所有机器人和对话框类。
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<Models.Configurations>(Configuration);
// Create the Bot Framework Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, Dialogs.MainChatbot>();
// Create the Conversation state. (Used by the Dialog system itself.)
var storage = new MemoryStorage();
var conversationState = new ConversationState(storage);
services.AddSingleton(conversationState);
// Register LUIS recognizer
services.AddSingleton<ChatbotRecognizer>();
// Dialogs
services.AddSingleton<Dialogs.RunBotDialog>();
services.AddSingleton<Dialogs.ListBotsDialog>();
}
这给了我一个 500 错误,所以我不知道出了什么问题。我正在使用机器人框架 v4。
解决方案
看来此代码按原样工作!不知道为什么它昨天没有工作。我会把它留给任何可能在未来寻找答案的人。
推荐阅读
- excel - 在 excel 中获取最大值以及 ID 和索引字段的问题
- android - RecyclerView.SmoothScroller 在第一次使用时不流畅
- javascript - UWP getHtmlPrintDocumentSourceAsync 问题(与 x-ms-webview 一起使用时)
- excel - 如何根据单元格的值运行代码?
- java - 什么元素控制 JPanel 和 JTabbedPane 选项卡之间的细条的颜色
- angularjs - safari 中的 md-autocomplete 输入高度问题
- python - 使用有时包含较少项目 (< x) 的 tolist() 函数填充固定数量 (x) 的新列
- javascript - 将表单转换为查询字符串,Vanilla JavaScript
- apache-kafka - 为什么 .index 和 .timeindex 文件占用 10MB 大小?
- python - 如何将带有出生年份的熊猫数据框列转换为年龄?(例如“1991”-> 28)