3

玩了微软的 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 有什么好的想法吗?

4

1 回答 1

7

我对机器人框架不是特别熟悉,但我可以评论有区别的联合——我们在 F# 数据中面临类似的问题。

如果你有<One name="string" /><Two id="42" />,最好提供有区别的联合案例One of stringTwo 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)

现在我考虑一下,这可能会更好,但这取决于机器人框架的典型用途。

于 2016-08-19T10:47:19.983 回答