lex_common.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <lex_common/lex_common.h>
17 
18 #include <algorithm>
19 #include <string>
20 #include <memory>
21 #include <vector>
22 
23 namespace Aws
24 {
25 namespace Lex
26 {
27 std::ostream & operator<<(
28  std::ostream & os,
29  const Aws::LexRuntimeService::Model::PostContentRequest & request)
30 {
31  os << "Request: " << std::endl;
32  os << "Bot Alias: " << request.GetBotAlias() << std::endl;
33  os << "Bot Name : " << request.GetBotName() << std::endl;
34  std::stringstream ss;
35  ss << request.GetBody()->rdbuf();
36  os << "Input data: " << ss.str() << std::endl;
37  os << "User Id: " << request.GetUserId() << std::endl;
38  os << "Accept Type: " << request.GetAccept() << std::endl;
39  os << "Content Type: " << request.GetContentType() << std::endl;
40  return os;
41 }
42 
43 std::ostream & operator<<(
44  std::ostream & os,
45  const Aws::LexRuntimeService::Model::PostContentResult & result)
46 {
47  os << "PostContentResult: " << std::endl;
48  os << "Message: " << result.GetMessage() << std::endl;
49  os << "Slot to elicit: " << result.GetSlotToElicit() << std::endl;
50  using Aws::LexRuntimeService::Model::MessageFormatTypeMapper::GetNameForMessageFormatType;
51  using Aws::LexRuntimeService::Model::DialogStateMapper::GetNameForDialogState;
52  os << "Dialog State: " << GetNameForDialogState(result.GetDialogState()) << std::endl;
53  os << "Message format type: " << GetNameForMessageFormatType(result.GetMessageFormat()) <<
54  std::endl;
55  os << "Slots: " << result.GetSlots() << std::endl;
56  os << "Session Attributes: " << result.GetSessionAttributes() << std::endl;
57  os << "Content Type: " << result.GetContentType() << std::endl;
58  os << "Intent Name: " << result.GetIntentName() << std::endl;
59  return os;
60 }
61 
63  Aws::LexRuntimeService::Model::PostContentResult & result,
64  LexResponse & response)
65 {
66  using Aws::LexRuntimeService::Model::MessageFormatTypeMapper::GetNameForMessageFormatType;
67  response.message_format_type = GetNameForMessageFormatType(result.GetMessageFormat()).c_str();
68  response.text_response = result.GetMessage().c_str();
69 
70  // Copy audio stream into vector
71  std::streampos audio_size = result.GetAudioStream().seekg(0, std::ios_base::end).tellg();
72  response.audio_response = std::vector<uint8_t>(audio_size);
73  result.GetAudioStream().seekg(0, std::ios_base::beg);
74  result.GetAudioStream().read(reinterpret_cast<char *>(&response.audio_response[0]), audio_size);
75 
76  response.intent_name = result.GetIntentName().c_str();
77  using Aws::LexRuntimeService::Model::DialogStateMapper::GetNameForDialogState;
78  response.dialog_state = GetNameForDialogState(result.GetDialogState()).c_str();
79  std::string session_attributes = result.GetSessionAttributes().c_str();
80 
81  // Strings are Base64 from lex, decode and store into a slot string
82  auto slot_byte_buffer = Aws::Utils::HashingUtils::Base64Decode(result.GetSlots().c_str());
83  Aws::String slot_string(reinterpret_cast<char *>(slot_byte_buffer.GetUnderlyingData()),
84  slot_byte_buffer.GetLength());
85  response.session_attributes = result.GetSessionAttributes().c_str();
86 
87  // Parse json into a map of slots
88  if (!slot_string.empty()) {
89  auto slot_json = Aws::Utils::Json::JsonValue(slot_string);
90  if (slot_json.WasParseSuccessful()) {
91  AWS_LOGSTREAM_DEBUG(__func__, "slot_json: " << slot_string);
92 
93  auto view = slot_json.View();
94  for (auto & element : view.GetAllObjects()) {
95  response.slots[element.first.c_str()] = element.second.AsString().c_str();
96  }
97  } else {
98  AWS_LOGSTREAM_WARN(__func__, "Unable to parse slot string " << slot_string);
99  return INVALID_RESULT;
100  }
101  }
102  return SUCCESS;
103 }
104 
106  std::shared_ptr<LexConfiguration> lex_configuration,
107  std::shared_ptr<Aws::LexRuntimeService::LexRuntimeServiceClient> lex_runtime_client)
108 {
109  if (!(lex_configuration && lex_runtime_client)) {
111  }
112  lex_configuration_ = lex_configuration;
113  lex_runtime_client_ = lex_runtime_client;
114  return ErrorCode::SUCCESS;
115 }
116 
118  const LexRequest & request,
119  LexResponse & response)
120 {
121  Aws::LexRuntimeService::Model::PostContentRequest post_content_request;
122  post_content_request.WithBotAlias(lex_configuration_->bot_alias.c_str())
123  .WithBotName(lex_configuration_->bot_name.c_str())
124  .WithAccept(request.accept_type.c_str())
125  .WithUserId(lex_configuration_->user_id.c_str());
126 
127  post_content_request.SetContentType(request.content_type.c_str());
128  auto io_stream = Aws::MakeShared<Aws::StringStream>(kAllocationTag);
129 
130  if (!request.audio_request.empty()) {
131  std::copy(request.audio_request.begin(),
132  request.audio_request.end(), std::ostream_iterator<unsigned char>(*io_stream));
133  } else {
134  *io_stream << request.text_request;
135  }
136  post_content_request.SetBody(io_stream);
137  AWS_LOGSTREAM_DEBUG(__func__, "PostContentRequest " << post_content_request);
138  auto post_content_result = lex_runtime_client_->PostContent(post_content_request);
139  ErrorCode result_code;
140  if (post_content_result.IsSuccess()) {
141  auto & result = post_content_result.GetResult();
142  AWS_LOGSTREAM_DEBUG(__func__, "PostContentResult succeeded: " << result.GetMessage());
143  result_code = CopyResult(result, response);
144 
145  } else {
146  bool is_retryable = post_content_result.GetError().ShouldRetry();
147  result_code = is_retryable ? RETRY_POST_CONTENT : FAILED_POST_CONTENT;
148  AWS_LOGSTREAM_ERROR(__func__,
149  "Aws Lex Error Has Occurred during LexRuntimeService->PostContent" << std::endl <<
150  "PostContentResult failed: " << std::endl <<
151  post_content_result.GetError() << std::endl <<
152  "Request which caused error: " << std::endl <<
153  post_content_request
154  );
155  }
156  return result_code;
157 }
158 
159 } // namespace Lex
160 } // namespace Aws
std::string text_request
Definition: lex_common.h:64
std::string session_attributes
Definition: lex_common.h:80
static const char * kAllocationTag
Definition: lex_common.h:56
std::string content_type
Definition: lex_common.h:66
std::vector< uint8_t > audio_request
Definition: lex_common.h:65
std::shared_ptr< Aws::LexRuntimeService::LexRuntimeServiceClient > lex_runtime_client_
Definition: lex_common.h:148
ErrorCode CopyResult(Aws::LexRuntimeService::Model::PostContentResult &result, LexResponse &response)
Copy the PostContentRestult to a LexResponse.
Definition: lex_common.cpp:62
std::ostream & operator<<(std::ostream &os, const Aws::LexRuntimeService::Model::PostContentRequest &request)
Definition: lex_common.cpp:27
std::string message_format_type
Definition: lex_common.h:75
std::string accept_type
Definition: lex_common.h:63
std::unordered_map< std::string, std::string > slots
Definition: lex_common.h:79
ErrorCode ConfigureAwsLex(std::shared_ptr< LexConfiguration > lex_configuration, std::shared_ptr< Aws::LexRuntimeService::LexRuntimeServiceClient > lex_runtime_client)
Configure the Lex Interactor.
Definition: lex_common.cpp:105
std::string text_response
Definition: lex_common.h:74
std::shared_ptr< LexConfiguration > lex_configuration_
Definition: lex_common.h:143
std::vector< uint8_t > audio_response
Definition: lex_common.h:76
std::string intent_name
Definition: lex_common.h:77
ErrorCode PostContent(const LexRequest &request, LexResponse &response) override
Definition: lex_common.cpp:117
std::string dialog_state
Definition: lex_common.h:78


lex_common
Author(s): AWS RoboMaker
autogenerated on Sat Mar 6 2021 03:43:24