Skip to content

Commit

Permalink
rate limit updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Feb 6, 2024
1 parent 196c080 commit 9cffa14
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
return debug;
}

DPResponse dpResponse = this.azureOpenAI.classification(text);
DPResponse dpResponse = this.azureOpenAI.classification(text, azurePetStoreSessionInfo.getSessionID());

if(dpResponse.isRateLimitExceeded())
{
return turnContext.sendActivity(
MessageFactory.text("I am sorry, you have exceeded your Azure Open AI Rate Limit, please try again shortly."))
.thenApply(sendResult -> null);
}

if (dpResponse.getClassification() == null) {
dpResponse.setClassification(Classification.SEARCH_FOR_PRODUCTS);
dpResponse = this.azureOpenAI.search(text, dpResponse.getClassification());
Expand All @@ -148,22 +155,16 @@ protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
dpResponse = this.azurePetStore.updateCart(azurePetStoreSessionInfo,
dpResponse.getProducts().get(0).getProductId());
}
} else {
dpResponse.setDpResponseText("update shopping cart request without session... text: " + text);
}
break;
case VIEW_SHOPPING_CART:
if (azurePetStoreSessionInfo != null) {
dpResponse = this.azurePetStore.viewCart(azurePetStoreSessionInfo);
} else {
dpResponse.setDpResponseText("view shopping cart request without session... text: " + text);
}
break;
case PLACE_ORDER:
if (azurePetStoreSessionInfo != null) {
dpResponse = this.azurePetStore.completeCart(azurePetStoreSessionInfo);
} else {
dpResponse.setDpResponseText("place order request without session... text: " + text);
}
break;
case SEARCH_FOR_DOG_FOOD:
Expand All @@ -174,31 +175,30 @@ protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
case SEARCH_FOR_FISH_TOYS:
case MORE_PRODUCT_INFORMATION:
case SEARCH_FOR_PRODUCTS:
if (azurePetStoreSessionInfo == null) {
dpResponse.setDpResponseText("search for products request without session... text: " + text);
} else {
if (azurePetStoreSessionInfo != null) {
dpResponse = this.azureOpenAI.search(text, dpResponse.getClassification());
}
break;
case SOMETHING_ELSE:
if (azurePetStoreSessionInfo == null) {
dpResponse.setDpResponseText("chatgpt request without session... text: " + text);
} else {
if (azurePetStoreSessionInfo != null) {
if (!text.isEmpty()) {
dpResponse = this.azureOpenAI.completion(text, dpResponse.getClassification());
dpResponse = this.azureOpenAI.completion(text, dpResponse.getClassification(), azurePetStoreSessionInfo.getSessionID());
} else {
dpResponse.setDpResponseText("chatgpt called without a search query... text: " + text);
}
}
break;
}

if(dpResponse.isRateLimitExceeded())
{
return turnContext.sendActivity(
MessageFactory.text("I am sorry, you have exceeded your Azure Open AI Rate Limit, please try again shortly."))
.thenApply(sendResult -> null);
}

if ((dpResponse.getDpResponseText() == null)) {
String responseText = "I am not sure how to handle that.";

if ((azurePetStoreSessionInfo == null)) {
responseText += " It may be because I did not have your session information.";
}
dpResponse.setDpResponseText(responseText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class DPResponse {
private boolean completeCart = false;
private String aoaiResponse = null;
private boolean contentCard = false;
private boolean rateLimitExceeded = false;

public DPResponse() {
super();
Expand Down Expand Up @@ -63,4 +64,10 @@ public boolean isContentCard() {
public void setContentCard(boolean contentCard) {
this.contentCard = contentCard;
}
public boolean isRateLimitExceeded() {
return this.rateLimitExceeded;
}
public void setRateLimitExceeded(boolean rateLimitExceeded) {
this.rateLimitExceeded = rateLimitExceeded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

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

import com.chtrembl.petstoreassistant.model.DPResponse;
import com.chtrembl.petstoreassistant.model.Product;
Expand All @@ -25,6 +25,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import retrofit2.HttpException;

@Service
public class AzureAIServices implements IAzureAIServices {
private static final Logger LOGGER = LoggerFactory.getLogger(AzureAIServices.class);
Expand All @@ -48,18 +50,23 @@ public class AzureAIServices implements IAzureAIServices {
private Resource semanticSearchRequestBodyBodyResource;
String semanticSearchRequestBodyBodyString;

private WebClient aoaiClient = WebClient.create("https://azurepetstore-aoai-gpt4.openai.azure.com");
private WebClient csClient = WebClient.create(
"https://azurepetstore-cs.search.windows.net/indexes/petproducts/docs/search?api-version=2023-10-01-Preview");
@Value("${apim.key:}")
private String apimKey;

@Value("${aoai.url}")
private String aoaiUrl;

@Value("${aoai.key}")
private String aoaiKey;

@Value("${cognitive.search.url}")
private String csUrl;

@Value("${cognitive.search.key}")
private String csKey;

@Autowired
private ICosmosDB cosmosDB;
private WebClient aoaiClient = null;
private WebClient csClient = null;

@PostConstruct
public void initialize() throws Exception {
Expand All @@ -75,6 +82,9 @@ public void initialize() throws Exception {

this.semanticSearchRequestBodyBodyString = StreamUtils
.copyToString(semanticSearchRequestBodyBodyResource.getInputStream(), Charset.defaultCharset());

this.aoaiClient = WebClient.create(this.aoaiUrl);
this.csClient = WebClient.create(this.csUrl);
}

public enum Classification {
Expand Down Expand Up @@ -108,14 +118,14 @@ public static Classification valueOfLabel(String label) {
}

@Override
public DPResponse classification(String text) {
public DPResponse classification(String text, String sessionID) {
LOGGER.info("classification invoked, text: {}", text);

DPResponse dpResponse = new DPResponse();

try {
String aoaiResponse = this.httpRequest(String.format(this.classificationRequestBodyString, text),
this.CLASSIFICATION_URI, this.aoaiKey, this.aoaiClient);
this.CLASSIFICATION_URI, this.aoaiKey, this.apimKey, sessionID, this.aoaiClient);

String classification = new Gson().fromJson(aoaiResponse, JsonElement.class).getAsJsonObject()
.get("choices")
Expand All @@ -128,14 +138,23 @@ public DPResponse classification(String text) {

LOGGER.info("classified {} as {}", text, classification);

} catch (Exception e) {
}
catch (WebClientException webClientException) {
LOGGER.error("Error parsing classification response ", webClientException);
if(webClientException.getMessage().contains("429"))
{
dpResponse.setRateLimitExceeded(true);
}
}
catch (Exception e) {
LOGGER.error("Error parsing classification response ", e);
}

return dpResponse;
}

@Override
public DPResponse completion(String text, Classification classification) {
public DPResponse completion(String text, Classification classification, String sessionID) {
LOGGER.info("completion invoked, text: {}", text);

DPResponse dpResponse = new DPResponse();
Expand All @@ -148,7 +167,7 @@ public DPResponse completion(String text, Classification classification) {
String aoaiResponse = this.httpRequest(
String.format(aoaiRequestBody,
text),
uri, this.aoaiKey, this.aoaiClient);
uri, this.aoaiKey, this.apimKey, sessionID, this.aoaiClient);

String content = null;

Expand All @@ -162,7 +181,15 @@ public DPResponse completion(String text, Classification classification) {

dpResponse.setAoaiResponse(content);
LOGGER.info("completion response for text {} was {}", text, content);
} catch (Exception e) {
}
catch (WebClientException webClientException) {
LOGGER.error("Error parsing completion response ", webClientException);
if(webClientException.getMessage().contains("429"))
{
dpResponse.setRateLimitExceeded(true);
}
}
catch (Exception e) {
LOGGER.error("Error parsing completion response ", e);
}
return dpResponse;
Expand Down Expand Up @@ -207,6 +234,8 @@ public DPResponse search(String text, Classification classification) {
category = "Fish Toy";
dpResponse.setContentCard(true);
break;
default:
break;
}

if(!classification.equals(Classification.MORE_PRODUCT_INFORMATION))
Expand All @@ -221,7 +250,7 @@ public DPResponse search(String text, Classification classification) {
LOGGER.info("search body: {}", body);

String searchResponse = this.httpRequest(body,
null, this.csKey, this.csClient);
null, this.csKey, this.apimKey, "", this.csClient);

LOGGER.info("search response: {}", searchResponse);

Expand Down Expand Up @@ -287,9 +316,11 @@ else if (products.size() > 0 && dpResponse.isContentCard())
return dpResponse;
}

private String httpRequest(String body, String uri, String apiKey, WebClient webClient) {
private String httpRequest(String body, String uri, String apiKey, String apimKey, String sessionID, WebClient webClient) throws HttpException {
String response = webClient.post().uri(uri)
.header("api-key", apiKey)
.header("Ocp-Apim-Subscription-Key", apimKey) // when APIM is enabled
.header("JSESSIONID", sessionID) // when APIM is enabled
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(body))
.retrieve()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.chtrembl.petstoreassistant.service.AzureAIServices.Classification;

public interface IAzureAIServices {
public DPResponse classification(String text);
public DPResponse completion(String text, Classification classification);
public DPResponse classification(String text, String sessionID);
public DPResponse completion(String text, Classification classification, String sessionID);
public DPResponse search(String text,Classification classification);
}

0 comments on commit 9cffa14

Please sign in to comment.