为 Microsoft 的 Luis 编写 F# Type Provider 的好方法是什么?
What's a good way to write a F# Type Provider for Microsoft's Luis?
玩了玩微软的 Luis + bot 框架,我的 "this would make a good type provider" 感觉开始发麻。不幸的是,类型提供者不能输出有区别的联合。我希望做类似下面的事情,但这是不可能的:
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# 机器人的处理程序供参考(我认为它在 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 有什么好的想法?
我不是特别熟悉 bot 框架,但我可以评论受歧视的联合 - 我们在 F# 数据中面临类似的问题。
如果您有 <One name="string" /><Two id="42" />
,最好提供带有案例 One of string
和 Two of int
的可区分联合。我们做的是提供一个类型:
type OneOrTwo =
member One : option<string>
member Two : option<int>
您可以按照相同的模式公开 API,看起来像这样:
type Luis = LuisProvider<"LuisId", "LuisPasskey">
let intentMatcher (intent:Luis.Intents) =
match intent.Greetings, intent.SetAlarm with
| Some(), _ -> greetingHandler()
| _, Some(title, startDate, startTime) -> alarmHandler title startDate startTime
| _ -> couldNotUnderstand()
Luis.Connect().OnIntent
|> Observable.subscribe intentMatcher
虽然不如discriminated union那么优雅,但技术上应该是可行的。
我想另一种选择是将各个操作的处理程序公开为单独的事件,然后您可以这样写:
type Luis = LuisProvider<"LuisId", "LuisPasskey">
let luis = Luis.Connect()
luis.BuiltIn.Greetings
|> Observable.add greetingHandler
luis.BuiltIn.SetAlarm
|> Observable.add (fun (title, startDate, startTime) ->
alarmHandler title startDate startTime)
现在想想,这可能会更好,但这取决于 bot 框架的典型用途。
玩了玩微软的 Luis + bot 框架,我的 "this would make a good type provider" 感觉开始发麻。不幸的是,类型提供者不能输出有区别的联合。我希望做类似下面的事情,但这是不可能的:
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# 机器人的处理程序供参考(我认为它在 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 有什么好的想法?
我不是特别熟悉 bot 框架,但我可以评论受歧视的联合 - 我们在 F# 数据中面临类似的问题。
如果您有 <One name="string" /><Two id="42" />
,最好提供带有案例 One of string
和 Two of int
的可区分联合。我们做的是提供一个类型:
type OneOrTwo =
member One : option<string>
member Two : option<int>
您可以按照相同的模式公开 API,看起来像这样:
type Luis = LuisProvider<"LuisId", "LuisPasskey">
let intentMatcher (intent:Luis.Intents) =
match intent.Greetings, intent.SetAlarm with
| Some(), _ -> greetingHandler()
| _, Some(title, startDate, startTime) -> alarmHandler title startDate startTime
| _ -> couldNotUnderstand()
Luis.Connect().OnIntent
|> Observable.subscribe intentMatcher
虽然不如discriminated union那么优雅,但技术上应该是可行的。
我想另一种选择是将各个操作的处理程序公开为单独的事件,然后您可以这样写:
type Luis = LuisProvider<"LuisId", "LuisPasskey">
let luis = Luis.Connect()
luis.BuiltIn.Greetings
|> Observable.add greetingHandler
luis.BuiltIn.SetAlarm
|> Observable.add (fun (title, startDate, startTime) ->
alarmHandler title startDate startTime)
现在想想,这可能会更好,但这取决于 bot 框架的典型用途。