From 3c9c924fde9cb6d9412c7865aa8c9eb5f75ee187 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 30 Aug 2024 14:46:34 -0400 Subject: [PATCH 1/3] feat: CONTEXT_LENGTH error type --- api/app/clients/BaseClient.js | 15 +++++++++++---- client/src/components/Messages/Content/Error.tsx | 8 ++++++++ client/src/localization/languages/Eng.ts | 2 ++ packages/data-provider/src/config.ts | 5 +++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.js index 22940bae5f8..597a4643b6c 100644 --- a/api/app/clients/BaseClient.js +++ b/api/app/clients/BaseClient.js @@ -1,6 +1,12 @@ const crypto = require('crypto'); const fetch = require('node-fetch'); -const { supportsBalanceCheck, Constants, CacheKeys, Time } = require('librechat-data-provider'); +const { + supportsBalanceCheck, + Constants, + CacheKeys, + Time, + ErrorTypes, +} = require('librechat-data-provider'); const { getMessages, saveMessage, updateMessage, saveConvo } = require('~/models'); const { addSpaceIfNeeded, isEnabled } = require('~/server/utils'); const checkBalance = require('~/models/checkBalance'); @@ -383,9 +389,10 @@ class BaseClient { const latestMessage = orderedWithInstructions[orderedWithInstructions.length - 1]; if (payload.length === 0 && !shouldSummarize && latestMessage) { - throw new Error( - `Prompt token count of ${latestMessage.tokenCount} exceeds max token count of ${this.maxContextTokens}.`, - ); + const info = `${latestMessage.tokenCount} / ${this.maxContextTokens}`; + const errorMessage = `{ "type": "${ErrorTypes.CONTEXT_LENGTH}", "info": "${info}" }`; + logger.warn(`Prompt token count exceeds max token count (${info}).`); + throw new Error(errorMessage); } if (usePrevSummary) { diff --git a/client/src/components/Messages/Content/Error.tsx b/client/src/components/Messages/Content/Error.tsx index 966528f1249..63f51ace76b 100644 --- a/client/src/components/Messages/Content/Error.tsx +++ b/client/src/components/Messages/Content/Error.tsx @@ -33,6 +33,10 @@ type TExpiredKey = { endpoint: string; }; +type TContextLength = { + info: string; +}; + const errorMessages = { [ErrorTypes.MODERATION]: 'com_error_moderation', [ErrorTypes.NO_USER_KEY]: 'com_error_no_user_key', @@ -42,6 +46,10 @@ const errorMessages = { const { expiredAt, endpoint } = json; return localize('com_error_expired_user_key', endpoint, expiredAt); }, + [ErrorTypes.CONTEXT_LENGTH]: (json: TContextLength, localize: LocalizeFunction) => { + const { info } = json; + return localize('com_error_context_length', info); + }, [ViolationTypes.BAN]: 'Your account has been temporarily banned due to violations of our service.', invalid_api_key: diff --git a/client/src/localization/languages/Eng.ts b/client/src/localization/languages/Eng.ts index 3d239bb8f5b..81442e0cdcc 100644 --- a/client/src/localization/languages/Eng.ts +++ b/client/src/localization/languages/Eng.ts @@ -25,6 +25,8 @@ export default { com_error_invalid_user_key: 'Invalid key provided. Please provide a valid key and try again.', com_error_expired_user_key: 'Provided key for {0} expired at {1}. Please provide a new key and try again.', + com_error_context_length: + 'The latest message token count is too long, exceeding the token limit ({0} respectively). Please shorten your message, adjust the max context size from the conversation parameters, or fork the conversation to continue.', com_files_no_results: 'No results.', com_files_filter: 'Filter files...', com_files_number_selected: '{0} of {1} file(s) selected', diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts index a22512636eb..9af8cb567a7 100644 --- a/packages/data-provider/src/config.ts +++ b/packages/data-provider/src/config.ts @@ -839,6 +839,11 @@ export enum ErrorTypes { * Moderation error */ MODERATION = 'moderation', + + /** + * Prompt exceeds max length + */ + CONTEXT_LENGTH = 'CONTEXT_LENGTH', } /** From df4d29b53e40db85016770e381bfeee2f3847b82 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 30 Aug 2024 14:52:48 -0400 Subject: [PATCH 2/3] chore: rename error type --- api/app/clients/BaseClient.js | 2 +- client/src/components/Messages/Content/Error.tsx | 6 +++--- client/src/localization/languages/Eng.ts | 2 +- packages/data-provider/src/config.ts | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.js index 597a4643b6c..0dab52777cf 100644 --- a/api/app/clients/BaseClient.js +++ b/api/app/clients/BaseClient.js @@ -390,7 +390,7 @@ class BaseClient { const latestMessage = orderedWithInstructions[orderedWithInstructions.length - 1]; if (payload.length === 0 && !shouldSummarize && latestMessage) { const info = `${latestMessage.tokenCount} / ${this.maxContextTokens}`; - const errorMessage = `{ "type": "${ErrorTypes.CONTEXT_LENGTH}", "info": "${info}" }`; + const errorMessage = `{ "type": "${ErrorTypes.INPUT_LENGTH}", "info": "${info}" }`; logger.warn(`Prompt token count exceeds max token count (${info}).`); throw new Error(errorMessage); } diff --git a/client/src/components/Messages/Content/Error.tsx b/client/src/components/Messages/Content/Error.tsx index 63f51ace76b..476360080fc 100644 --- a/client/src/components/Messages/Content/Error.tsx +++ b/client/src/components/Messages/Content/Error.tsx @@ -33,7 +33,7 @@ type TExpiredKey = { endpoint: string; }; -type TContextLength = { +type TInputLength = { info: string; }; @@ -46,9 +46,9 @@ const errorMessages = { const { expiredAt, endpoint } = json; return localize('com_error_expired_user_key', endpoint, expiredAt); }, - [ErrorTypes.CONTEXT_LENGTH]: (json: TContextLength, localize: LocalizeFunction) => { + [ErrorTypes.INPUT_LENGTH]: (json: TInputLength, localize: LocalizeFunction) => { const { info } = json; - return localize('com_error_context_length', info); + return localize('com_error_input_length', info); }, [ViolationTypes.BAN]: 'Your account has been temporarily banned due to violations of our service.', diff --git a/client/src/localization/languages/Eng.ts b/client/src/localization/languages/Eng.ts index 81442e0cdcc..efcd403ad32 100644 --- a/client/src/localization/languages/Eng.ts +++ b/client/src/localization/languages/Eng.ts @@ -25,7 +25,7 @@ export default { com_error_invalid_user_key: 'Invalid key provided. Please provide a valid key and try again.', com_error_expired_user_key: 'Provided key for {0} expired at {1}. Please provide a new key and try again.', - com_error_context_length: + com_error_input_length: 'The latest message token count is too long, exceeding the token limit ({0} respectively). Please shorten your message, adjust the max context size from the conversation parameters, or fork the conversation to continue.', com_files_no_results: 'No results.', com_files_filter: 'Filter files...', diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts index 9af8cb567a7..405e0f77939 100644 --- a/packages/data-provider/src/config.ts +++ b/packages/data-provider/src/config.ts @@ -843,7 +843,7 @@ export enum ErrorTypes { /** * Prompt exceeds max length */ - CONTEXT_LENGTH = 'CONTEXT_LENGTH', + INPUT_LENGTH = 'INPUT_LENGTH', } /** From 51dc76b5e0142eea2eb4d02e4d12a1691f593d82 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 30 Aug 2024 14:54:26 -0400 Subject: [PATCH 3/3] chore: import order --- api/app/clients/BaseClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.js index 0dab52777cf..0d769155b1b 100644 --- a/api/app/clients/BaseClient.js +++ b/api/app/clients/BaseClient.js @@ -2,10 +2,10 @@ const crypto = require('crypto'); const fetch = require('node-fetch'); const { supportsBalanceCheck, + ErrorTypes, Constants, CacheKeys, Time, - ErrorTypes, } = require('librechat-data-provider'); const { getMessages, saveMessage, updateMessage, saveConvo } = require('~/models'); const { addSpaceIfNeeded, isEnabled } = require('~/server/utils');