Skip to content

Commit

Permalink
Error message in case when prompt exceeds the limit
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisPalnitsky committed Aug 29, 2024
1 parent 757b6d3 commit 6ad6f2e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
19 changes: 15 additions & 4 deletions api/server/middleware/abortMiddleware.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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 = {
Expand All @@ -178,6 +188,7 @@ const handleAbortError = async (res, req, error, data) => {
text: errorText,
shouldSaveMessage: true,
user: req.user.id,
code: errorKey,
};

if (partialText) {
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/Messages/Content/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions client/src/localization/languages/Eng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions packages/data-provider/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,11 @@ export enum ErrorTypes {
* Moderation error
*/
MODERATION = 'moderation',

/**
* Prompt exceeds max length
*/
PROMPT_LENGTH = 'prompt_length',
}

/**
Expand Down

0 comments on commit 6ad6f2e

Please sign in to comment.