Skip to content

Commit

Permalink
Merge branch 'development' into fix/2021-delete-app-tost
Browse files Browse the repository at this point in the history
  • Loading branch information
Derikyan authored Sep 9, 2024
2 parents 7bc7cc0 + 2b83c38 commit c8025e6
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions apps/chat/src/utils/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,7 @@ export const OpenAIStream = async ({
body,
});

if (
res.status === 400 &&
retries === 0 &&
model.limits?.isMaxRequestTokensCustom
) {
retries += 1;
const json = await res.json();
logger.info(
json,
`Getting 400 error and retrying chat request to ${model.id} model`,
);
messagesToSend = hardLimitMessages(messagesToSend);
continue;
} else if (res.status !== 200) {
if (res.status !== 200) {
let result: DialAIErrorResponse;
try {
result = (await res.json()) as DialAIErrorResponse;
Expand All @@ -134,19 +121,37 @@ export const OpenAIStream = async ({
);
}

if (result.error) {
throw new DialAIError(
result.error.message ?? '',
result.error.type ?? '',
result.error.param ?? '',
result.error.code ?? res.status.toString(10),
result.error.display_message,
);
} else {
if (!result.error) {
throw new Error(
`Core API returned an error: ${JSON.stringify(result, null, 2)}`,
);
}

const dial_error = new DialAIError(
result.error.message ?? '',
result.error.type ?? '',
result.error.param ?? '',
result.error.code ?? res.status.toString(10),
result.error.display_message,
);

if (
res.status === 400 &&
dial_error.code === 'truncate_prompt_error' &&
retries === 0 &&
model.limits?.isMaxRequestTokensCustom
) {
retries += 1;
const json = await res.json();
logger.info(
json,
`Getting error with status ${res.status} and code '${dial_error.code}'. Retrying chat request to ${model.id} model`,
);
messagesToSend = hardLimitMessages(messagesToSend);
continue;
}

throw dial_error;
}

break;
Expand Down

0 comments on commit c8025e6

Please sign in to comment.