|
| 1 | +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' |
| 2 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 3 | +import { BedrockEmbeddings, BedrockEmbeddingsParams } from 'langchain/embeddings/bedrock' |
| 4 | +import { BedrockRuntimeClient, InvokeModelCommand } from '@aws-sdk/client-bedrock-runtime' |
| 5 | + |
| 6 | +class AWSBedrockEmbedding_Embeddings implements INode { |
| 7 | + label: string |
| 8 | + name: string |
| 9 | + version: number |
| 10 | + type: string |
| 11 | + icon: string |
| 12 | + category: string |
| 13 | + description: string |
| 14 | + baseClasses: string[] |
| 15 | + credential: INodeParams |
| 16 | + inputs: INodeParams[] |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.label = 'AWS Bedrock Embeddings' |
| 20 | + this.name = 'AWSBedrockEmbeddings' |
| 21 | + this.version = 1.0 |
| 22 | + this.type = 'AWSBedrockEmbeddings' |
| 23 | + this.icon = 'awsBedrock.png' |
| 24 | + this.category = 'Embeddings' |
| 25 | + this.description = 'AWSBedrock embedding models to generate embeddings for a given text' |
| 26 | + this.baseClasses = [this.type, ...getBaseClasses(BedrockEmbeddings)] |
| 27 | + this.credential = { |
| 28 | + label: 'AWS Credential', |
| 29 | + name: 'credential', |
| 30 | + type: 'credential', |
| 31 | + credentialNames: ['awsApi'], |
| 32 | + optional: true |
| 33 | + } |
| 34 | + this.inputs = [ |
| 35 | + { |
| 36 | + label: 'Region', |
| 37 | + name: 'region', |
| 38 | + type: 'options', |
| 39 | + options: [ |
| 40 | + { label: 'af-south-1', name: 'af-south-1' }, |
| 41 | + { label: 'ap-east-1', name: 'ap-east-1' }, |
| 42 | + { label: 'ap-northeast-1', name: 'ap-northeast-1' }, |
| 43 | + { label: 'ap-northeast-2', name: 'ap-northeast-2' }, |
| 44 | + { label: 'ap-northeast-3', name: 'ap-northeast-3' }, |
| 45 | + { label: 'ap-south-1', name: 'ap-south-1' }, |
| 46 | + { label: 'ap-south-2', name: 'ap-south-2' }, |
| 47 | + { label: 'ap-southeast-1', name: 'ap-southeast-1' }, |
| 48 | + { label: 'ap-southeast-2', name: 'ap-southeast-2' }, |
| 49 | + { label: 'ap-southeast-3', name: 'ap-southeast-3' }, |
| 50 | + { label: 'ap-southeast-4', name: 'ap-southeast-4' }, |
| 51 | + { label: 'ap-southeast-5', name: 'ap-southeast-5' }, |
| 52 | + { label: 'ap-southeast-6', name: 'ap-southeast-6' }, |
| 53 | + { label: 'ca-central-1', name: 'ca-central-1' }, |
| 54 | + { label: 'ca-west-1', name: 'ca-west-1' }, |
| 55 | + { label: 'cn-north-1', name: 'cn-north-1' }, |
| 56 | + { label: 'cn-northwest-1', name: 'cn-northwest-1' }, |
| 57 | + { label: 'eu-central-1', name: 'eu-central-1' }, |
| 58 | + { label: 'eu-central-2', name: 'eu-central-2' }, |
| 59 | + { label: 'eu-north-1', name: 'eu-north-1' }, |
| 60 | + { label: 'eu-south-1', name: 'eu-south-1' }, |
| 61 | + { label: 'eu-south-2', name: 'eu-south-2' }, |
| 62 | + { label: 'eu-west-1', name: 'eu-west-1' }, |
| 63 | + { label: 'eu-west-2', name: 'eu-west-2' }, |
| 64 | + { label: 'eu-west-3', name: 'eu-west-3' }, |
| 65 | + { label: 'il-central-1', name: 'il-central-1' }, |
| 66 | + { label: 'me-central-1', name: 'me-central-1' }, |
| 67 | + { label: 'me-south-1', name: 'me-south-1' }, |
| 68 | + { label: 'sa-east-1', name: 'sa-east-1' }, |
| 69 | + { label: 'us-east-1', name: 'us-east-1' }, |
| 70 | + { label: 'us-east-2', name: 'us-east-2' }, |
| 71 | + { label: 'us-gov-east-1', name: 'us-gov-east-1' }, |
| 72 | + { label: 'us-gov-west-1', name: 'us-gov-west-1' }, |
| 73 | + { label: 'us-west-1', name: 'us-west-1' }, |
| 74 | + { label: 'us-west-2', name: 'us-west-2' } |
| 75 | + ], |
| 76 | + default: 'us-east-1' |
| 77 | + }, |
| 78 | + { |
| 79 | + label: 'Model Name', |
| 80 | + name: 'model', |
| 81 | + type: 'options', |
| 82 | + options: [{ label: 'amazon.titan-embed-text-v1', name: 'amazon.titan-embed-text-v1' }], |
| 83 | + default: 'amazon.titan-embed-text-v1' |
| 84 | + } |
| 85 | + ] |
| 86 | + } |
| 87 | + |
| 88 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 89 | + const iRegion = nodeData.inputs?.region as string |
| 90 | + const iModel = nodeData.inputs?.model as string |
| 91 | + |
| 92 | + const obj: BedrockEmbeddingsParams = { |
| 93 | + model: iModel, |
| 94 | + region: iRegion |
| 95 | + } |
| 96 | + |
| 97 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 98 | + if (credentialData && Object.keys(credentialData).length !== 0) { |
| 99 | + const credentialApiKey = getCredentialParam('awsKey', credentialData, nodeData) |
| 100 | + const credentialApiSecret = getCredentialParam('awsSecret', credentialData, nodeData) |
| 101 | + const credentialApiSession = getCredentialParam('awsSession', credentialData, nodeData) |
| 102 | + |
| 103 | + obj.credentials = { |
| 104 | + accessKeyId: credentialApiKey, |
| 105 | + secretAccessKey: credentialApiSecret, |
| 106 | + sessionToken: credentialApiSession |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + const client = new BedrockRuntimeClient({ |
| 111 | + region: obj.region, |
| 112 | + credentials: obj.credentials |
| 113 | + }) |
| 114 | + |
| 115 | + const model = new BedrockEmbeddings(obj) |
| 116 | + |
| 117 | + // Avoid Illegal Invocation |
| 118 | + model.embedQuery = async (document: string): Promise<number[]> => { |
| 119 | + return await embedText(document, client, iModel) |
| 120 | + } |
| 121 | + |
| 122 | + model.embedDocuments = async (documents: string[]): Promise<number[][]> => { |
| 123 | + return Promise.all(documents.map((document) => embedText(document, client, iModel))) |
| 124 | + } |
| 125 | + return model |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +const embedText = async (text: string, client: BedrockRuntimeClient, model: string): Promise<number[]> => { |
| 130 | + // replace newlines, which can negatively affect performance. |
| 131 | + const cleanedText = text.replace(/\n/g, ' ') |
| 132 | + |
| 133 | + const res = await client.send( |
| 134 | + new InvokeModelCommand({ |
| 135 | + modelId: model, |
| 136 | + body: JSON.stringify({ |
| 137 | + inputText: cleanedText |
| 138 | + }), |
| 139 | + contentType: 'application/json', |
| 140 | + accept: 'application/json' |
| 141 | + }) |
| 142 | + ) |
| 143 | + |
| 144 | + try { |
| 145 | + const body = new TextDecoder().decode(res.body) |
| 146 | + return JSON.parse(body).embedding |
| 147 | + } catch (e) { |
| 148 | + throw new Error('An invalid response was returned by Bedrock.') |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +module.exports = { nodeClass: AWSBedrockEmbedding_Embeddings } |
0 commit comments