@n8n/n8n-nodes-langchain
Version:
151 lines • 6.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddingsAwsBedrock = void 0;
const ai_utilities_1 = require("@n8n/ai-utilities");
const createBedrockRuntimeClient_1 = require("../../../utils/aws/createBedrockRuntimeClient");
const resolveAwsCredentials_1 = require("../../../utils/aws/resolveAwsCredentials");
const resolveBedrockRegion_1 = require("../../../utils/aws/resolveBedrockRegion");
const utils_1 = require("n8n-nodes-base/dist/nodes/Aws/utils");
const n8n_workflow_1 = require("n8n-workflow");
const BedrockInvokeModelEmbeddings_1 = require("./BedrockInvokeModelEmbeddings");
const listModels_1 = require("./methods/listModels");
function isJsonObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
class EmbeddingsAwsBedrock {
constructor() {
this.description = {
displayName: 'Embeddings AWS Bedrock',
name: 'embeddingsAwsBedrock',
icon: 'file:bedrock.svg',
credentials: utils_1.awsNodeCredentials,
group: ['transform'],
version: 1,
description: 'Use Embeddings AWS Bedrock',
defaults: {
name: 'Embeddings AWS Bedrock',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Embeddings'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.embeddingsawsbedrock/',
},
],
},
},
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.AiEmbedding],
outputNames: ['Embeddings'],
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL: '=https://bedrock.{{$credentials?.region ?? "eu-central-1"}}.amazonaws.com',
},
properties: [
utils_1.awsNodeAuthOptions,
(0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiVectorStore]),
{
displayName: 'Model',
name: 'model',
type: 'options',
allowArbitraryValues: true,
description: 'The model or inference profile which will generate the embeddings. <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/foundation-models.html">Learn more</a>.',
typeOptions: {
loadOptionsDependsOn: ['authentication'],
loadOptionsMethod: 'listModels',
},
routing: {
send: {
type: 'body',
property: 'model',
},
},
default: '',
},
{
displayName: 'Options',
name: 'options',
placeholder: 'Add Option',
description: 'Additional options to add',
type: 'collection',
default: {},
options: [
{
displayName: 'Additional Model Request Fields',
name: 'additionalModelRequestFields',
default: '{}',
description: 'Model-specific request fields passed through as JSON (e.g. Titan <code>dimensions</code>/<code>normalize</code>, Cohere <code>input_type</code>/<code>truncate</code>). See the <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html">AWS model parameters docs</a>.',
type: 'json',
typeOptions: { rows: 4 },
},
{
displayName: 'Max Retries',
name: 'maxRetries',
default: 2,
description: 'Maximum number of retries to attempt when a request fails',
type: 'number',
},
{
displayName: 'Timeout',
name: 'timeout',
default: 60000,
description: 'Maximum amount of time a request is allowed to take in milliseconds. Set to 0 to disable.',
type: 'number',
},
],
},
],
};
this.methods = {
loadOptions: {
listModels: listModels_1.listModels,
},
};
}
async supplyData(itemIndex) {
const { region: credentialRegion, credentials, bedrockRuntimeEndpoint, } = await (0, resolveAwsCredentials_1.resolveAwsCredentials)(this, itemIndex);
const modelName = this.getNodeParameter('model', itemIndex);
const options = this.getNodeParameter('options', itemIndex, {});
const region = (0, resolveBedrockRegion_1.resolveBedrockRegion)(modelName, credentialRegion);
const client = (0, createBedrockRuntimeClient_1.createBedrockRuntimeClient)({
region,
credentials,
bedrockRuntimeEndpoint,
maxRetries: options.maxRetries,
timeout: options.timeout,
});
let additionalModelRequestFields;
const additionalFields = options.additionalModelRequestFields?.trim();
if (additionalFields && additionalFields !== '{}') {
let parsed;
try {
parsed = (0, n8n_workflow_1.jsonParse)(additionalFields);
}
catch {
throw new n8n_workflow_1.UserError('Additional Model Request Fields must be valid JSON', {
level: 'warning',
});
}
if (!isJsonObject(parsed)) {
throw new n8n_workflow_1.UserError('Additional Model Request Fields must be a JSON object', {
level: 'warning',
});
}
additionalModelRequestFields = parsed;
}
const embeddings = new BedrockInvokeModelEmbeddings_1.BedrockInvokeModelEmbeddings({
client,
model: modelName,
additionalModelRequestFields,
});
return {
response: (0, ai_utilities_1.logWrapper)(embeddings, this),
};
}
}
exports.EmbeddingsAwsBedrock = EmbeddingsAwsBedrock;
//# sourceMappingURL=EmbeddingsAwsBedrock.node.js.map