首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何关闭/重置会话

如何关闭/重置会话
EN

Stack Overflow用户
提问于 2021-07-14 16:37:48
回答 1查看 1.7K关注 0票数 1

如何以编程方式关闭或重置Java应用程序中的会话?根据Dialogflow CX文档,“会话保持活动,其数据在为会话发送最后请求后存储30分钟。”

我想让会话保持较短的时间。例如,如果我希望会话活动5分钟,当用户在最后一条消息之后发送消息5分钟或更多时,必须重新开始会话,必须关闭以前的流,并且必须删除上下文参数。

对于对话框流ES,使用ContextsClient是可行的,但是新版本不提供ContextsClient类。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-15 14:29:40

对话框流CX使用状态处理程序控制会话路径,与使用语境的对话框流不同。

对于对话框流CX,可以使用END_SESSION 符号过渡目标结束当前会话。一旦调用了END_SESSION转换目标,它就会清除当前会话,下一个用户输入将在起始页 of 默认启动流重新启动会话。

要实现您想要的用例,您必须为它创建自己的实现。请注意,下面的解决方案只有在您将您的Dialogflow CX代理集成到自定义前端时才能工作。

首先,您应该向所有的事件处理程序中添加一个页面,这样在会话流的任何部分都可以访问事件处理程序。在此事件处理程序中,定义自定义事件-例如:clearsession.然后,将其转换为结束会话页面。一旦调用了clearsession事件,它将结束当前会话。

然后,使用您自己的业务逻辑,您可以创建一个自定义函数,它可以作为每个用户查询的定时器。一旦计时器到达5分钟,您的自定义应用程序应该以编程方式向您的CX代理发送一个detectIntent请求。此detectIntent请求必须包含当前会话ID和自定义事件(来自先前创建的事件处理程序)。

下面是一个使用detectIntent调用自定义事件的示例Java客户端库请求

代码语言:javascript
运行
复制
// [START dialogflow_cx_detect_intent_event]
 
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.cx.v3.*;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.List;
import java.util.Map;
 
public class DetectIntent {
 
    // DialogFlow API Detect Intent sample with event input.
    public static Map<String, QueryResult> detectIntentEvent(
            String projectId,
            String locationId,
            String agentId,
            String sessionId,
            String languageCode,
            String event)
            throws IOException, ApiException {
        SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
        if (locationId.equals("global")) {
            sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
        } else {
            sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
        }
        SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();
 
        Map<String, QueryResult> queryResults = Maps.newHashMap();
        // Instantiates a client
        try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
            // Set the session name using the projectID (my-project-id), locationID (global), agentID
            // (UUID), and sessionId (UUID).
            SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);
            System.out.println("Session Path: " + session.toString());
 
            EventInput.Builder eventInput = EventInput.newBuilder().setEvent(event);
 
            // Build the query with the EventInput and language code (en-US).
            QueryInput queryInput =
                    QueryInput.newBuilder().setEvent(eventInput).setLanguageCode(languageCode).build();
 
            // Build the DetectIntentRequest with the SessionName and QueryInput.
            DetectIntentRequest request =
                    DetectIntentRequest.newBuilder()
                            .setSession(session.toString())
                            .setQueryInput(queryInput)
                            .build();
 
            // Performs the detect intent request.
            DetectIntentResponse response = sessionsClient.detectIntent(request);
 
            // Display the query result.
            QueryResult queryResult = response.getQueryResult();
 
            System.out.println("====================");
            System.out.format(
                    "Detected Intent: %s (confidence: %f)\n",
                    queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
 
 
        }
        return queryResults;
    }
 
    public static void main(String[] args) {
        String projectId = "<project-id>";
        String locationId = "<location-id>";
        String agentId = "<agent-id>";
        String sessionId = "<current-session-id>";
        String languageCode = "<language-code>";
        String event = "clearsession";
        try{
            detectIntentEvent(projectId,locationId,agentId,sessionId, languageCode, event);
        } catch (IOException e){
            System.out.println(e.getMessage());
        }
    }
}
// [END dialogflow_cx_detect_intent_event]
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68381987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档