From 6ad6f2efc2c948d39e72a2ff72db9be3370d294b Mon Sep 17 00:00:00 2001 From: Denis Palnitsky Date: Thu, 29 Aug 2024 09:43:16 +0200 Subject: [PATCH] Error message in case when prompt exceeds the limit --- api/server/middleware/abortMiddleware.js | 19 +++++++++++++++---- .../src/components/Messages/Content/Error.tsx | 3 ++- client/src/localization/languages/Eng.ts | 2 ++ packages/data-provider/src/config.ts | 5 +++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/api/server/middleware/abortMiddleware.js b/api/server/middleware/abortMiddleware.js index e855c0cb69d..92f2b718d60 100644 --- a/api/server/middleware/abortMiddleware.js +++ b/api/server/middleware/abortMiddleware.js @@ -1,4 +1,4 @@ -const { isAssistantsEndpoint } = require('librechat-data-provider'); +const { isAssistantsEndpoint, ErrorTypes } = require('librechat-data-provider'); const { sendMessage, sendError, countTokens, isEnabled } = require('~/server/utils'); const { truncateText, smartTruncateText } = require('~/app/clients/prompts'); const clearPendingReq = require('~/cache/clearPendingReq'); @@ -165,9 +165,19 @@ const handleAbortError = async (res, req, error, data) => { ); } - const errorText = error?.message?.includes('"type"') - ? error.message - : 'An error occurred while processing your request. Please contact the Admin.'; + let errorText; + + let errorKey = 0; + if (error?.message?.includes('"type"')) { + errorText = error.message; + } else if ( + error?.message?.includes('Prompt token count of') && + error.message.includes('exceeds') + ) { + errorText = `{ "code": "${ErrorTypes.PROMPT_LENGTH}", "message": "The prompt token count exceeded the limit. Please try again with a shorter prompt or move part of the prompt to a text file and attach it to chat." }`; + } else { + errorText = 'An error occurred while processing your request. Please contact the Admin.'; + } const respondWithError = async (partialText) => { let options = { @@ -178,6 +188,7 @@ const handleAbortError = async (res, req, error, data) => { text: errorText, shouldSaveMessage: true, user: req.user.id, + code: errorKey, }; if (partialText) { diff --git a/client/src/components/Messages/Content/Error.tsx b/client/src/components/Messages/Content/Error.tsx index 966528f1249..6c9c5340dd5 100644 --- a/client/src/components/Messages/Content/Error.tsx +++ b/client/src/components/Messages/Content/Error.tsx @@ -42,6 +42,7 @@ const errorMessages = { const { expiredAt, endpoint } = json; return localize('com_error_expired_user_key', endpoint, expiredAt); }, + [ErrorTypes.PROMPT_LENGTH]: 'com_error_prompt_length', [ViolationTypes.BAN]: 'Your account has been temporarily banned due to violations of our service.', invalid_api_key: @@ -87,9 +88,9 @@ const errorMessages = { const Error = ({ text }: { text: string }) => { const localize = useLocalize(); const jsonString = extractJson(text); + const errorMessage = text.length > 512 && !jsonString ? text.slice(0, 512) + '...' : text; const defaultResponse = `Something went wrong. Here's the specific error message we encountered: ${errorMessage}`; - if (!isJson(jsonString)) { return defaultResponse; } diff --git a/client/src/localization/languages/Eng.ts b/client/src/localization/languages/Eng.ts index f2c8ee84284..f3b9f3c96e5 100644 --- a/client/src/localization/languages/Eng.ts +++ b/client/src/localization/languages/Eng.ts @@ -23,6 +23,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_prompt_length: + 'The prompt is too long and exceeds the token limit. Please shorten your prompt or attach part of it as a text file.', 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..f6e6ab6c3b3 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 + */ + PROMPT_LENGTH = 'prompt_length', } /**