mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-11 18:42:36 +01:00
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
* chore: Update dependencies by adding ai-tokenizer and removing tiktoken - Added ai-tokenizer version 1.0.6 to package.json and package-lock.json across multiple packages. - Removed tiktoken version 1.0.15 from package.json and package-lock.json in the same locations, streamlining dependency management. * refactor: replace js-tiktoken with ai-tokenizer - Added support for 'claude' encoding in the AgentClient class to improve model compatibility. - Updated Tokenizer class to utilize 'ai-tokenizer' for both 'o200k_base' and 'claude' encodings, replacing the previous 'tiktoken' dependency. - Refactored tests to reflect changes in tokenizer behavior and ensure accurate token counting for both encoding types. - Removed deprecated references to 'tiktoken' and adjusted related tests for improved clarity and functionality. * chore: remove tiktoken mocks from DALLE3 tests - Eliminated mock implementations of 'tiktoken' from DALLE3-related test files to streamline test setup and align with recent dependency updates. - Adjusted related test structures to ensure compatibility with the new tokenizer implementation. * chore: Add distinct encoding support for Anthropic Claude models - Introduced a new method `getEncoding` in the AgentClient class to handle the specific BPE tokenizer for Claude models, ensuring compatibility with the distinct encoding requirements. - Updated documentation to clarify the encoding logic for Claude and other models. * docs: Update return type documentation for getEncoding method in AgentClient - Clarified the return type of the getEncoding method to specify that it can return an EncodingName or undefined, enhancing code readability and type safety. * refactor: Tokenizer class and error handling - Exported the EncodingName type for broader usage. - Renamed encodingMap to encodingData for clarity. - Improved error handling in getTokenCount method to ensure recovery attempts are logged and return 0 on failure. - Updated countTokens function documentation to specify the use of 'o200k_base' encoding. * refactor: Simplify encoding documentation and export type - Updated the getEncoding method documentation to clarify the default behavior for non-Anthropic Claude models. - Exported the EncodingName type separately from the Tokenizer module for improved clarity and usage. * test: Update text processing tests for token limits - Adjusted test cases to handle smaller text sizes, changing scenarios from ~120k tokens to ~20k tokens for both the real tokenizer and countTokens functions. - Updated token limits in tests to reflect new constraints, ensuring tests accurately assess performance and call reduction. - Enhanced console log messages for clarity regarding token counts and reductions in the updated scenarios. * refactor: Update Tokenizer imports and exports - Moved Tokenizer and countTokens exports to the tokenizer module for better organization. - Adjusted imports in memory.ts to reflect the new structure, ensuring consistent usage across the codebase. - Updated memory.test.ts to mock the Tokenizer from the correct module path, enhancing test accuracy. * refactor: Tokenizer initialization and error handling - Introduced an async `initEncoding` method to preload tokenizers, improving performance and accuracy in token counting. - Updated `getTokenCount` to handle uninitialized tokenizers more gracefully, ensuring proper recovery and logging on errors. - Removed deprecated synchronous tokenizer retrieval, streamlining the overall tokenizer management process. * test: Enhance tokenizer tests with initialization and encoding checks - Added `beforeAll` hooks to initialize tokenizers for 'o200k_base' and 'claude' encodings before running tests, ensuring proper setup. - Updated tests to validate the loading of encodings and the correctness of token counts for both 'o200k_base' and 'claude'. - Improved test structure to deduplicate concurrent initialization calls, enhancing performance and reliability.
207 lines
6 KiB
JavaScript
207 lines
6 KiB
JavaScript
const OpenAI = require('openai');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const DALLE3 = require('../DALLE3');
|
|
|
|
jest.mock('openai');
|
|
jest.mock('@librechat/data-schemas', () => {
|
|
return {
|
|
logger: {
|
|
info: jest.fn(),
|
|
warn: jest.fn(),
|
|
debug: jest.fn(),
|
|
error: jest.fn(),
|
|
},
|
|
};
|
|
});
|
|
|
|
const processFileURL = jest.fn();
|
|
|
|
const generate = jest.fn();
|
|
OpenAI.mockImplementation(() => ({
|
|
images: {
|
|
generate,
|
|
},
|
|
}));
|
|
|
|
jest.mock('fs', () => {
|
|
return {
|
|
existsSync: jest.fn(),
|
|
mkdirSync: jest.fn(),
|
|
promises: {
|
|
writeFile: jest.fn(),
|
|
readFile: jest.fn(),
|
|
unlink: jest.fn(),
|
|
},
|
|
};
|
|
});
|
|
|
|
jest.mock('path', () => {
|
|
return {
|
|
resolve: jest.fn(),
|
|
join: jest.fn(),
|
|
relative: jest.fn(),
|
|
extname: jest.fn().mockImplementation((filename) => {
|
|
return filename.slice(filename.lastIndexOf('.'));
|
|
}),
|
|
};
|
|
});
|
|
|
|
describe('DALLE3', () => {
|
|
let originalEnv;
|
|
let dalle; // Keep this declaration if you need to use dalle in other tests
|
|
const mockApiKey = 'mock_api_key';
|
|
|
|
beforeAll(() => {
|
|
// Save the original process.env
|
|
originalEnv = { ...process.env };
|
|
});
|
|
|
|
beforeEach(() => {
|
|
// Reset the process.env before each test
|
|
jest.resetModules();
|
|
process.env = { ...originalEnv, DALLE_API_KEY: mockApiKey };
|
|
// Instantiate DALLE3 for tests that do not depend on DALLE3_SYSTEM_PROMPT
|
|
dalle = new DALLE3({ processFileURL });
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
// Restore the original process.env after each test
|
|
process.env = originalEnv;
|
|
});
|
|
|
|
it('should throw an error if all potential API keys are missing', () => {
|
|
delete process.env.DALLE3_API_KEY;
|
|
delete process.env.DALLE_API_KEY;
|
|
expect(() => new DALLE3()).toThrow('Missing DALLE_API_KEY environment variable.');
|
|
});
|
|
|
|
it('should replace unwanted characters in input string', () => {
|
|
const input = 'This is a test\nstring with "quotes" and new lines.';
|
|
const expectedOutput = 'This is a test string with quotes and new lines.';
|
|
expect(dalle.replaceUnwantedChars(input)).toBe(expectedOutput);
|
|
});
|
|
|
|
it('should generate markdown image URL correctly', () => {
|
|
const imageName = 'test.png';
|
|
const markdownImage = dalle.wrapInMarkdown(imageName);
|
|
expect(markdownImage).toBe('');
|
|
});
|
|
|
|
it('should call OpenAI API with correct parameters', async () => {
|
|
const mockData = {
|
|
prompt: 'A test prompt',
|
|
quality: 'standard',
|
|
size: '1024x1024',
|
|
style: 'vivid',
|
|
};
|
|
|
|
const mockResponse = {
|
|
data: [
|
|
{
|
|
url: 'http://example.com/img-test.png',
|
|
},
|
|
],
|
|
};
|
|
|
|
generate.mockResolvedValue(mockResponse);
|
|
processFileURL.mockResolvedValue({
|
|
filepath: 'http://example.com/img-test.png',
|
|
});
|
|
|
|
const result = await dalle._call(mockData);
|
|
|
|
expect(generate).toHaveBeenCalledWith({
|
|
model: 'dall-e-3',
|
|
quality: mockData.quality,
|
|
style: mockData.style,
|
|
size: mockData.size,
|
|
prompt: mockData.prompt,
|
|
n: 1,
|
|
});
|
|
|
|
expect(result).toContain('![generated image]');
|
|
});
|
|
|
|
it('should use the system prompt if provided', () => {
|
|
process.env.DALLE3_SYSTEM_PROMPT = 'System prompt for testing';
|
|
jest.resetModules(); // This will ensure the module is fresh and will read the new env var
|
|
const DALLE3 = require('../DALLE3'); // Re-require after setting the env var
|
|
const dalleWithSystemPrompt = new DALLE3();
|
|
expect(dalleWithSystemPrompt.description_for_model).toBe('System prompt for testing');
|
|
});
|
|
|
|
it('should not use the system prompt if not provided', async () => {
|
|
delete process.env.DALLE3_SYSTEM_PROMPT;
|
|
const dalleWithoutSystemPrompt = new DALLE3();
|
|
expect(dalleWithoutSystemPrompt.description_for_model).not.toBe('System prompt for testing');
|
|
});
|
|
|
|
it('should throw an error if prompt is missing', async () => {
|
|
const mockData = {
|
|
quality: 'standard',
|
|
size: '1024x1024',
|
|
style: 'vivid',
|
|
};
|
|
await expect(dalle._call(mockData)).rejects.toThrow('Missing required field: prompt');
|
|
});
|
|
|
|
it('should log appropriate debug values', async () => {
|
|
const mockData = {
|
|
prompt: 'A test prompt',
|
|
};
|
|
const mockResponse = {
|
|
data: [
|
|
{
|
|
url: 'http://example.com/invalid-url',
|
|
},
|
|
],
|
|
};
|
|
|
|
generate.mockResolvedValue(mockResponse);
|
|
await dalle._call(mockData);
|
|
expect(logger.debug).toHaveBeenCalledWith('[DALL-E-3]', {
|
|
data: { url: 'http://example.com/invalid-url' },
|
|
theImageUrl: 'http://example.com/invalid-url',
|
|
extension: expect.any(String),
|
|
imageBasename: expect.any(String),
|
|
imageExt: expect.any(String),
|
|
imageName: expect.any(String),
|
|
});
|
|
});
|
|
|
|
it('should log an error and return the image URL if there is an error saving the image', async () => {
|
|
const mockData = {
|
|
prompt: 'A test prompt',
|
|
};
|
|
const mockResponse = {
|
|
data: [
|
|
{
|
|
url: 'http://example.com/img-test.png',
|
|
},
|
|
],
|
|
};
|
|
const error = new Error('Error while saving the image');
|
|
generate.mockResolvedValue(mockResponse);
|
|
processFileURL.mockRejectedValue(error);
|
|
const result = await dalle._call(mockData);
|
|
expect(logger.error).toHaveBeenCalledWith('Error while saving the image:', error);
|
|
expect(result).toBe('Failed to save the image locally. Error while saving the image');
|
|
});
|
|
|
|
it('should handle error when saving image to Firebase Storage fails', async () => {
|
|
const mockData = {
|
|
prompt: 'A test prompt',
|
|
};
|
|
const mockImageUrl = 'http://example.com/img-test.png';
|
|
const mockResponse = { data: [{ url: mockImageUrl }] };
|
|
const error = new Error('Error while saving to Firebase');
|
|
generate.mockResolvedValue(mockResponse);
|
|
processFileURL.mockRejectedValue(error);
|
|
|
|
const result = await dalle._call(mockData);
|
|
|
|
expect(logger.error).toHaveBeenCalledWith('Error while saving the image:', error);
|
|
expect(result).toContain('Failed to save the image');
|
|
});
|
|
});
|