Skip to content

Commit

Permalink
Tweak Lex responses and remove intents
Browse files Browse the repository at this point in the history
  • Loading branch information
docwho2 committed Nov 29, 2023
1 parent 7c399e9 commit b816aa2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 51 deletions.
36 changes: 5 additions & 31 deletions ChatGPT/src/main/java/cloud/cleo/squareup/ChatGPTLambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,36 +273,12 @@ private LexV2Response processGPT(LexV2Event lexRequest) {
return buildResponse(lexRequest, botResponse);
}

/**
* Response that sends you to the Quit intent so the call or session can be ended
*
* @param lexRequest
* @param response
* @return
*/
private LexV2Response buildQuitResponses(LexV2Event lexRequest) {

// State to return
final var ss = SessionState.builder()
// Retain the current session attributes
.withSessionAttributes(lexRequest.getSessionState().getSessionAttributes())
// Send back Quit Intent
.withIntent(Intent.builder().withName("Quit").withState("InProgress").build())
// Indicate the Action
.withDialogAction(DialogAction.builder().withType("Close").build())
.build();

final var lexV2Res = LexV2Response.builder()
.withSessionState(ss)
.build();
log.debug("Response is " + mapper.valueToTree(lexV2Res));
return lexV2Res;
}

private LexV2Response buildQuitResponse(LexV2Event lexRequest, String botResponse) {

final var attrs = lexRequest.getSessionState().getSessionAttributes();

// The controller (Chime SAM Lambda) will grab this from the session since we are delegating, then transfer the call for us
// The controller (Chime SAM Lambda) will grab this from the session
attrs.put("botResponse", botResponse);
attrs.put("action", "quit");

Expand All @@ -314,9 +290,7 @@ private LexV2Response buildQuitResponse(LexV2Event lexRequest, String botRespon
final var ss = SessionState.builder()
// Retain the current session attributes
.withSessionAttributes(attrs)
// Send back Transfer Intent and let lex know that caller will fullfil it (namely Chime SMA Controller)
.withIntent(Intent.builder().withName("FallbackIntent").withState("Fulfilled").build())
// Indicate the action as delegate, meaning lex won't fullfill, the caller will (Chime SMA Controller)
.withDialogAction(DialogAction.builder().withType("Close").build())
.build();

Expand All @@ -339,7 +313,7 @@ private LexV2Response buildTransferResponse(LexV2Event lexRequest, String transf

final var attrs = lexRequest.getSessionState().getSessionAttributes();

// The controller (Chime SAM Lambda) will grab this from the session since we are delegating, then transfer the call for us
// The controller (Chime SAM Lambda) will grab this from the session, then transfer the call for us
attrs.put("transferNumber", transferNumber);
attrs.put("action", "transfer");

Expand All @@ -354,12 +328,12 @@ private LexV2Response buildTransferResponse(LexV2Event lexRequest, String transf
// Split the response on newline because sometimes GPT adds halucination about not being able to transfer a call even though it just did
attrs.put("botResponse", botResponse.split("\n")[0]);
} else {
// Use a default transfer message if somehow no botmessage
// Use a default transfer message if somehow no bot message
attrs.put("botResponse", "Your call will no be transferred.");
}
}

log.debug("Responding with transfer Intent with transfer number [" + transferNumber + "]");
log.debug("Responding with transfer number [" + transferNumber + "]");
log.debug("Bot Response is " + botResponse);

// State to return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public static Action getMainMenu() {
// Two invocations of the bot, so create one function and use for both
Function<StartBotConversationAction, Action> botNextAction = (a) -> {
final var attrs = a.getActionData().getIntentResult().getSessionState().getSessionAttributes();
final var botResponse = attrs.get("botResponse");
final var action = attrs.get("action");
final var botResponse = attrs.get("botResponse"); // When transferring or hanging up, play back GPT's last response
final var action = attrs.get("action"); // We don't need or want real intents, so the action when exiting the Bot will be set
return switch (action) {
case "transfer" -> {
final var phone = attrs.get("transferNumber");
Expand All @@ -119,14 +119,14 @@ public static Action getMainMenu() {
SpeakAction.builder()
.withDescription("Saying Good Bye")
.withTextF(tf -> botResponse)
.withNextAction(goodbye)
.withNextAction(hangup)
.build();
default ->
SpeakAction.builder()
.withText("A system error has occured, please call back and try again")
.withNextAction(hangup)
.build();
}; // The Lex bot also has intent to speak with someone
};
};

// Both bots are the same, so the handler is the same
Expand Down
22 changes: 6 additions & 16 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,12 @@ Resources:
VoiceSettings:
VoiceId: !Ref VOICEIDEN
Intents:
- Name: "Quit"
Description: "Hang Up the call"
SampleUtterances:
- Utterance: "Quit"
- Utterance: "good bye"
- Name: "Transfer"
Description: "Send Call to a Phone Number"
- Name: "Bogus"
Description: "Must have one other intent, but we never want it to match"
SampleUtterances:
- Utterance: "null"
- Name: "FallbackIntent"
Description: "Default intent when no other intent matches, send to GPT"
Description: "Default intent, send to GPT, which should be everything"
ParentIntentSignature: "AMAZON.FallbackIntent"
FulfillmentCodeHook:
Enabled: true
Expand Down Expand Up @@ -368,17 +363,12 @@ Resources:
VoiceSettings:
VoiceId: !Ref VOICEIDES
Intents:
- Name: "Quit"
Description: "Hang Up the call"
SampleUtterances:
- Utterance: "abandonar"
- Utterance: "adiós"
- Name: "Transfer"
Description: "Send Call to a Person"
- Name: "Bogus"
Description: "Must have one other intent, but we never want it to match"
SampleUtterances:
- Utterance: "null"
- Name: "FallbackIntent"
Description: "Default intent when no other intent matches, send to GPT"
Description: "Default intent, send to GPT, which should be everything"
ParentIntentSignature: "AMAZON.FallbackIntent"
FulfillmentCodeHook:
Enabled: true
Expand Down

0 comments on commit b816aa2

Please sign in to comment.