mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
fix(OpenAI Chat Model Node): Fix loading of custom models when using custom credential URL (#12634)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import type { ILoadOptionsFunctions, INodeListSearchResult } from 'n8n-workflow';
|
||||
import OpenAI from 'openai';
|
||||
|
||||
export async function searchModels(
|
||||
this: ILoadOptionsFunctions,
|
||||
filter?: string,
|
||||
): Promise<INodeListSearchResult> {
|
||||
const credentials = await this.getCredentials('openAiApi');
|
||||
const baseURL =
|
||||
(this.getNodeParameter('options.baseURL', '') as string) ||
|
||||
(credentials.url as string) ||
|
||||
'https://api.openai.com/v1';
|
||||
|
||||
const openai = new OpenAI({ baseURL, apiKey: credentials.apiKey as string });
|
||||
const { data: models = [] } = await openai.models.list();
|
||||
|
||||
const filteredModels = models.filter((model: { id: string }) => {
|
||||
const isValidModel =
|
||||
(baseURL && !baseURL.includes('api.openai.com')) ||
|
||||
model.id.startsWith('ft:') ||
|
||||
model.id.startsWith('o1') ||
|
||||
(model.id.startsWith('gpt-') && !model.id.includes('instruct'));
|
||||
|
||||
if (!filter) return isValidModel;
|
||||
|
||||
return isValidModel && model.id.toLowerCase().includes(filter.toLowerCase());
|
||||
});
|
||||
|
||||
const results = {
|
||||
results: filteredModels.map((model: { id: string }) => ({
|
||||
name: model.id,
|
||||
value: model.id,
|
||||
})),
|
||||
};
|
||||
|
||||
return results;
|
||||
}
|
||||
Reference in New Issue
Block a user