From 2df29a4358dfa82f63c7b19f029ebde9e021b067 Mon Sep 17 00:00:00 2001 From: Anton Dubovik Date: Thu, 5 Sep 2024 17:53:26 +0100 Subject: [PATCH 1/2] fix: hard-cutting message only when truncate_prompt_error is received --- apps/chat/src/utils/server/index.ts | 51 ++++++++++++++++------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/apps/chat/src/utils/server/index.ts b/apps/chat/src/utils/server/index.ts index f9650829d3..f327e1546d 100644 --- a/apps/chat/src/utils/server/index.ts +++ b/apps/chat/src/utils/server/index.ts @@ -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; @@ -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)}`, ); } + + let 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; From 789038c04d1dc8639c6877fc966a5a29021fc5ef Mon Sep 17 00:00:00 2001 From: Anton Dubovik Date: Thu, 5 Sep 2024 18:01:04 +0100 Subject: [PATCH 2/2] fix: linting fixes --- apps/chat/src/utils/server/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/chat/src/utils/server/index.ts b/apps/chat/src/utils/server/index.ts index f327e1546d..30bba401a7 100644 --- a/apps/chat/src/utils/server/index.ts +++ b/apps/chat/src/utils/server/index.ts @@ -127,7 +127,7 @@ export const OpenAIStream = async ({ ); } - let dial_error = new DialAIError( + const dial_error = new DialAIError( result.error.message ?? '', result.error.type ?? '', result.error.param ?? '',