2023-11-29 03:13:55 -08:00
|
|
|
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
|
|
|
|
import {
|
|
|
|
NodeConnectionType,
|
|
|
|
type IExecuteFunctions,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
|
|
|
type SupplyData,
|
2024-01-29 07:34:23 -08:00
|
|
|
type INodeProperties,
|
2023-11-29 03:13:55 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import type { ClientOptions } from 'openai';
|
2024-03-07 02:36:36 -08:00
|
|
|
import { OpenAIEmbeddings } from '@langchain/openai';
|
2023-11-29 03:13:55 -08:00
|
|
|
import { logWrapper } from '../../../utils/logWrapper';
|
|
|
|
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
|
|
|
|
|
2024-01-29 07:34:23 -08:00
|
|
|
const modelParameter: INodeProperties = {
|
|
|
|
displayName: 'Model',
|
|
|
|
name: 'model',
|
|
|
|
type: 'options',
|
|
|
|
description:
|
|
|
|
'The model which will generate the embeddings. <a href="https://platform.openai.com/docs/models/overview">Learn more</a>.',
|
|
|
|
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',
|
|
|
|
};
|
|
|
|
|
2023-11-29 03:13:55 -08:00
|
|
|
export class EmbeddingsOpenAi implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Embeddings OpenAI',
|
|
|
|
name: 'embeddingsOpenAi',
|
2024-06-06 04:34:30 -07:00
|
|
|
icon: { light: 'file:openAiLight.svg', dark: 'file:openAiLight.dark.svg' },
|
2023-11-29 03:13:55 -08:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'openAiApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Use Embeddings OpenAI',
|
|
|
|
defaults: {
|
|
|
|
name: 'Embeddings OpenAI',
|
|
|
|
},
|
|
|
|
|
|
|
|
codex: {
|
|
|
|
categories: ['AI'],
|
|
|
|
subcategories: {
|
|
|
|
AI: ['Embeddings'],
|
|
|
|
},
|
|
|
|
resources: {
|
|
|
|
primaryDocumentation: [
|
|
|
|
{
|
|
|
|
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.embeddingsopenai/',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
|
|
|
|
inputs: [],
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
|
|
|
|
outputs: [NodeConnectionType.AiEmbedding],
|
|
|
|
outputNames: ['Embeddings'],
|
2024-02-02 03:27:13 -08:00
|
|
|
requestDefaults: {
|
|
|
|
ignoreHttpStatusErrors: true,
|
|
|
|
baseURL:
|
|
|
|
'={{ $parameter.options?.baseURL?.split("/").slice(0,-1).join("/") || "https://api.openai.com" }}',
|
|
|
|
},
|
2023-11-29 03:13:55 -08:00
|
|
|
properties: [
|
|
|
|
getConnectionHintNoticeField([NodeConnectionType.AiVectorStore]),
|
2024-01-29 07:34:23 -08:00
|
|
|
{
|
|
|
|
...modelParameter,
|
|
|
|
default: 'text-embedding-ada-002',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...modelParameter,
|
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
|
|
|
'@version': [1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-29 03:13:55 -08:00
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
description: 'Additional options to add',
|
|
|
|
type: 'collection',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Base URL',
|
|
|
|
name: 'baseURL',
|
|
|
|
default: 'https://api.openai.com/v1',
|
|
|
|
description: 'Override the default base URL for the API',
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Batch Size',
|
|
|
|
name: 'batchSize',
|
|
|
|
default: 512,
|
|
|
|
typeOptions: { maxValue: 2048 },
|
|
|
|
description: 'Maximum number of documents to send in each request',
|
|
|
|
type: 'number',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Strip New Lines',
|
|
|
|
name: 'stripNewLines',
|
|
|
|
default: true,
|
|
|
|
description: 'Whether to strip new lines from the input text',
|
|
|
|
type: 'boolean',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Timeout',
|
|
|
|
name: 'timeout',
|
|
|
|
default: -1,
|
|
|
|
description:
|
|
|
|
'Maximum amount of time a request is allowed to take in seconds. Set to -1 for no timeout.',
|
|
|
|
type: 'number',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
|
2024-08-28 00:32:53 -07:00
|
|
|
this.logger.debug('Supply data for embeddings');
|
2023-11-29 03:13:55 -08:00
|
|
|
const credentials = await this.getCredentials('openAiApi');
|
|
|
|
|
|
|
|
const options = this.getNodeParameter('options', itemIndex, {}) as {
|
|
|
|
baseURL?: string;
|
|
|
|
batchSize?: number;
|
|
|
|
stripNewLines?: boolean;
|
|
|
|
timeout?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.timeout === -1) {
|
|
|
|
options.timeout = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const configuration: ClientOptions = {};
|
|
|
|
if (options.baseURL) {
|
|
|
|
configuration.baseURL = options.baseURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const embeddings = new OpenAIEmbeddings(
|
|
|
|
{
|
2024-01-29 07:34:23 -08:00
|
|
|
modelName: this.getNodeParameter('model', itemIndex, 'text-embedding-3-small') as string,
|
2023-11-29 03:13:55 -08:00
|
|
|
openAIApiKey: credentials.apiKey as string,
|
|
|
|
...options,
|
|
|
|
},
|
|
|
|
configuration,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
response: logWrapper(embeddings, this),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|