Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Sep 27, 2024
1 parent d40e94c commit 455a6ff
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.chtrembl.petstoreassistant.model.Prompt;
import com.chtrembl.petstoreassistant.service.AzureAIServices.Classification;
import com.chtrembl.petstoreassistant.service.IAzureAIServices;
import com.chtrembl.petstoreassistant.service.IAzureDemo;
import com.chtrembl.petstoreassistant.service.IAzurePetStore;
import com.chtrembl.petstoreassistant.service.ICosmosDB;
import com.chtrembl.petstoreassistant.utility.PetStoreAssistantUtilities;
Expand Down Expand Up @@ -53,6 +54,9 @@ public class PetStoreAssistantBot extends ActivityHandler {
@Autowired
private IAzurePetStore azurePetStore;

@Autowired
private IAzureDemo azureDemo;

@Autowired
private ICosmosDB cosmosDB;

Expand All @@ -62,7 +66,7 @@ public class PetStoreAssistantBot extends ActivityHandler {
private String ERROR_MESSAGE = "I am sorry, I am having trouble understanding you, please try interacting via text or restarting your browser.";

private UserState userState;

Cache<String, AzurePetStoreSessionInfo> cache = Caffeine.newBuilder()
.expireAfterWrite(30, TimeUnit.MINUTES)
.maximumSize(10000)
Expand Down Expand Up @@ -123,6 +127,21 @@ protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
text = azurePetStoreSessionInfo.getNewText();
}

if(text.startsWith("at1:"))
{
azurePetStoreSessionInfo.setAt1(text.substring(4));
return turnContext.sendActivity(
MessageFactory.text("at1 set thank you..."))
.thenApply(sendResult -> null);
}
if(text.startsWith("at2:"))
{
azurePetStoreSessionInfo.setAt2(text.substring(4));
return turnContext.sendActivity(
MessageFactory.text("at2 set thank you..."))
.thenApply(sendResult -> null);
}

// the client browser initialized
if (text.equals("...")) {
LOGGER.info("onMessageActivity new session established, " + azurePetStoreSessionInfo != null ? "session id: " + azurePetStoreSessionInfo.getId() + " id: " + azurePetStoreSessionInfo.getId() : "session id: null");
Expand Down Expand Up @@ -157,6 +176,16 @@ protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
}

switch (dpResponse.getClassification()) {
case VIEW_AZURE_RESOURCES_DEMO:
if (azurePetStoreSessionInfo != null) {
dpResponse = this.azureDemo.getAzureResources(azurePetStoreSessionInfo);
}
break;
case EXECUTE_ADO_PIPELINES_DEMO:
if (azurePetStoreSessionInfo != null) {
dpResponse = this.azureDemo.executeDevopsPipeline(azurePetStoreSessionInfo);
}
break;
case UPDATE_SHOPPING_CART:
if (azurePetStoreSessionInfo != null) {
dpResponse = this.azureOpenAI.search(text, Classification.SEARCH_FOR_PRODUCTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class AzurePetStoreSessionInfo implements Serializable{

private List<Prompt> prompts = null;

private String at1 = null;
private String at2 = null;

public AzurePetStoreSessionInfo(String sessionID, String csrfToken, String arrAffinity, String newText) {
super();
this.sessionID = sessionID.trim().toUpperCase(); //JSESSION needs to be UPPER CASE
Expand Down Expand Up @@ -62,5 +65,17 @@ public void addPrompt(Prompt prompt) {
}
this.prompts.add(prompt);
}


public void setAt1(String at1) {
this.at1 = at1;
}
public String getAt1() {
return this.at1;
}
public void setAt2(String at2) {
this.at2 = at2;
}
public String getAt2() {
return this.at2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public enum Classification {
SEARCH_FOR_FISH_FOOD("search for fish food"),
SEARCH_FOR_PRODUCTS("search for products"),
MORE_PRODUCT_INFORMATION("more product information"),
VIEW_AZURE_RESOURCES_DEMO("resources or services or subscription changes or updates"),
DISPLAY_ADO_PIPELINES_DEMO("display devops pipeline"),
EXECUTE_ADO_PIPELINES_DEMO("execute devops pipeline"),
SOMETHING_ELSE("something else");

public final String label;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.chtrembl.petstoreassistant.service;

import java.nio.charset.Charset;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientException;

import com.chtrembl.petstoreassistant.model.AzurePetStoreSessionInfo;
import com.chtrembl.petstoreassistant.model.DPResponse;
import com.chtrembl.petstoreassistant.utility.PetStoreAssistantUtilities;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


@Service
public class AzureDemo implements IAzureDemo {
private static final Logger LOGGER = LoggerFactory.getLogger(AzureDemo.class);

@Value("classpath:json/azurerequests/azureResourcesDemoBody.txt")
private Resource azureResourcesDemoBodyResource;
String azureResourcesDemoBodyBodyString;

@Value("${subscriptionId}")
private String subscriptionId;

private WebClient azureClient = null;
private WebClient adoClient = null;

@PostConstruct
public void initialize() throws Exception {
this.azureResourcesDemoBodyBodyString = StreamUtils
.copyToString(azureResourcesDemoBodyResource.getInputStream(), Charset.defaultCharset());

this.azureClient = WebClient.create("https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01");
this.adoClient = WebClient.create("https://dev.azure.com/chtrembl/PetStore/_apis/pipelines/49/runs?api-version=6.0-preview.1&Content-Type=application/json");
}

@Override
public DPResponse getAzureResources(AzurePetStoreSessionInfo azurePetStoreSessionInfo) {
LOGGER.info("getAzureResources invoked, text: {}", azurePetStoreSessionInfo.getNewText());

DPResponse dpResponse = new DPResponse();

try {
int days = 10;
if (azurePetStoreSessionInfo.getNewText().matches(".*\\d.*")) {
days = Integer.parseInt(azurePetStoreSessionInfo.getNewText().replaceAll("[^0-9]", ""));
}

String azureResponse = this.azureClient.post()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + azurePetStoreSessionInfo.getAt1())
.bodyValue((String.format(this.azureResourcesDemoBodyBodyString, subscriptionId, days)))
.retrieve()
.bodyToMono(String.class)
.block();

JsonObject jsonObject = JsonParser.parseString(azureResponse).getAsJsonObject();

// Get the data array
JsonArray dataArray = jsonObject.getAsJsonArray("data");

// StringBuilder to append resourceType values
StringBuilder resourceTypes = new StringBuilder();

// Iterate over the data array
for (int i = 0; i < dataArray.size(); i++) {
JsonObject dataObject = dataArray.get(i).getAsJsonObject();
String resourceType = dataObject.get("resourceType").getAsString();
String changeType = dataObject.get("changeType").getAsString();
resourceTypes.append(changeType+" "+resourceType).append("\n");
}

String content = "We Found: " + dataArray.size()+" over the last " + days+" days\n" +resourceTypes.toString();

dpResponse.setDpResponseText(PetStoreAssistantUtilities.cleanDataFromAOAIResponseContent(content));

dpResponse.setAoaiResponse(content);
LOGGER.info("getAzureResources response for text {} was {}", azurePetStoreSessionInfo.getNewText(), content);
}
catch (WebClientException webClientException) {
LOGGER.error("Error parsing getAzureResources response ", webClientException);
if(webClientException.getMessage().contains("429"))
{
dpResponse.setRateLimitExceeded(true);
}
dpResponse.setDpResponseText("I'm sorry, I wasn't able to get the Azure resources, check at.");
}
catch (Exception e) {
LOGGER.error("Error parsing getAzureResources response azure " + azurePetStoreSessionInfo != null ? "session id: " + azurePetStoreSessionInfo.getId() + " id: " + azurePetStoreSessionInfo.getId() : "session id: null", e);
}
return dpResponse;
}

@Override
public DPResponse executeDevopsPipeline(AzurePetStoreSessionInfo azurePetStoreSessionInfo) {
DPResponse dpResponse = new DPResponse();
dpResponse.setDpResponseText("I'm sorry, I wasn't able to execute the DevOps pipeline.");
return dpResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.chtrembl.petstoreassistant.service;

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

public interface IAzureDemo {
public DPResponse getAzureResources(AzurePetStoreSessionInfo azurePetStoreSessionInfo);
public DPResponse executeDevopsPipeline(AzurePetStoreSessionInfo azurePetStoreSessionInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"subscriptions": [
"%s"
],
"query": "resourcechanges
| extend changeTime = todatetime(properties.changeAttributes.timestamp),
targetResourceId = tostring(properties.targetResourceId),
changeType = tostring(properties.changeType),
correlationId = properties.changeAttributes.correlationId,
changedProperties = properties.changes,
changeCount = properties.changeAttributes.changesCount
| where changeTime > ago(%sd)
| order by changeTime desc
| project changeTime, targetResourceId, changeType, correlationId, changeCount, changedProperties
| summarize ChangeCount = count() by targetResourceId, changeTime, changeType
| extend resourceName = split(targetResourceId, \"/\")[array_length(split(targetResourceId, \"/\")) - 1],
resourceType = replace(\"['./']\", \" \", strcat_array(array_slice(split(targetResourceId, \"/\"), array_length(split(targetResourceId, \"/\")) - 3, array_length(split(targetResourceId, \"/\")) - 1), \"/\"))"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"role":"user",
"content":"\"%s\"\nclassify above text into one of the 12 categories \"update shopping cart\", \"view shopping cart\", \"place order\", \"search for dog toys\", \"search for dog food\", \"search for cat toys\", \"search for cat food\", \"search for fish toys\", \"search for fish food\", \"search for products\", \"more product information\", \"something else\""
"content":"\"%s\"\nclassify above text into one of the 15 categories \"resources or services or subscription changes or updates\", \"display devops pipelines\", \"execute devops pipeline\", \"update shopping cart\", \"view shopping cart\", \"place order\", \"search for dog toys\", \"search for dog food\", \"search for cat toys\", \"search for cat food\", \"search for fish toys\", \"search for fish food\", \"search for products\", \"more product information\", \"something else\""
}
]
}

0 comments on commit 455a6ff

Please sign in to comment.