Prevent OpenAi retry on http client errors such as rate-limit or wrong authentication

This commit is contained in:
Christian Tzolov
2024-01-14 23:04:17 +01:00
parent abbd911f69
commit 08fa0e393c
2 changed files with 22 additions and 0 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.ai.metadata.RateLimit;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage;
import org.springframework.ai.openai.api.OpenAiApi.OpenAiApiClientErrorException;
import org.springframework.ai.openai.api.OpenAiApi.OpenAiApiException;
import org.springframework.ai.openai.metadata.OpenAiGenerationMetadata;
import org.springframework.ai.openai.metadata.support.OpenAiResponseHeaderExtractor;

View File

@@ -94,6 +94,10 @@ public class OpenAiApi {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
if (response.getStatusCode().isError()) {
if (response.getStatusCode().is4xxClientError()) {
throw new OpenAiApiClientErrorException(String.format("%s - %s", response.getStatusCode().value(),
new ObjectMapper().readValue(response.getBody(), ResponseError.class)));
}
throw new OpenAiApiException(String.format("%s - %s", response.getStatusCode().value(),
new ObjectMapper().readValue(response.getBody(), ResponseError.class)));
}
@@ -124,6 +128,23 @@ public class OpenAiApi {
}
}
/**
* Thrown on 4xx client errors, such as 401 - Incorrect API key provided,
* 401 - You must be a member of an organization to use the API,
* 429 - Rate limit reached for requests, 429 - You exceeded your current quota
* , please check your plan and billing details.
*/
public static class OpenAiApiClientErrorException extends RuntimeException {
public OpenAiApiClientErrorException(String message) {
super(message);
}
public OpenAiApiClientErrorException(String message, Throwable cause) {
super(message, cause);
}
}
/**
* API error response.
* @param error Error details.