本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Java 应用程序与 Amazon Lex V2 机器人进行交互
AWS SDK for Java 2.0
以下应用程序与您在练习 1:根据示例创建机器人中创建的 OrderFlowers 机器人进行交互。使用 SDK for Java 的 LexRuntimeV2Client
来调用 RecognizeText 操作执行与机器人的对话。
该对话的输出如下所示:
User : I would like to order flowers
Bot : What type of flowers would you like to order?
User : 1 dozen roses
Bot : What day do you want the dozen roses to be picked up?
User : Next Monday
Bot : At what time do you want the dozen roses to be picked up?
User : 5 in the evening
Bot : Okay, your dozen roses will be ready for pickup by 17:00 on 2021-01-04. Does this sound okay?
User : Yes
Bot : Thanks.
有关在客户端应用程序与 Amazon Lex V2 机器人之间发送的 JSON 结构,请参阅练习 2:查看对话流程。
要运行应用程序,您需要提供以下信息:
-
“botId”:您在创建机器人时为该机器人指定的标识符。您可以在 Amazon Lex V2 控制台的机器人设置页面上查看该机器人 ID。
-
“botAliasId”:您在创建机器人别名时为该机器人别名指定的标识符。您可以在 Amazon Lex V2 控制台的别名页面上查看该机器人别名 ID。如果列表中未显示别名 ID,请选择右上角的齿轮图标并打开别名 ID。
-
“localeId”:您用于机器人的区域设置的标识符。有关区域设置列表,请参阅Amazon Lex V2 支持的语言和区域设置。
-
“accessKey”和“secretKey”:您账户的身份验证密钥。如果没有密钥集,请使用 AWS Identity and Access Management 控制台创建密钥。
-
“sessionId”:与 Amazon Lex V2 机器人会话的标识符。在这种情况下,代码使用随机的 UUID。
-
“region”:如果您的机器人不在美国东部(弗吉尼亚州北部)区域,请务必更改“区域”设置。
应用程序使用名为 getRecognizeTextRequest
的函数创建对机器人的单独请求。该函数使用必要参数生成一个请求以发送到 Amazon Lex V2。
package com.lex.recognizetext.sample; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.lexruntimev2.LexRuntimeV2Client; import software.amazon.awssdk.services.lexruntimev2.model.RecognizeTextRequest; import software.amazon.awssdk.services.lexruntimev2.model.RecognizeTextResponse; import java.net.URISyntaxException; import java.util.UUID; /** * This is a sample application to interact with a bot using RecognizeText API. */ public class OrderFlowersSampleApplication { public static void main(String[] args) throws URISyntaxException, InterruptedException { String botId = ""; String botAliasId = ""; String localeId = "en_US"; String accessKey = ""; String secretKey = ""; String sessionId = UUID.randomUUID().toString(); Region region = Region.US_EAST_1; // pick an appropriate region AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accessKey, secretKey); AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider.create(awsCreds); LexRuntimeV2Client lexV2Client = LexRuntimeV2Client .builder() .credentialsProvider(awsCredentialsProvider) .region(region) .build(); // utterance 1 String userInput = "I would like to order flowers"; RecognizeTextRequest recognizeTextRequest = getRecognizeTextRequest(botId, botAliasId, localeId, sessionId, userInput); RecognizeTextResponse recognizeTextResponse = lexV2Client.recognizeText(recognizeTextRequest); System.out.println("User : " + userInput); recognizeTextResponse.messages().forEach(message -> { System.out.println("Bot : " + message.content()); }); // utterance 2 userInput = "1 dozen roses"; recognizeTextRequest = getRecognizeTextRequest(botId, botAliasId, localeId, sessionId, userInput); recognizeTextResponse = lexV2Client.recognizeText(recognizeTextRequest); System.out.println("User : " + userInput); recognizeTextResponse.messages().forEach(message -> { System.out.println("Bot : " + message.content()); }); // utterance 3 userInput = "next monday"; recognizeTextRequest = getRecognizeTextRequest(botId, botAliasId, localeId, sessionId, userInput); recognizeTextResponse = lexV2Client.recognizeText(recognizeTextRequest); System.out.println("User : " + userInput); recognizeTextResponse.messages().forEach(message -> { System.out.println("Bot : " + message.content()); }); // utterance 4 userInput = "5 in evening"; recognizeTextRequest = getRecognizeTextRequest(botId, botAliasId, localeId, sessionId, userInput); recognizeTextResponse = lexV2Client.recognizeText(recognizeTextRequest); System.out.println("User : " + userInput); recognizeTextResponse.messages().forEach(message -> { System.out.println("Bot : " + message.content()); }); // utterance 5 userInput = "Yes"; recognizeTextRequest = getRecognizeTextRequest(botId, botAliasId, localeId, sessionId, userInput); recognizeTextResponse = lexV2Client.recognizeText(recognizeTextRequest); System.out.println("User : " + userInput); recognizeTextResponse.messages().forEach(message -> { System.out.println("Bot : " + message.content()); }); } private static RecognizeTextRequest getRecognizeTextRequest(String botId, String botAliasId, String localeId, String sessionId, String userInput) { RecognizeTextRequest recognizeTextRequest = RecognizeTextRequest.builder() .botAliasId(botAliasId) .botId(botId) .localeId(localeId) .sessionId(sessionId) .text(userInput) .build(); return recognizeTextRequest; } }