From 981ea3930e96c3b45267fa7ddac48710846e49ac Mon Sep 17 00:00:00 2001 From: oleg Date: Mon, 29 Jan 2024 16:34:23 +0100 Subject: [PATCH] feat: Add model parameter to OpenAI embeddings (#8481) Signed-off-by: Oleg Ivaniv --- .../EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) 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 8154559412a0f..edc4a48679557 100644 --- a/packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsOpenAI/EmbeddingsOpenAi.node.ts @@ -5,6 +5,7 @@ import { type INodeType, type INodeTypeDescription, type SupplyData, + type INodeProperties, } from 'n8n-workflow'; import type { ClientOptions } from 'openai'; @@ -12,6 +13,60 @@ import { OpenAIEmbeddings } from 'langchain/embeddings/openai'; import { logWrapper } from '../../../utils/logWrapper'; import { getConnectionHintNoticeField } from '../../../utils/sharedFields'; +const modelParameter: INodeProperties = { + displayName: 'Model', + name: 'model', + type: 'options', + description: + 'The model which will generate the embeddings. Learn more.', + typeOptions: { + loadOptions: { + routing: { + request: { + method: 'GET', + url: '={{ $parameter.options?.baseURL?.split("/").slice(-1).pop() || "v1" }}/models', + }, + output: { + postReceive: [ + { + type: 'rootProperty', + properties: { + property: 'data', + }, + }, + { + type: 'filter', + properties: { + pass: "={{ $responseItem.id.includes('embed') }}", + }, + }, + { + type: 'setKeyValue', + properties: { + name: '={{$responseItem.id}}', + value: '={{$responseItem.id}}', + }, + }, + { + type: 'sort', + properties: { + key: 'name', + }, + }, + ], + }, + }, + }, + }, + routing: { + send: { + type: 'body', + property: 'model', + }, + }, + default: 'text-embedding-3-small', +}; + export class EmbeddingsOpenAi implements INodeType { description: INodeTypeDescription = { displayName: 'Embeddings OpenAI', @@ -50,6 +105,23 @@ export class EmbeddingsOpenAi implements INodeType { outputNames: ['Embeddings'], properties: [ getConnectionHintNoticeField([NodeConnectionType.AiVectorStore]), + { + ...modelParameter, + default: 'text-embedding-ada-002', + displayOptions: { + show: { + '@version': [1], + }, + }, + }, + { + ...modelParameter, + displayOptions: { + hide: { + '@version': [1], + }, + }, + }, { displayName: 'Options', name: 'options', @@ -115,6 +187,7 @@ export class EmbeddingsOpenAi implements INodeType { const embeddings = new OpenAIEmbeddings( { + modelName: this.getNodeParameter('model', itemIndex, 'text-embedding-3-small') as string, openAIApiKey: credentials.apiKey as string, ...options, },