Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚨 feat: Implement INPUT_LENGTH Error Type #3866

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions api/app/clients/BaseClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const crypto = require('crypto');
const fetch = require('node-fetch');
const { supportsBalanceCheck, Constants, CacheKeys, Time } = require('librechat-data-provider');
const {
supportsBalanceCheck,
ErrorTypes,
Constants,
CacheKeys,
Time,
} = require('librechat-data-provider');
const { getMessages, saveMessage, updateMessage, saveConvo } = require('~/models');
const { addSpaceIfNeeded, isEnabled } = require('~/server/utils');
const checkBalance = require('~/models/checkBalance');
Expand Down Expand Up @@ -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.INPUT_LENGTH}", "info": "${info}" }`;
logger.warn(`Prompt token count exceeds max token count (${info}).`);
throw new Error(errorMessage);
}

if (usePrevSummary) {
Expand Down
8 changes: 8 additions & 0 deletions client/src/components/Messages/Content/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type TExpiredKey = {
endpoint: string;
};

type TInputLength = {
info: string;
};

const errorMessages = {
[ErrorTypes.MODERATION]: 'com_error_moderation',
[ErrorTypes.NO_USER_KEY]: 'com_error_no_user_key',
Expand All @@ -42,6 +46,10 @@ const errorMessages = {
const { expiredAt, endpoint } = json;
return localize('com_error_expired_user_key', endpoint, expiredAt);
},
[ErrorTypes.INPUT_LENGTH]: (json: TInputLength, localize: LocalizeFunction) => {
const { info } = json;
return localize('com_error_input_length', info);
},
[ViolationTypes.BAN]:
'Your account has been temporarily banned due to violations of our service.',
invalid_api_key:
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 @@ -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_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...',
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
*/
INPUT_LENGTH = 'INPUT_LENGTH',
}

/**
Expand Down
Loading