Skip to content

Commit

Permalink
Merge pull request #342 from Portkey-AI/feat/preconditional_check
Browse files Browse the repository at this point in the history
feat: preconditional check for request validation
  • Loading branch information
VisargD committed May 13, 2024
2 parents 8207f61 + 01f7684 commit 863fddf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const RESPONSE_HEADER_KEYS: Record<string, string> = {
export const RETRY_STATUS_CODES = [429, 500, 502, 503, 504];
export const MAX_RETRIES = 5;
export const REQUEST_TIMEOUT_STATUS_CODE = 408;
export const PRECONDITION_CHECK_FAILED_STATUS_CODE = 412;

export const OPEN_AI: string = 'openai';
export const COHERE: string = 'cohere';
Expand Down
28 changes: 20 additions & 8 deletions src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,16 @@ export async function tryPost(
onStatusCodes: providerOption.retry?.onStatusCodes ?? RETRY_STATUS_CODES,
};

const [getFromCacheFunction, cacheIdentifier, requestOptions] = [
const [
getFromCacheFunction,
cacheIdentifier,
requestOptions,
preRequestValidator,
] = [
c.get('getFromCache'),
c.get('cacheIdentifier'),
c.get('requestOptions') ?? [],
c.get('preRequestValidator'),
];

let cacheResponse, cacheKey, cacheMode, cacheMaxAge;
Expand Down Expand Up @@ -575,13 +581,19 @@ export async function tryPost(
}
}

[response, retryCount] = await retryRequest(
url,
fetchOptions,
providerOption.retry.attempts,
providerOption.retry.onStatusCodes,
providerOption.requestTimeout || null
);
response = preRequestValidator
? preRequestValidator(providerOption, requestHeaders)
: undefined;

if (!response) {
[response, retryCount] = await retryRequest(
url,
fetchOptions,
providerOption.retry.attempts,
providerOption.retry.onStatusCodes,
providerOption.requestTimeout || null
);
}

const mappedResponse = await responseHandler(
response,
Expand Down
8 changes: 7 additions & 1 deletion src/handlers/streamHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
COHERE,
GOOGLE,
REQUEST_TIMEOUT_STATUS_CODE,
PRECONDITION_CHECK_FAILED_STATUS_CODE,
} from '../globals';
import { OpenAIChatCompleteResponse } from '../providers/openai/chatComplete';
import { OpenAICompleteResponse } from '../providers/openai/complete';
Expand Down Expand Up @@ -200,7 +201,12 @@ export async function handleNonStreamingMode(
// 408 is thrown whenever a request takes more than request_timeout to respond.
// In that case, response thrown by gateway is already in OpenAI format.
// So no need to transform it again.
if (response.status === REQUEST_TIMEOUT_STATUS_CODE) {
if (
[
REQUEST_TIMEOUT_STATUS_CODE,
PRECONDITION_CHECK_FAILED_STATUS_CODE,
].includes(response.status)
) {
return response;
}

Expand Down

0 comments on commit 863fddf

Please sign in to comment.