LibreChat/packages/api/src/prompts/format.ts
Danny Avila 8bdc808074
Some checks are pending
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Waiting to run
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
refactor: Optimize & Standardize Tokenizer Usage (#10777)
* refactor: Token Limit Processing with Enhanced Efficiency

- Added a new test suite for `processTextWithTokenLimit`, ensuring comprehensive coverage of various scenarios including under, at, and exceeding token limits.
- Refactored the `processTextWithTokenLimit` function to utilize a ratio-based estimation method, significantly reducing the number of token counting function calls compared to the previous binary search approach.
- Improved handling of edge cases and variable token density, ensuring accurate truncation and performance across diverse text inputs.
- Included direct comparisons with the old implementation to validate correctness and efficiency improvements.

* refactor: Remove Tokenizer Route and Related References

- Deleted the tokenizer route from the server and removed its references from the routes index and server files, streamlining the API structure.
- This change simplifies the routing configuration by eliminating unused endpoints.

* refactor: Migrate countTokens Utility to API Module

- Removed the local countTokens utility and integrated it into the @librechat/api module for centralized access.
- Updated various files to reference the new countTokens import from the API module, ensuring consistent usage across the application.
- Cleaned up unused references and imports related to the previous countTokens implementation.

* refactor: Centralize escapeRegExp Utility in API Module

- Moved the escapeRegExp function from local utility files to the @librechat/api module for consistent usage across the application.
- Updated imports in various files to reference the new centralized escapeRegExp function, ensuring cleaner code and reducing redundancy.
- Removed duplicate implementations of escapeRegExp from multiple files, streamlining the codebase.

* refactor: Enhance Token Counting Flexibility in Text Processing

- Updated the `processTextWithTokenLimit` function to accept both synchronous and asynchronous token counting functions, improving its versatility.
- Introduced a new `TokenCountFn` type to define the token counting function signature.
- Added comprehensive tests to validate the behavior of `processTextWithTokenLimit` with both sync and async token counting functions, ensuring consistent results.
- Implemented a wrapper to track call counts for the `countTokens` function, optimizing performance and reducing unnecessary calls.
- Enhanced existing tests to compare the performance of the new implementation against the old one, demonstrating significant improvements in efficiency.

* chore: documentation for Truncation Safety Buffer in Token Processing

- Added a safety buffer multiplier to the character position estimates during text truncation to prevent overshooting token limits.
- Updated the `processTextWithTokenLimit` function to utilize the new `TRUNCATION_SAFETY_BUFFER` constant, enhancing the accuracy of token limit processing.
- Improved documentation to clarify the rationale behind the buffer and its impact on performance and efficiency in token counting.
2025-12-02 12:22:04 -05:00

153 lines
4.3 KiB
TypeScript

import { SystemCategories } from 'librechat-data-provider';
import type { IPromptGroupDocument as IPromptGroup } from '@librechat/data-schemas';
import type { Types } from 'mongoose';
import type { PromptGroupsListResponse } from '~/types';
import { escapeRegExp } from '~/utils/common';
/**
* Formats prompt groups for the paginated /groups endpoint response
*/
export function formatPromptGroupsResponse({
promptGroups = [],
pageNumber,
pageSize,
actualLimit,
hasMore = false,
after = null,
}: {
promptGroups: IPromptGroup[];
pageNumber?: string;
pageSize?: string;
actualLimit?: string | number;
hasMore?: boolean;
after?: string | null;
}): PromptGroupsListResponse {
const currentPage = parseInt(pageNumber || '1');
// Calculate total pages based on whether there are more results
// If hasMore is true, we know there's at least one more page
// We use a high number (9999) to indicate "many pages" since we don't know the exact count
const totalPages = hasMore ? '9999' : currentPage.toString();
return {
promptGroups,
pageNumber: pageNumber || '1',
pageSize: pageSize || String(actualLimit) || '10',
pages: totalPages,
has_more: hasMore,
after,
};
}
/**
* Creates an empty response for the paginated /groups endpoint
*/
export function createEmptyPromptGroupsResponse({
pageNumber,
pageSize,
actualLimit,
}: {
pageNumber?: string;
pageSize?: string;
actualLimit?: string | number;
}): PromptGroupsListResponse {
return {
promptGroups: [],
pageNumber: pageNumber || '1',
pageSize: pageSize || String(actualLimit) || '10',
pages: '0',
has_more: false,
after: null,
};
}
/**
* Marks prompt groups as public based on the publicly accessible IDs
*/
export function markPublicPromptGroups(
promptGroups: IPromptGroup[],
publiclyAccessibleIds: Types.ObjectId[],
): IPromptGroup[] {
if (!promptGroups.length) {
return [];
}
return promptGroups.map((group) => {
const isPublic = publiclyAccessibleIds.some((id) => id.equals(group._id?.toString()));
return isPublic ? ({ ...group, isPublic: true } as IPromptGroup) : group;
});
}
/**
* Builds filter object for prompt group queries
*/
export function buildPromptGroupFilter({
name,
category,
...otherFilters
}: {
name?: string;
category?: string;
[key: string]: string | number | boolean | RegExp | undefined;
}): {
filter: Record<string, string | number | boolean | RegExp | undefined>;
searchShared: boolean;
searchSharedOnly: boolean;
} {
const filter: Record<string, string | number | boolean | RegExp | undefined> = {
...otherFilters,
};
let searchShared = true;
let searchSharedOnly = false;
// Handle name filter - convert to regex for case-insensitive search
if (name) {
filter.name = new RegExp(escapeRegExp(name), 'i');
}
// Handle category filters with special system categories
if (category === SystemCategories.MY_PROMPTS) {
searchShared = false;
} else if (category === SystemCategories.NO_CATEGORY) {
filter.category = '';
} else if (category === SystemCategories.SHARED_PROMPTS) {
searchSharedOnly = true;
} else if (category) {
filter.category = category;
}
return { filter, searchShared, searchSharedOnly };
}
/**
* Filters accessible IDs based on shared/public prompts logic
*/
export async function filterAccessibleIdsBySharedLogic({
accessibleIds,
searchShared,
searchSharedOnly,
publicPromptGroupIds,
}: {
accessibleIds: Types.ObjectId[];
searchShared: boolean;
searchSharedOnly: boolean;
publicPromptGroupIds?: Types.ObjectId[];
}): Promise<Types.ObjectId[]> {
const publicIdStrings = new Set((publicPromptGroupIds || []).map((id) => id.toString()));
if (!searchShared) {
// For MY_PROMPTS - exclude public prompts to show only user's own prompts
return accessibleIds.filter((id) => !publicIdStrings.has(id.toString()));
}
if (searchSharedOnly) {
// Handle SHARED_PROMPTS filter - only return public prompts that user has access to
if (!publicPromptGroupIds?.length) {
return [];
}
const accessibleIdStrings = new Set(accessibleIds.map((id) => id.toString()));
return publicPromptGroupIds.filter((id) => accessibleIdStrings.has(id.toString()));
}
return [...accessibleIds, ...(publicPromptGroupIds || [])];
}