mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 00:54:06 -08:00
c2748802a2
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
169 lines
4.8 KiB
TypeScript
169 lines
4.8 KiB
TypeScript
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
|
|
import {
|
|
NodeConnectionType,
|
|
type IExecuteFunctions,
|
|
type INodeType,
|
|
type INodeTypeDescription,
|
|
type SupplyData,
|
|
} from 'n8n-workflow';
|
|
import { ChatBedrock } from 'langchain/chat_models/bedrock';
|
|
import { logWrapper } from '../../../utils/logWrapper';
|
|
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
|
|
// Dependencies needed underneath the hood. We add them
|
|
// here only to track where what dependency is used
|
|
import '@aws-sdk/credential-provider-node';
|
|
import '@aws-sdk/client-bedrock-runtime';
|
|
|
|
export class LmChatAwsBedrock implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'AWS Bedrock Chat Model',
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
|
|
name: 'lmChatAwsBedrock',
|
|
icon: 'file:bedrock.svg',
|
|
group: ['transform'],
|
|
version: 1,
|
|
description: 'Language Model AWS Bedrock',
|
|
defaults: {
|
|
name: 'AWS Bedrock Chat Model',
|
|
},
|
|
codex: {
|
|
categories: ['AI'],
|
|
subcategories: {
|
|
AI: ['Language Models'],
|
|
},
|
|
resources: {
|
|
primaryDocumentation: [
|
|
{
|
|
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatawsbedrock/',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
// 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.AiLanguageModel],
|
|
outputNames: ['Model'],
|
|
credentials: [
|
|
{
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-credentials-name-unsuffixed
|
|
name: 'aws',
|
|
required: true,
|
|
},
|
|
],
|
|
requestDefaults: {
|
|
ignoreHttpStatusErrors: true,
|
|
baseURL: '=https://bedrock.{{$credentials?.region ?? "eu-central-1"}}.amazonaws.com',
|
|
},
|
|
properties: [
|
|
getConnectionHintNoticeField([NodeConnectionType.AiChain, NodeConnectionType.AiChain]),
|
|
{
|
|
displayName: 'Model',
|
|
name: 'model',
|
|
type: 'options',
|
|
description:
|
|
'The model which will generate the completion. <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/foundation-models.html">Learn more</a>.',
|
|
typeOptions: {
|
|
loadOptions: {
|
|
routing: {
|
|
request: {
|
|
method: 'GET',
|
|
url: '/foundation-models?&byOutputModality=TEXT',
|
|
},
|
|
output: {
|
|
postReceive: [
|
|
{
|
|
type: 'rootProperty',
|
|
properties: {
|
|
property: 'modelSummaries',
|
|
},
|
|
},
|
|
{
|
|
type: 'filter',
|
|
properties: {
|
|
// Not a foundational model
|
|
pass: "={{ !['anthropic.claude-instant-v1-100k'].includes($responseItem.modelId) }}",
|
|
},
|
|
},
|
|
{
|
|
type: 'setKeyValue',
|
|
properties: {
|
|
name: '={{$responseItem.modelName}}',
|
|
description: '={{$responseItem.modelArn}}',
|
|
value: '={{$responseItem.modelId}}',
|
|
},
|
|
},
|
|
{
|
|
type: 'sort',
|
|
properties: {
|
|
key: 'name',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
routing: {
|
|
send: {
|
|
type: 'body',
|
|
property: 'model',
|
|
},
|
|
},
|
|
default: '',
|
|
},
|
|
{
|
|
displayName: 'Options',
|
|
name: 'options',
|
|
placeholder: 'Add Option',
|
|
description: 'Additional options to add',
|
|
type: 'collection',
|
|
default: {},
|
|
options: [
|
|
{
|
|
displayName: 'Maximum Number of Tokens',
|
|
name: 'maxTokensToSample',
|
|
default: 2000,
|
|
description: 'The maximum number of tokens to generate in the completion',
|
|
type: 'number',
|
|
},
|
|
{
|
|
displayName: 'Sampling Temperature',
|
|
name: 'temperature',
|
|
default: 0.7,
|
|
typeOptions: { maxValue: 1, minValue: 0, numberPrecision: 1 },
|
|
description:
|
|
'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
|
|
type: 'number',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
|
|
const credentials = await this.getCredentials('aws');
|
|
const modelName = this.getNodeParameter('model', itemIndex) as string;
|
|
const options = this.getNodeParameter('options', itemIndex, {}) as {
|
|
temperature: number;
|
|
maxTokensToSample: number;
|
|
};
|
|
|
|
const model = new ChatBedrock({
|
|
region: credentials.region as string,
|
|
model: modelName,
|
|
temperature: options.temperature,
|
|
maxTokens: options.maxTokensToSample,
|
|
credentials: {
|
|
secretAccessKey: credentials.secretAccessKey as string,
|
|
accessKeyId: credentials.accessKeyId as string,
|
|
sessionToken: credentials.sessionToken as string,
|
|
},
|
|
});
|
|
|
|
return {
|
|
response: logWrapper(model, this),
|
|
};
|
|
}
|
|
}
|