Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Jan 5, 2024
1 parent 83e5a11 commit 4ed246b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 58 deletions.
21 changes: 10 additions & 11 deletions petstore/petstoreassistant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<version>2.4.2</version>
<relativePath/>
</parent>

<properties>
<java.version>1.8</java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<java.version>17</java.version>
<start-class>com.chtrembl.petstoreassistant.Application</start-class>
<repo.url>https://botbuilder.myget.org/F/botbuilder-v4-java-daily/maven/</repo.url>
</properties>

<dependencies>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -58,7 +61,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.0</version>
<version>2.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -130,10 +133,6 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
Expand Down Expand Up @@ -172,8 +171,8 @@
</appSettings>
<runtime>
<os>linux</os>
<javaVersion>jre8</javaVersion>
<webContainer>jre8</webContainer>
<javaVersion>jre17</javaVersion>
<webContainer>jre17</webContainer>
</runtime>
<deployment>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -46,9 +47,6 @@
public class PetStoreAssistantBot extends ActivityHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(PetStoreAssistantBot.class);

//clean this up
private Map<String, AzurePetStoreSessionInfo> sessionCache = new HashMap<String, AzurePetStoreSessionInfo>();

@Autowired
private IAzureAIServices azureOpenAI;

Expand All @@ -59,6 +57,11 @@ public class PetStoreAssistantBot extends ActivityHandler {

private UserState userState;

Cache<String, AzurePetStoreSessionInfo> cache = Caffeine.newBuilder()
.expireAfterWrite(30, TimeUnit.MINUTES)
.maximumSize(10000)
.build();

public PetStoreAssistantBot(UserState withUserState) {
this.userState = withUserState;
}
Expand All @@ -81,58 +84,24 @@ protected CompletableFuture<Void> 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<Void> 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);

Expand Down Expand Up @@ -260,24 +229,71 @@ 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
.getAzurePetStoreSessionInfo(text);
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);
}


return azurePetStoreSessionInfo;
}

private CompletableFuture<Void> 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;
}
}

0 comments on commit 4ed246b

Please sign in to comment.