gpt4 book ai didi

c# - 机器人框架 v4 : Stepcontext Option

转载 作者:行者123 更新时间:2023-12-02 16:59:26 26 4
gpt4 key购买 nike

您好,有人可以解释一下如何使用瀑布 stepcontext.Option 吗?我一直在示例中看到它,但我不太明白如何使用它。这是来自 this 的示例和 this .

我打算重构我的整个代码,如果可能的话我想使用这个选项。谢谢!

private static async Task<DialogTurnResult> TableStepAsync(
WaterfallStepContext step,
CancellationToken cancellationToken = default(CancellationToken))
{
string greeting = step.Options is GuestInfo guest
&& !string.IsNullOrWhiteSpace(guest?.Name)
? $"Welcome {guest.Name}" : "Welcome";

string prompt = $"{greeting}, How many diners will be at your table?";
string[] choices = new string[] { "1", "2", "3", "4", "5", "6" };
return await step.PromptAsync(
TablePrompt,
new PromptOptions
{
Prompt = MessageFactory.Text(prompt),
Choices = ChoiceFactory.ToChoices(choices),
},
cancellationToken);
}

    private async Task<DialogTurnResult> SelectionStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Continue using the same selection list, if any, from the previous iteration of this dialog.
List<string> list = stepContext.Options as List<string> ?? new List<string>();
stepContext.Values[CompaniesSelected] = list;

// Create a prompt message.
string message;
if (list.Count is 0)
{
message = $"Please choose a company to review, or `{DoneOption}` to finish.";
}
else
{
message = $"You have selected **{list[0]}**. You can review an additional company, " +
$"or choose `{DoneOption}` to finish.";
}

// Create the list of options to choose from.
List<string> options = _companyOptions.ToList();
options.Add(DoneOption);
if (list.Count > 0)
{
options.Remove(list[0]);
}

// Prompt the user for a choice.
return await stepContext.PromptAsync(
SelectionPrompt,
new PromptOptions
{
Prompt = MessageFactory.Text(message),
RetryPrompt = MessageFactory.Text("Please choose an option from the list."),
Choices = ChoiceFactory.ToChoices(options),
},
cancellationToken);
}

如果可能的话,我也想学习如何在这个例子中像这样传递和获取值

    private static async Task<DialogTurnResult> RoomStepAsync(
WaterfallStepContext step,
CancellationToken cancellationToken = default(CancellationToken))
{
// Save the name and prompt for the room number.
string name = step.Result as string;
((GuestInfo)step.Values[GuestKey]).Name = name;
return await step.PromptAsync(
TextPrompt,
new PromptOptions
{
Prompt = MessageFactory.Text($"Hi {name}. What room will you be staying in?"),
},
cancellationToken);
}

private static async Task<DialogTurnResult> FinalStepAsync(
WaterfallStepContext step,
CancellationToken cancellationToken = default(CancellationToken))
{
// Save the room number and "sign off".
string room = step.Result as string;
((GuestInfo)step.Values[GuestKey]).Room = room;

await step.Context.SendActivityAsync(
"Great, enjoy your stay!",
cancellationToken: cancellationToken);

// End the dialog, returning the guest info.
return await step.EndDialogAsync(
(GuestInfo)step.Values[GuestKey],
cancellationToken);
}

现在这就是我保存值的方式。

var userstate = await (stepContext.Context.TurnState["BasicAccessors"] as BasicAccessors).BasicUserStateAccessor.GetAsync(stepContext.Context);
userstate.Name = value;

最佳答案

你到底想做什么?stepContext.Options 是您可以在调用对话框时发送的对象,可以使用 BeginDialog 或 ReplaceDialog。例如:

await BeginDialogAsync(dialogId, sendobject, cancellationToken)

stepContext.Options 是您通过调用的对话框接收该对象的方式。

例如,在第一个文档中,主对话框调用每个子对话框并向它们发送 userInfo.Guest 对象:

return await stepContext.BeginDialogAsync(TableDialogId, userInfo.Guest, cancellationToken);

被调用的对话框正在接收它并将其转换为字符串作为验证:

string greeting = step.Options is GuestInfo guest
&& !string.IsNullOrWhiteSpace(guest?.Name)
? $"Welcome {guest.Name}" : "Welcome";

您可以删除验证,它看起来像这样,请记住,这仅在发送的对象 (userInfo.Guest) 不为 null 并且可以转换为字符串时才有效:

string greeting = (string)step.Options;

请记住:步骤上下文.选项;是一个对象,需要转换为正确的类型。如果您不添加 null/类型验证,转换可能会失败并且您的机器人可能会崩溃。这是框架的一项功能,但机器人工作不需要它,您可以使用其他方式通过方法或类发送对象。

关于c# - 机器人框架 v4 : Stepcontext Option,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54799332/

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