2023-11-29 03:13:55 -08:00
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
2024-09-25 02:21:54 -07:00
import { ChatBedrockConverse } from '@langchain/aws' ;
2023-11-29 03:13:55 -08:00
import {
NodeConnectionType ,
type INodeType ,
type INodeTypeDescription ,
2024-10-28 03:37:23 -07:00
type ISupplyDataFunctions ,
2023-11-29 03:13:55 -08:00
type SupplyData ,
} from 'n8n-workflow' ;
2024-09-25 02:21:54 -07:00
2023-11-29 03:13:55 -08:00
import { getConnectionHintNoticeField } from '../../../utils/sharedFields' ;
2024-05-12 12:12:07 -07:00
import { N8nLlmTracing } from '../N8nLlmTracing' ;
2023-11-29 03:13:55 -08:00
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 : {
2024-08-05 04:59:02 -07:00
AI : [ 'Language Models' , 'Root Nodes' ] ,
2024-07-23 07:40:28 -07:00
'Language Models' : [ 'Chat Models (Recommended)' ] ,
2023-11-29 03:13:55 -08:00
} ,
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' ,
2024-04-09 05:22:48 -07:00
url : '/foundation-models?&byOutputModality=TEXT&byInferenceType=ON_DEMAND' ,
2023-11-29 03:13:55 -08:00
} ,
output : {
postReceive : [
{
type : 'rootProperty' ,
properties : {
property : 'modelSummaries' ,
} ,
} ,
{
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' ,
} ,
] ,
} ,
] ,
} ;
2024-10-28 03:37:23 -07:00
async supplyData ( this : ISupplyDataFunctions , itemIndex : number ) : Promise < SupplyData > {
2023-11-29 03:13:55 -08:00
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 ;
} ;
2024-09-25 02:21:54 -07:00
const model = new ChatBedrockConverse ( {
2023-11-29 03:13:55 -08:00
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 ,
} ,
2024-05-12 12:12:07 -07:00
callbacks : [ new N8nLlmTracing ( this ) ] ,
2023-11-29 03:13:55 -08:00
} ) ;
return {
2024-05-12 12:12:07 -07:00
response : model ,
2023-11-29 03:13:55 -08:00
} ;
}
}