玩微软的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有任何好的想法吗?
答案 0 :(得分:7)
我对机器人框架并不是特别熟悉,但我可以评判受歧视的联盟 - 我们在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
它不像歧视的工会那么优雅,但它在技术上应该是可行的。
我认为另一种选择是将各个操作的处理程序作为单独的事件公开,然后你可以这样写:
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)
现在我考虑一下,这可能会更好,但这取决于机器人框架的典型用途。