Skip to content

Commit

Permalink
cleanup openai prompts and hack code
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Oct 25, 2023
1 parent e4b3c2a commit a2f4eca
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected CompletableFuture<Void> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> productIDs = new ArrayList<String>();

/*
* 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<String> productIDs = new ArrayList<String>();

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("\\\\", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}

0 comments on commit a2f4eca

Please sign in to comment.