Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Nov 1, 2023
1 parent 21bd9f7 commit 81d467d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo;
import com.chtrembl.petstoreassistant.model.DPResponse;
import com.chtrembl.petstoreassistant.service.AzureOpenAI;
import com.chtrembl.petstoreassistant.utility.PetStoreAssistantUtilities;
import com.codepoetics.protonpack.collectors.CompletableFutures;
import com.microsoft.bot.builder.ActivityHandler;
import com.microsoft.bot.builder.MessageFactory;
Expand Down Expand Up @@ -43,9 +45,14 @@ public class PetStoreAssistantBot extends ActivityHandler {

@Override
protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
logTurnContext(turnContext);

String text = turnContext.getActivity().getText().toLowerCase();

// strip out session id and csrf token
AzurePetStoreSessionInfo azurePetStoreSessionInfo = PetStoreAssistantUtilities.getAzurePetStoreSessionInfo(text);
if(azurePetStoreSessionInfo != null)
{
text = azurePetStoreSessionInfo.getNewText();
}

DPResponse dpResponse = this.azureOpenAI.classification(text);

Expand Down Expand Up @@ -84,16 +91,4 @@ protected CompletableFuture<Void> onMembersAdded(
MessageFactory.text("Hello and welcome to the Azure Pet Store, you can ask me questions about our products, your shopping cart and your order, you can also ask me for information about pet animals. How can I help you?")))
.collect(CompletableFutures.toFutureList()).thenApply(resourceResponses -> null);
}

private void logTurnContext(TurnContext turnContext) {
try {
LOGGER.info("text: " + turnContext.getActivity().getText());
LOGGER.info("channel data: " + turnContext.getActivity().getChannelData());
LOGGER.info("entity data: " + turnContext.getActivity().getEntities());
LOGGER.info("properties: " + turnContext.getActivity().getProperties());
LOGGER.info("sessionid: " + turnContext.getActivity().getProperties().get("sessionid"));
} catch (Exception e) {
LOGGER.error("Error getting channel/entity data", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.chtrembl.petstoreassistant.model;

import java.io.Serializable;

public class AzurePetStoreSessionInfo implements Serializable{
private String sessionID = null;
private String csrfToken = null;
private String newText = null;

public AzurePetStoreSessionInfo(String sessionID, String csrfToken, String newText) {
super();
this.sessionID = sessionID;
this.csrfToken = csrfToken;
this.newText = newText;
}
public String getSessionID() {
return sessionID;
}
public String getCsrfToken() {
return csrfToken;
}
public String getNewText() {
return newText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo;
import com.chtrembl.petstoreassistant.model.DPResponse;
import com.chtrembl.petstoreassistant.model.ProductsCache;

Expand Down Expand Up @@ -63,4 +64,22 @@ public static String cleanDataFromAOAIResponseContent(String content) {
//remove quotes, slashes and all chars after the last period
return content.replaceAll("[\"']", "").replaceAll("\\\\", "").replaceAll("\\.[^.]*$", "");
}

public static AzurePetStoreSessionInfo getAzurePetStoreSessionInfo(String text) {
AzurePetStoreSessionInfo azurePetStoreSessionInfo = null;

Pattern pattern = Pattern.compile("sid:(.*)csrf:(.*)");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
String sessionID = matcher.group(1);
String csrfToken = matcher.group(2);
String newText = text.substring(0, text.indexOf("sid:")).trim();
azurePetStoreSessionInfo = new AzurePetStoreSessionInfo(sessionID, csrfToken, newText);
LOGGER.info("Found session id:" + sessionID + " and csrf token:" + csrfToken + " in text: " + text + " new text: " + newText);
} else {
LOGGER.info("No session id or csrf token found in text: " + text);
}

return azurePetStoreSessionInfo;
}
}

0 comments on commit 81d467d

Please sign in to comment.