如何从 Java 应用程序以编程方式关闭或重置对话?根据 Dialogflow CX 文档,“会话保持活动状态,并且在为会话发送最后一个请求后,其数据将存储 30 分钟。 ”
我想让会话保持活跃的时间更短。例如,如果我希望会话保持活动状态 5 分钟,则当用户在上一条消息后 5 分钟或更长时间发送消息时,必须重新开始对话,并且必须关闭之前的流程,并且必须删除上下文参数。
对于 Dialogflow ES,可以使用 ContextsClient,但是新版本不提供 ContextsClient 类。
小智 7
Dialogflow CX 使用状态处理程序来控制对话路径,这与使用Contexts 的Dialogflow ES 不同。
\n对于 Dialogflow CX,您可以使用END_SESSION 符号转换目标结束当前会话。一旦调用 END_SESSION 转换目标,它就会清除当前会话,下一个用户输入将在Default Start Flow的起始页面重新启动会话。
\n为了实现您想要的用例,您\xe2\x80\x99必须为其创建自己的实现。请注意,仅当您将 Dialogflow CX 代理集成到自定义前端时,以下解决方案才有效。
\n首先,您应该向所有页面添加一个事件处理程序- 以便在对话流的任何部分都可以访问该事件处理程序。在此事件处理程序中,定义自定义事件 - 例如:clearsession。然后,将其 Transition 设置为End Session Page。一旦调用clearsession事件,它将结束当前会话。
\n\n然后,使用您自己的业务逻辑,您可以创建一个自定义函数,该函数可以充当每个用户查询的计时器。一旦计时器达到 5 分钟,您的自定义应用程序应以编程方式向您的 CX 代理发送一个detectorIntent 请求。此 detectorIntent 请求必须包含当前会话 ID 和自定义事件(来自先前创建的事件处理程序)。
\n这里\xe2\x80\x99是一个使用Java客户端库调用自定义事件的示例DetectIntent请求:
\n// [START dialogflow_cx_detect_intent_event]\n \nimport com.google.api.gax.rpc.ApiException;\nimport com.google.cloud.dialogflow.cx.v3.*;\nimport com.google.common.collect.Maps;\nimport java.io.IOException;\nimport java.util.List;\nimport java.util.Map;\n \npublic class DetectIntent {\n \n // DialogFlow API Detect Intent sample with event input.\n public static Map<String, QueryResult> detectIntentEvent(\n String projectId,\n String locationId,\n String agentId,\n String sessionId,\n String languageCode,\n String event)\n throws IOException, ApiException {\n SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();\n if (locationId.equals("global")) {\n sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");\n } else {\n sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");\n }\n SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();\n \n Map<String, QueryResult> queryResults = Maps.newHashMap();\n // Instantiates a client\n try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {\n // Set the session name using the projectID (my-project-id), locationID (global), agentID\n // (UUID), and sessionId (UUID).\n SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);\n System.out.println("Session Path: " + session.toString());\n \n EventInput.Builder eventInput = EventInput.newBuilder().setEvent(event);\n \n // Build the query with the EventInput and language code (en-US).\n QueryInput queryInput =\n QueryInput.newBuilder().setEvent(eventInput).setLanguageCode(languageCode).build();\n \n // Build the DetectIntentRequest with the SessionName and QueryInput.\n DetectIntentRequest request =\n DetectIntentRequest.newBuilder()\n .setSession(session.toString())\n .setQueryInput(queryInput)\n .build();\n \n // Performs the detect intent request.\n DetectIntentResponse response = sessionsClient.detectIntent(request);\n \n // Display the query result.\n QueryResult queryResult = response.getQueryResult();\n \n System.out.println("====================");\n System.out.format(\n "Detected Intent: %s (confidence: %f)\\n",\n queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());\n \n \n }\n return queryResults;\n }\n \n public static void main(String[] args) {\n String projectId = "<project-id>";\n String locationId = "<location-id>";\n String agentId = "<agent-id>";\n String sessionId = "<current-session-id>";\n String languageCode = "<language-code>";\n String event = "clearsession";\n try{\n detectIntentEvent(projectId,locationId,agentId,sessionId, languageCode, event);\n } catch (IOException e){\n System.out.println(e.getMessage());\n }\n }\n}\n// [END dialogflow_cx_detect_intent_event]\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
3832 次 |
最近记录: |