From 4d8e9cfc61c550c7edb3c44a3257630063ae31eb Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Tue, 17 Dec 2024 09:12:15 -0500 Subject: [PATCH] refactor: Move OpenAI Base URL option to credentials (#12175) --- .../EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts | 11 +++++++++-- .../nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts | 14 +++++++++++--- .../OpenAi/actions/assistant/message.operation.ts | 9 ++++++++- .../vendors/OpenAi/actions/versionDescription.ts | 2 +- .../credentials/OpenAiApi.credentials.ts | 11 +++++++++-- packages/nodes-base/nodes/OpenAi/OpenAi.node.ts | 3 ++- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts b/packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts index fb5f67e30c21f..cd44cb114b870 100644 --- a/packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts @@ -79,7 +79,7 @@ export class EmbeddingsOpenAi implements INodeType { }, ], group: ['transform'], - version: [1, 1.1], + version: [1, 1.1, 1.2], description: 'Use Embeddings OpenAI', defaults: { name: 'Embeddings OpenAI', @@ -106,7 +106,7 @@ export class EmbeddingsOpenAi implements INodeType { requestDefaults: { ignoreHttpStatusErrors: true, baseURL: - '={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}', + '={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || $credentials.url?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}', }, properties: [ getConnectionHintNoticeField([NodeConnectionType.AiVectorStore]), @@ -171,6 +171,11 @@ export class EmbeddingsOpenAi implements INodeType { default: 'https://api.openai.com/v1', description: 'Override the default base URL for the API', type: 'string', + displayOptions: { + hide: { + '@version': [{ _cnd: { gte: 1.2 } }], + }, + }, }, { displayName: 'Batch Size', @@ -219,6 +224,8 @@ export class EmbeddingsOpenAi implements INodeType { const configuration: ClientOptions = {}; if (options.baseURL) { configuration.baseURL = options.baseURL; + } else if (credentials.url) { + configuration.baseURL = credentials.url as string; } const embeddings = new OpenAIEmbeddings( diff --git a/packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts b/packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts index f3dceb5dd1df6..cf24d944dea44 100644 --- a/packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts @@ -22,7 +22,7 @@ export class LmChatOpenAi implements INodeType { name: 'lmChatOpenAi', icon: { light: 'file:openAiLight.svg', dark: 'file:openAiLight.dark.svg' }, group: ['transform'], - version: 1, + version: [1, 1.1], description: 'For advanced usage with an AI chain', defaults: { name: 'OpenAI Chat Model', @@ -55,7 +55,7 @@ export class LmChatOpenAi implements INodeType { requestDefaults: { ignoreHttpStatusErrors: true, baseURL: - '={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}', + '={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || $credentials?.url?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}', }, properties: [ getConnectionHintNoticeField([NodeConnectionType.AiChain, NodeConnectionType.AiAgent]), @@ -82,7 +82,7 @@ export class LmChatOpenAi implements INodeType { routing: { request: { method: 'GET', - url: '={{ $parameter.options?.baseURL?.split("/").slice(-1).pop() || "v1" }}/models', + url: '={{ $parameter.options?.baseURL?.split("/").slice(-1).pop() || $credentials?.url?.split("/").slice(-1).pop() || "v1" }}/models', }, output: { postReceive: [ @@ -98,6 +98,7 @@ export class LmChatOpenAi implements INodeType { // If the baseURL is not set or is set to api.openai.com, include only chat models pass: `={{ ($parameter.options?.baseURL && !$parameter.options?.baseURL?.includes('api.openai.com')) || + ($credentials?.url && !$credentials.url.includes('api.openai.com')) || $responseItem.id.startsWith('ft:') || $responseItem.id.startsWith('o1') || ($responseItem.id.startsWith('gpt-') && !$responseItem.id.includes('instruct')) @@ -156,6 +157,11 @@ export class LmChatOpenAi implements INodeType { default: 'https://api.openai.com/v1', description: 'Override the default base URL for the API', type: 'string', + displayOptions: { + hide: { + '@version': [{ _cnd: { gte: 1.1 } }], + }, + }, }, { displayName: 'Frequency Penalty', @@ -261,6 +267,8 @@ export class LmChatOpenAi implements INodeType { const configuration: ClientOptions = {}; if (options.baseURL) { configuration.baseURL = options.baseURL; + } else if (credentials.url) { + configuration.baseURL = credentials.url as string; } const model = new ChatOpenAI({ diff --git a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/assistant/message.operation.ts b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/assistant/message.operation.ts index 977530cc830be..bc22ac948fe01 100644 --- a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/assistant/message.operation.ts +++ b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/actions/assistant/message.operation.ts @@ -106,6 +106,11 @@ const properties: INodeProperties[] = [ default: 'https://api.openai.com/v1', description: 'Override the default base URL for the API', type: 'string', + displayOptions: { + hide: { + '@version': [{ _cnd: { gte: 1.8 } }], + }, + }, }, { displayName: 'Max Retries', @@ -182,11 +187,13 @@ export async function execute(this: IExecuteFunctions, i: number): Promise