gpt4 book ai didi

dialogflow-es - 如何在 Dialogflow 实现中获取当前意图的名称?

转载 作者:行者123 更新时间:2023-12-03 16:46:58 25 4
gpt4 key购买 nike

我想在 fulfillment 中获取当前意图的名称,这样我就可以根据我所处的不同意图处理不同的响应。但是我找不到它的功能。

function getDateAndTime(agent) {    
date = agent.parameters.date;
time = agent.parameters.time;

// Is there any function like this to help me get current intent's name?
const intent = agent.getIntent();
}

// I have two intents are calling the same function getDateAndTime()
intentMap.set('Start Booking - get date and time', getDateAndTime);
intentMap.set('Start Cancelling - get date and time', getDateAndTime);

最佳答案

使用 intentMap 或为每个 Intent 创建一个 Intent Handler 并没有什么神奇或特别之处。 handleRequest() 函数所做的就是查看 action.intent 以获取 Intent 名称,从映射中获取具有该名称的处理程序,调用它,并可能处理它返回的 Promise。

但是如果您要违反约定,您应该有充分的理由这样做。每个 Intent 都有一个 Intent Handler 可以非常清楚每个匹配的 Intent 正在执行什么代码,这使您的代码更易于维护。

看起来您想要这样做的原因是因为两个处理程序之间存在大量重复代码。在您的示例中,这是获取 datetime 参数,但它也可以是更多的东西。

如果这是真的,那么做程序员几十年来一直在做的事情:将这些任务推送到一个可以从每个处理程序调用的函数。所以你的例子可能看起来像这样:

function getParameters( agent ){
return {
date: agent.parameters.date,
time: agent.parameters.time
}
}

function bookingHandler( agent ){
const {date, time} = getParameters( agent );
// Then do the stuff that uses the date and time to book the appointment
// and send an appropriate reply
}

function cancelHandler( agent ){
const {date, time} = getParameters( agent );
// Similarly, cancel things and reply as appropriate
}

intentMap.set( 'Start Booking', bookingHandler );
intentMap.set( 'Cancel Booking', cancelHandler );

关于dialogflow-es - 如何在 Dialogflow 实现中获取当前意图的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53655713/

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