玩了微软的 Luis + bot 框架后,我的“这将是一个很好的类型提供者”的感觉开始刺痛。不幸的是,类型提供者不能输出有区别的联合。我希望做类似以下的事情,但这是不可能的:
type Luis = LuisProvider<@"LuisId",@"LuisPasskey">
let IntentMatcher Intent =
match intent with
| Luis.Intents.Greeting -> GreetingHandler()
| Luis.Intents.SetAlarm title startDate startTime -> AlarmHandler title startDate startTime
| _ -> CouldNotUnderstand()
Luis 意图和它们的参数都可以通过 APIs 获得,这使得它们成为 typeProviderization 的绝佳候选者
这里的参考是来自示例 C# bot 的处理程序(我认为它可能更干净,并且在 F# 中更安全):
public const string Entity_Alarm_Title = "builtin.alarm.title";
public const string Entity_Alarm_Start_Time = "builtin.alarm.start_time";
public const string Entity_Alarm_Start_Date = "builtin.alarm.start_date";
public const string DefaultAlarmWhat = "default";
[LuisIntent("builtin.intent.alarm.set_alarm")]
public async Task SetAlarm(IDialogContext context, LuisResult result)
{
EntityRecommendation title;
if (!result.TryFindEntity(Entity_Alarm_Title, out title))
{
title = new EntityRecommendation(type: Entity_Alarm_Title) { Entity = DefaultAlarmWhat };
}
EntityRecommendation date;
if (!result.TryFindEntity(Entity_Alarm_Start_Date, out date))
{
date = new EntityRecommendation(type: Entity_Alarm_Start_Date) { Entity = string.Empty };
}
EntityRecommendation time;
if (!result.TryFindEntity(Entity_Alarm_Start_Time, out time))
{
time = new EntityRecommendation(type: Entity_Alarm_Start_Time) { Entity = string.Empty };
}
var parser = new Chronic.Parser();
var span = parser.Parse(date.Entity + " " + time.Entity);
if (span != null)
{
var when = span.Start ?? span.End;
var alarm = new Alarm() { What = title.Entity, When = when.Value };
this.alarmByWhat[alarm.What] = alarm;
string reply = $"alarm {alarm} created";
await context.PostAsync(reply);
}
else
{
await context.PostAsync("could not find time for alarm");
}
context.Wait(MessageReceived);
}
无论如何,问题是:有更多经验构建类型提供程序的人对我如何构建一个实际上可行的可读 dsl 有什么好的想法吗?