Skip to content

Commit

Permalink
updates to persist prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Jan 8, 2024
1 parent 6f4ba65 commit 0c4614e
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 150 deletions.
2 changes: 1 addition & 1 deletion petstore/petstoreassistant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
<version>2.22.1</version>
</dependency>

<dependency>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.chtrembl.petstoreassistant.model;

import java.io.Serializable;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

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

private List<Prompt> prompts = null;

public AzurePetStoreSessionInfo(String sessionID, String csrfToken, String newText) {
super();
this.sessionID = sessionID.trim().toUpperCase(); //JSESSION needs to be UPPER CASE
Expand All @@ -32,4 +38,17 @@ public String getId() {
public void setId(String id) {
this.id = id;
}
public List<Prompt> getPrompts() {
return prompts;
}
public void setPrompts(List<Prompt> prompts) {
this.prompts = prompts;
}
public void addPrompt(Prompt prompt) {
if (this.prompts == null) {
this.prompts = new java.util.ArrayList<Prompt>();
}
this.prompts.add(prompt);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.chtrembl.petstoreassistant.model;

import java.io.Serializable;

import com.chtrembl.petstoreassistant.service.AzureAIServices.Classification;

public class Prompt implements Serializable{
private Classification classification = null;
private String question = null;
private String answer = null;

public Prompt() {
super();
}
public Prompt(Classification classification, String question, String answer) {
super();
this.classification = classification;
this.question = question;
this.answer = answer;
}
public Classification getClassification() {
return classification;
}
public void setClassification(Classification classification) {
this.classification = classification;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public DPResponse search(String text, Classification classification) {
DPResponse dpResponse = new DPResponse();
dpResponse.setClassification(classification);

String dpResponseText = "I found some information on our products. We have a ";
String dpResponseText = "We have a ";

String filter = "";

Expand Down Expand Up @@ -265,7 +265,7 @@ public DPResponse search(String text, Classification classification) {
if(products.size() == 1 || classification.equals(Classification.MORE_PRODUCT_INFORMATION))
{
dpResponse.setImageContentCard(true);
dpResponseText = "Check out this product of ours, the " + products.get(0).getName();
dpResponseText = "Check out this product, the " + products.get(0).getName();

dpResponse.setDpResponseText(dpResponseText);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo;
import com.chtrembl.petstoreassistant.model.DPResponse;
import com.chtrembl.petstoreassistant.service.AzureAIServices.Classification;

import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -30,7 +31,7 @@ public class AzurePetStore implements IAzurePetStore {
@Override
public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo, String productId) {
DPResponse dpResponse = new DPResponse();

try {
Request request = new Request.Builder()
.url(this.UPDATE_CART_URL + "?csrf=" + azurePetStoreSessionInfo.getCsrfToken()
Expand All @@ -47,15 +48,15 @@ public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo,
+ azurePetStoreSessionInfo.getCsrfToken());

dpResponse.setDpResponseText("I just added the "
+ this.cosmosDB.getCachedProducts().get(productId).getName()
+ this.cosmosDB.getProducts().get(productId).getName()
+ " to your cart.");

dpResponse.setUpdateCart(true);
} catch (Exception e) {
LOGGER.error("Error updating cart with product id: " + productId + " for session id: "
+ azurePetStoreSessionInfo.getSessionID() + " " + e.getMessage());
dpResponse.setDpResponseText("I'm sorry, I wasn't able to add the "
+ this.cosmosDB.getCachedProducts().get(productId).getName()
+ this.cosmosDB.getProducts().get(productId).getName()
+ " to your cart. " + azurePetStoreSessionInfo.getSessionID() + "|"
+ azurePetStoreSessionInfo.getCsrfToken());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.util.CosmosPagedIterable;
import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo;
import com.chtrembl.petstoreassistant.model.Product;

@Service
Expand All @@ -26,7 +27,8 @@ public class CosmosDB implements ICosmosDB {

private static final String DATABASE_ID = "E-Commerce";

private static final String CONTAINER_ID = "ProductsV2";
private static final String PRODUCTS_CONTAINER_ID = "ProductsV2";
private static final String PROMPTS_CONTAINER_ID = "Prompts";

private CosmosClient client = null;

Expand All @@ -39,14 +41,13 @@ public void initialize() throws Exception {
.endpoint(ENDPOINT)
.key(this.cosmosKey)
.buildClient();
this.products = this.getProducts();
this.cacheProducts();
}

@Override
public HashMap<String, Product> getProducts() {
HashMap<String, Product> products = new HashMap<String, Product> ();
private void cacheProducts() {
this.products = new HashMap<String, Product> ();

CosmosContainer container = client.getDatabase(DATABASE_ID).getContainer(CONTAINER_ID);
CosmosContainer container = client.getDatabase(DATABASE_ID).getContainer(this.PRODUCTS_CONTAINER_ID);

String query = "SELECT * FROM ProductsV2";

Expand All @@ -55,12 +56,25 @@ public HashMap<String, Product> getProducts() {
products.put(product.getProductId(), product);
}

LOGGER.info("Retrieved " + products.size() + " products from CosmosDB");

return products;
LOGGER.info("Cached " + products.size() + " products from CosmosDB");
}

public HashMap<String, Product> getCachedProducts() {
public HashMap<String, Product> getProducts() {
return this.products;
}

public void storePrompt(AzurePetStoreSessionInfo azurePetStoreSessionInfo) {
CosmosContainer container = client.getDatabase(DATABASE_ID).getContainer(this.PROMPTS_CONTAINER_ID);

try
{
container.upsertItem(azurePetStoreSessionInfo);
LOGGER.info("Upsert prompt record in CosmosDB id: " + azurePetStoreSessionInfo.getId());

}
catch (Exception e)
{
LOGGER.error("Error upserting prompt in CosmosDB " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.util.HashMap;

import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo;
import com.chtrembl.petstoreassistant.model.Product;

public interface ICosmosDB {
public HashMap<String, Product> getProducts();
public HashMap<String, Product> getCachedProducts();
public void storePrompt(AzurePetStoreSessionInfo azurePetStoreSessionInfo);
}

0 comments on commit 0c4614e

Please sign in to comment.