Files
n8n-enterprise-unlocked/packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/methods/loadModels.ts
कारतोफ्फेलस्क्रिप्ट™ d3bc80c22b fix(core): Do not use url.includes to check for domain names (#13802)
2025-03-19 12:29:00 +01:00

42 lines
1.2 KiB
TypeScript

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 url = baseURL && new URL(baseURL);
const isValidModel =
(url && url.hostname !== 'api.openai.com') ||
model.id.startsWith('ft:') ||
model.id.startsWith('o1') ||
model.id.startsWith('o3') ||
(model.id.startsWith('gpt-') && !model.id.includes('instruct'));
if (!filter) return isValidModel;
return isValidModel && model.id.toLowerCase().includes(filter.toLowerCase());
});
filteredModels.sort((a, b) => a.id.localeCompare(b.id));
const results = {
results: filteredModels.map((model: { id: string }) => ({
name: model.id,
value: model.id,
})),
};
return results;
}