From 4ed246b9f1fe5d6ef8ee40698ec33387d2e79ac0 Mon Sep 17 00:00:00 2001 From: Chris Tremblay Date: Fri, 5 Jan 2024 10:57:12 -0500 Subject: [PATCH] updates --- petstore/petstoreassistant/pom.xml | 21 ++-- .../PetStoreAssistantBot.java | 110 ++++++++++-------- 2 files changed, 73 insertions(+), 58 deletions(-) diff --git a/petstore/petstoreassistant/pom.xml b/petstore/petstoreassistant/pom.xml index b37df8f6..d863e1b2 100644 --- a/petstore/petstoreassistant/pom.xml +++ b/petstore/petstoreassistant/pom.xml @@ -17,19 +17,22 @@ org.springframework.boot spring-boot-starter-parent - 2.4.0 + 2.4.2 - 1.8 - 1.8 - 1.8 + 17 com.chtrembl.petstoreassistant.Application https://botbuilder.myget.org/F/botbuilder-v4-java-daily/maven/ + + com.github.ben-manes.caffeine + caffeine + 3.1.8 + junit junit @@ -58,7 +61,7 @@ org.springframework.boot spring-boot-starter-test - 2.4.0 + 2.4.2 test @@ -130,10 +133,6 @@ maven-compiler-plugin 3.8.1 - - 1.8 - 1.8 - maven-war-plugin @@ -172,8 +171,8 @@ linux - jre8 - jre8 + jre17 + jre17 diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java index de52d6d5..db2d0bee 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java @@ -3,10 +3,9 @@ package com.chtrembl.petstoreassistant; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,6 +19,8 @@ import com.chtrembl.petstoreassistant.service.IAzureAIServices; import com.chtrembl.petstoreassistant.service.IAzurePetStore; import com.chtrembl.petstoreassistant.utility.PetStoreAssistantUtilities; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.microsoft.bot.builder.ActivityHandler; @@ -46,9 +47,6 @@ public class PetStoreAssistantBot extends ActivityHandler { private static final Logger LOGGER = LoggerFactory.getLogger(PetStoreAssistantBot.class); - //clean this up - private Map sessionCache = new HashMap(); - @Autowired private IAzureAIServices azureOpenAI; @@ -59,6 +57,11 @@ public class PetStoreAssistantBot extends ActivityHandler { private UserState userState; + Cache cache = Caffeine.newBuilder() + .expireAfterWrite(30, TimeUnit.MINUTES) + .maximumSize(10000) + .build(); + public PetStoreAssistantBot(UserState withUserState) { this.userState = withUserState; } @@ -81,58 +84,24 @@ protected CompletableFuture onMessageActivity(TurnContext turnContext) { AzurePetStoreSessionInfo azurePetStoreSessionInfo = configureSession(turnContext, text); - // get the text without the session id and csrf token if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) { + // get the text without the session id and csrf token text = azurePetStoreSessionInfo.getNewText(); } - //the client kickoff message + //the client browser initialized if(text.equals("...")) { return turnContext.sendActivity( MessageFactory.text(WELCOME_MESSAGE)).thenApply(sendResult -> null); } - //DEBUG ONLY - if (text.contains("debug")) - { - return turnContext.sendActivity( - MessageFactory.text("id:"+azurePetStoreSessionInfo.getId())).thenApply(sendResult -> null); - } - - if (text.contains("card")) { - if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) - { - text = azurePetStoreSessionInfo.getNewText(); - } - String jsonString = "{\"type\":\"buttonWithImage\",\"id\":\"buttonWithImage\",\"data\":{\"title\":\"Soul Machines\",\"imageUrl\":\"https://www.soulmachines.com/wp-content/uploads/cropped-sm-favicon-180x180.png\",\"description\":\"Soul Machines is the leader in astonishing AGI\",\"imageAltText\":\"some text\",\"buttonText\":\"push me\"}}"; - - Attachment attachment = new Attachment(); - attachment.setContentType("application/json"); - - attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); - attachment.setName("public-content-card"); - - return turnContext.sendActivity( - MessageFactory.attachment(attachment, "I have something nice to show @showcards(content-card) you.")) - .thenApply(sendResult -> null); - } - - if (text.contains("ball")) + CompletableFuture debug = getDebug(turnContext, text, azurePetStoreSessionInfo); + if(debug != null) { - String jsonString = "{\"type\":\"image\",\"id\":\"image-ball\",\"data\":{\"url\": \"https://raw.githubusercontent.com/chtrembl/staticcontent/master/dog-toys/ball.jpg?raw=true\",\"alt\": \"This is a ball\"}}"; - Attachment attachment = new Attachment(); - attachment.setContentType("application/json"); - - attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); - attachment.setName("public-image-ball"); - - return turnContext.sendActivity( - MessageFactory.attachment(attachment, "I have something nice to show @showcards(image-ball) you.")) - .thenApply(sendResult -> null); + return debug; } - //END DEBUG DPResponse dpResponse = this.azureOpenAI.classification(text); @@ -260,7 +229,7 @@ private AzurePetStoreSessionInfo configureSession(TurnContext turnContext, Strin id = id.substring(0, id.indexOf("-")); } - AzurePetStoreSessionInfo azurePetStoreSessionInfo = this.sessionCache.get(id); + AzurePetStoreSessionInfo azurePetStoreSessionInfo = this.cache.getIfPresent(id); // strip out session id and csrf token if one was passed in AzurePetStoreSessionInfo incomingAzurePetStoreSessionInfo = PetStoreAssistantUtilities @@ -268,11 +237,11 @@ private AzurePetStoreSessionInfo configureSession(TurnContext turnContext, Strin if (incomingAzurePetStoreSessionInfo != null) { text = incomingAzurePetStoreSessionInfo.getNewText(); //turnContext.getActivity().getId() is unique per browser over the broken recipient for some reason - this.sessionCache.put(id, incomingAzurePetStoreSessionInfo); + this.cache.put(id, incomingAzurePetStoreSessionInfo); azurePetStoreSessionInfo = incomingAzurePetStoreSessionInfo; azurePetStoreSessionInfo.setId(id); } - else + else if(azurePetStoreSessionInfo != null) { azurePetStoreSessionInfo.setNewText(text); } @@ -280,4 +249,51 @@ private AzurePetStoreSessionInfo configureSession(TurnContext turnContext, Strin return azurePetStoreSessionInfo; } + + private CompletableFuture getDebug(TurnContext turnContext, String text, AzurePetStoreSessionInfo azurePetStoreSessionInfo) { + if (text.contains("debug")) + { + if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) + { + return turnContext.sendActivity( + MessageFactory.text("id:"+azurePetStoreSessionInfo.getId()+", cache size: "+cache.estimatedSize())).thenApply(sendResult -> null); + } + else{ + return turnContext.sendActivity( + MessageFactory.text("azurePetStoreSessionInfo was null, cache size: "+cache.estimatedSize())).thenApply(sendResult -> null); + } + } + if (text.contains("card")) { + if(azurePetStoreSessionInfo != null && azurePetStoreSessionInfo.getNewText() != null) + { + text = azurePetStoreSessionInfo.getNewText(); + } + String jsonString = "{\"type\":\"buttonWithImage\",\"id\":\"buttonWithImage\",\"data\":{\"title\":\"Soul Machines\",\"imageUrl\":\"https://www.soulmachines.com/wp-content/uploads/cropped-sm-favicon-180x180.png\",\"description\":\"Soul Machines is the leader in astonishing AGI\",\"imageAltText\":\"some text\",\"buttonText\":\"push me\"}}"; + + Attachment attachment = new Attachment(); + attachment.setContentType("application/json"); + + attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); + attachment.setName("public-content-card"); + + return turnContext.sendActivity( + MessageFactory.attachment(attachment, "I have something nice to show @showcards(content-card) you.")) + .thenApply(sendResult -> null); + } + + if (text.contains("ball")) + { + String jsonString = "{\"type\":\"image\",\"id\":\"image-ball\",\"data\":{\"url\": \"https://raw.githubusercontent.com/chtrembl/staticcontent/master/dog-toys/ball.jpg?raw=true\",\"alt\": \"This is a ball\"}}"; + Attachment attachment = new Attachment(); + attachment.setContentType("application/json"); + + attachment.setContent(new Gson().fromJson(jsonString, JsonObject.class)); + attachment.setName("public-image-ball"); + + return turnContext.sendActivity( + MessageFactory.attachment(attachment, "I have something nice to show @showcards(image-ball) you.")) + .thenApply(sendResult -> null); + } + return null; + } }