gpt4 book ai didi

f# - 为 Microsoft 的 Luis 编写 F# Type Provider 的好方法是什么?

转载 作者:行者123 更新时间:2023-12-04 23:48:11 25 4
gpt4 key购买 nike

尝试使用 Microsoft 的 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# 机器人的处理程序供引用(我认为它在 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 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)

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

关于f# - 为 Microsoft 的 Luis 编写 F# Type Provider 的好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com