如何根据每个不同的 luis 意图启动不同的对话?
How can I start a different dialog based on each different luis intent?
我正在尝试设置一个简单的聊天机器人,用户可以在其中说出不同的意图,并在此基础上进行不同的对话。目前我有 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 错误,所以我不知道出了什么问题。我正在使用 bot-framework v4.
这段代码似乎可以正常工作!不知道为什么它昨天不起作用。我会把它留给将来可能正在寻找答案的人。
我正在尝试设置一个简单的聊天机器人,用户可以在其中说出不同的意图,并在此基础上进行不同的对话。目前我有 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 错误,所以我不知道出了什么问题。我正在使用 bot-framework v4.
这段代码似乎可以正常工作!不知道为什么它昨天不起作用。我会把它留给将来可能正在寻找答案的人。