mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
fix(OpenAI Chat Model Node): Sort models alphabetically (#13909)
This commit is contained in:
@@ -94,9 +94,9 @@ describe('searchModels', () => {
|
|||||||
const result = await searchModels.call(mockContext, 'gpt');
|
const result = await searchModels.call(mockContext, 'gpt');
|
||||||
|
|
||||||
expect(result.results).toEqual([
|
expect(result.results).toEqual([
|
||||||
{ name: 'gpt-4', value: 'gpt-4' },
|
|
||||||
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
|
|
||||||
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
|
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
|
||||||
|
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
|
||||||
|
{ name: 'gpt-4', value: 'gpt-4' },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -104,9 +104,43 @@ describe('searchModels', () => {
|
|||||||
const result = await searchModels.call(mockContext, 'GPT');
|
const result = await searchModels.call(mockContext, 'GPT');
|
||||||
|
|
||||||
expect(result.results).toEqual([
|
expect(result.results).toEqual([
|
||||||
{ name: 'gpt-4', value: 'gpt-4' },
|
|
||||||
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
|
|
||||||
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
|
{ name: 'ft:gpt-3.5-turbo', value: 'ft:gpt-3.5-turbo' },
|
||||||
|
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
|
||||||
|
{ name: 'gpt-4', value: 'gpt-4' },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return models sorted alphabetically by id', async () => {
|
||||||
|
// Setup a mock with scrambled order
|
||||||
|
const mockUnsortedInstance = {
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
models: {
|
||||||
|
list: jest.fn().mockResolvedValue({
|
||||||
|
data: [
|
||||||
|
{ id: 'gpt-4' },
|
||||||
|
{ id: 'a-model' },
|
||||||
|
{ id: 'o1-model' },
|
||||||
|
{ id: 'gpt-3.5-turbo' },
|
||||||
|
{ id: 'z-model' },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
} as unknown as OpenAI;
|
||||||
|
|
||||||
|
(OpenAI as jest.MockedClass<typeof OpenAI>).mockImplementation(() => mockUnsortedInstance);
|
||||||
|
|
||||||
|
// Custom API endpoint to include all models
|
||||||
|
mockContext.getNodeParameter = jest.fn().mockReturnValue('https://custom-api.com');
|
||||||
|
|
||||||
|
const result = await searchModels.call(mockContext);
|
||||||
|
|
||||||
|
// Verify the results are sorted alphabetically
|
||||||
|
expect(result.results).toEqual([
|
||||||
|
{ name: 'a-model', value: 'a-model' },
|
||||||
|
{ name: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' },
|
||||||
|
{ name: 'gpt-4', value: 'gpt-4' },
|
||||||
|
{ name: 'o1-model', value: 'o1-model' },
|
||||||
|
{ name: 'z-model', value: 'z-model' },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ export async function searchModels(
|
|||||||
return isValidModel && model.id.toLowerCase().includes(filter.toLowerCase());
|
return isValidModel && model.id.toLowerCase().includes(filter.toLowerCase());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
filteredModels.sort((a, b) => a.id.localeCompare(b.id));
|
||||||
|
|
||||||
const results = {
|
const results = {
|
||||||
results: filteredModels.map((model: { id: string }) => ({
|
results: filteredModels.map((model: { id: string }) => ({
|
||||||
name: model.id,
|
name: model.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user