Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hard-cutting message only when truncate_prompt_error code is received #2067

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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