Skip to content

Commit 7536e64

Browse files
authored
🚨 feat: Implement INPUT_LENGTH Error Type (danny-avila#3866)
* feat: CONTEXT_LENGTH error type * chore: rename error type * chore: import order
1 parent 6936d00 commit 7536e64

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

api/app/clients/BaseClient.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const crypto = require('crypto');
22
const fetch = require('node-fetch');
3-
const { supportsBalanceCheck, Constants, CacheKeys, Time } = require('librechat-data-provider');
3+
const {
4+
supportsBalanceCheck,
5+
ErrorTypes,
6+
Constants,
7+
CacheKeys,
8+
Time,
9+
} = require('librechat-data-provider');
410
const { getMessages, saveMessage, updateMessage, saveConvo } = require('~/models');
511
const { addSpaceIfNeeded, isEnabled } = require('~/server/utils');
612
const checkBalance = require('~/models/checkBalance');
@@ -383,9 +389,10 @@ class BaseClient {
383389

384390
const latestMessage = orderedWithInstructions[orderedWithInstructions.length - 1];
385391
if (payload.length === 0 && !shouldSummarize && latestMessage) {
386-
throw new Error(
387-
`Prompt token count of ${latestMessage.tokenCount} exceeds max token count of ${this.maxContextTokens}.`,
388-
);
392+
const info = `${latestMessage.tokenCount} / ${this.maxContextTokens}`;
393+
const errorMessage = `{ "type": "${ErrorTypes.INPUT_LENGTH}", "info": "${info}" }`;
394+
logger.warn(`Prompt token count exceeds max token count (${info}).`);
395+
throw new Error(errorMessage);
389396
}
390397

391398
if (usePrevSummary) {

client/src/components/Messages/Content/Error.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ type TExpiredKey = {
3333
endpoint: string;
3434
};
3535

36+
type TInputLength = {
37+
info: string;
38+
};
39+
3640
const errorMessages = {
3741
[ErrorTypes.MODERATION]: 'com_error_moderation',
3842
[ErrorTypes.NO_USER_KEY]: 'com_error_no_user_key',
@@ -42,6 +46,10 @@ const errorMessages = {
4246
const { expiredAt, endpoint } = json;
4347
return localize('com_error_expired_user_key', endpoint, expiredAt);
4448
},
49+
[ErrorTypes.INPUT_LENGTH]: (json: TInputLength, localize: LocalizeFunction) => {
50+
const { info } = json;
51+
return localize('com_error_input_length', info);
52+
},
4553
[ViolationTypes.BAN]:
4654
'Your account has been temporarily banned due to violations of our service.',
4755
invalid_api_key:

client/src/localization/languages/Eng.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default {
2525
com_error_invalid_user_key: 'Invalid key provided. Please provide a valid key and try again.',
2626
com_error_expired_user_key:
2727
'Provided key for {0} expired at {1}. Please provide a new key and try again.',
28+
com_error_input_length:
29+
'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.',
2830
com_files_no_results: 'No results.',
2931
com_files_filter: 'Filter files...',
3032
com_files_number_selected: '{0} of {1} file(s) selected',

packages/data-provider/src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,11 @@ export enum ErrorTypes {
839839
* Moderation error
840840
*/
841841
MODERATION = 'moderation',
842+
843+
/**
844+
* Prompt exceeds max length
845+
*/
846+
INPUT_LENGTH = 'INPUT_LENGTH',
842847
}
843848

844849
/**

0 commit comments

Comments
 (0)