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 653c13bd..adc3250c 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/PetStoreAssistantBot.java @@ -81,7 +81,7 @@ protected CompletableFuture onMembersAdded( .equals(member.getId(), turnContext.getActivity().getRecipient().getId())) .map(channel -> turnContext .sendActivity( - MessageFactory.text("Hello and welcome to the Azure Pet Store, How can I help you?"))) + MessageFactory.text("Hello and welcome to the Azure Pet Store! You can ask me questions about products, your shopping cart and or order and you can even ask me for information on pet animals. How can I help you?"))) .collect(CompletableFutures.toFutureList()).thenApply(resourceResponses -> null); } diff --git a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java index 676f9970..36efb917 100644 --- a/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java +++ b/petstore/petstoreassistant/src/main/java/com/chtrembl/petstoreassistant/utility/PetStoreAssistantUtilities.java @@ -13,101 +13,54 @@ public class PetStoreAssistantUtilities { private static final Logger LOGGER = LoggerFactory.getLogger(PetStoreAssistantUtilities.class); - public static String[] productIdKeyVariants = new String[] { " productid: ", " product id: ", " product id of ", " product id for "," productid ", " product id ", " productkey: ", " product key: ", " product key of ", " product key for ", " product key ", " is " }; - - //TODO move this to a regex - public static String cleanDataFromAOAIResponseContent(String content) { - // whack new lines, quotes & slashes if they exist - content = content.replace("\n", " "); // some ids would be concatenated if we don't do this - content = content.replace("\\n", " "); // some ids would be concatenated if we don't do this - content = content.replace("\\\n", " "); // some ids would be concatenated if we don't do this - content = content.replace("\"", ""); - content = content.replace("\\", ""); - content = content.replace("\\\\", ""); - content = content.replace("'", ""); - content = content.replace("\'", ""); - content = content.replace("`", ""); - content = content.replace("\\`", ""); - - // strip some parentheses and such - content = content.replaceAll("\\(", "").replaceAll("\\)", ""); - content = content.replaceAll("\\{", "").replaceAll("\\}", ""); - - // strip some ** - content = content.replaceAll("\\**", ""); - // remove the last period if it exists - if (content.charAt(content.length() - 1) == '.') { - content = content.substring(0, content.length() - 1); - } - - return content.trim(); - } - public static DPResponse processAOAIProductsCompletion(String text, ProductsCache productsCache) { DPResponse dpResponse = new DPResponse(); - String dpResponseText = null; - - String key = locateProductIDKey(text); - - if (key != null) { - - //now trim any whitespace so the regex can work - key = key.trim(); - - dpResponseText = "We have,"; - - ArrayList productIDs = new ArrayList(); - - /* - * shout out to copilot for this regex to find integers after a key that can be - * concatenated with other random stuff - */ - //create a regex to find the first group of numbers after a defined key, note the group of numbers might have other characters concatenated that can be excluded, - String keyRegEx = "(?<=" + key + ")\\D*(\\d+)"; - //This pattern uses a positive lookbehind (?<=key) to match the string "key" before the number. It then matches any non-digit characters \D* (zero or more) until it finds a group of digits \d+. The group of digits are captured in a group using parentheses. - - Matcher m = Pattern.compile(keyRegEx).matcher(text); - int i = 0; - while (m.find()) { - //just to be safe ensure we only have numbers at this point - String matchedText = m.group().replaceAll("[^0-9]+", ""); - productIDs.add(matchedText); + String dpResponseText = "We have,"; + ; + + // remove cog search references + text = text.replaceAll("\\[(doc\\d+)\\]", ""); + + // tokenize on the commas + String[] substrings = text.split(","); + + ArrayList productIDs = new ArrayList(); + + for (String substring : substrings) { + substring = substring.trim(); + // grab numbers only + Pattern pattern = Pattern.compile("\\d+"); + Matcher matcher = pattern.matcher(substring); + if (matcher.find()) { + String number = matcher.group(); + productIDs.add(number); + } else { + LOGGER.info("No product id found in substring: " + substring); } - - if (productIDs.size() > 0) { - i = 0; - for (String productID : productIDs) { - if (i == 0) { - dpResponseText += " " + productsCache.getProducts().get(productID).getName(); - i++; - } else if (i++ != productIDs.size() - 1) { - dpResponseText += ", " + productsCache.getProducts().get(productID).getName(); - } else { - dpResponseText += " and " + productsCache.getProducts().get(productID).getName(); - } + } + + if (productIDs.size() > 0) { + int i = 0; + for (String productID : productIDs) { + if (i == 0) { + dpResponseText += " " + productsCache.getProducts().get(productID).getName(); + i++; + } else if (i++ != productIDs.size() - 1) { + dpResponseText += ", " + productsCache.getProducts().get(productID).getName(); + } else { + dpResponseText += " and " + productsCache.getProducts().get(productID).getName(); } - - dpResponse.setDpResponseText(dpResponseText); - dpResponse.setResponseProductIDs(productIDs); } - } + dpResponse.setDpResponseText(dpResponseText); + dpResponse.setResponseProductIDs(productIDs); + } + return dpResponse; } - // aoai is inconsistent and non deterministic, even tho we ask for "productKey" - // there are times it will return "product id" or "product key" - // TODO clean this up with a reg expression or something - public static String locateProductIDKey(String text) { - String productIDKey = null; - //iterate through the variants and see if we can find a match - for (String variant : productIdKeyVariants) { - if (text.contains(variant)) { - productIDKey = variant; - break; - } - } - return productIDKey; + public static String cleanDataFromAOAIResponseContent(String content) { + return content.replaceAll("[\"']", "").replaceAll("\\\\", ""); } } diff --git a/petstore/petstoreassistant/src/main/resources/json/aoairequests/azurepetstoredataCompletionRequestBody.txt b/petstore/petstoreassistant/src/main/resources/json/aoairequests/azurepetstoredataCompletionRequestBody.txt index 32a4ee62..378db304 100644 --- a/petstore/petstoreassistant/src/main/resources/json/aoairequests/azurepetstoredataCompletionRequestBody.txt +++ b/petstore/petstoreassistant/src/main/resources/json/aoairequests/azurepetstoredataCompletionRequestBody.txt @@ -12,11 +12,11 @@ "messages": [ { "role":"system", - "content":"You are an AI shopping assistant that helps people find information and products related to animals" + "content":"You are an AI shopping assistant that helps people find products and product information related to pets. This information is in the referenced documents." }, { "role": "user", - "content": "%s, only return the product id and a productId key, seperating each by a \",\"" + "content": "%s Return \"productIds:\" + list of productId, separated by commas." } ] } \ No newline at end of file diff --git a/petstore/petstoreassistant/src/main/resources/json/aoairequests/chatgpt4completionRequestBody.txt b/petstore/petstoreassistant/src/main/resources/json/aoairequests/chatgpt4completionRequestBody.txt index 9123dcd8..8497642a 100644 --- a/petstore/petstoreassistant/src/main/resources/json/aoairequests/chatgpt4completionRequestBody.txt +++ b/petstore/petstoreassistant/src/main/resources/json/aoairequests/chatgpt4completionRequestBody.txt @@ -2,11 +2,11 @@ "messages": [ { - "role":"system","content": "you can only answer questions related to pets" + "role":"system","content": "If the question asked is not related to pets, shopping for pet-related items, or animals, respond by saying that: \"I'm sorry, but I only have information related to Azure Pet Store\". Limit answers to two sentences." } , { - "role":"user","content": "%s, answer in one paragraph" + "role":"user","content": "%s" } ] } \ No newline at end of file