@@ -183,9 +183,9 @@ export interface BedrockChatFields
183
183
*
184
184
* ```bash
185
185
* npm install @langchain/openai
186
- * export BEDROCK_AWS_REGION ="your-aws-region"
187
- * export BEDROCK_AWS_SECRET_ACCESS_KEY ="your-aws-secret-access-key"
188
- * export BEDROCK_AWS_ACCESS_KEY_ID ="your-aws-access-key-id"
186
+ * export AWS_REGION ="your-aws-region"
187
+ * export AWS_SECRET_ACCESS_KEY ="your-aws-secret-access-key"
188
+ * export AWS_ACCESS_KEY_ID ="your-aws-access-key-id"
189
189
* ```
190
190
*
191
191
* ## [Constructor args](/classes/langchain_community_chat_models_bedrock.BedrockChat.html#constructor)
@@ -217,20 +217,26 @@ export interface BedrockChatFields
217
217
* <summary><strong>Instantiate</strong></summary>
218
218
*
219
219
* ```typescript
220
- * import { BedrockChat } from '@langchain/community/chat_models/bedrock';
220
+ * import { BedrockChat } from '@langchain/community/chat_models/bedrock/web ';
221
221
*
222
222
* const llm = new BedrockChat({
223
- * region: process.env.BEDROCK_AWS_REGION ,
223
+ * region: process.env.AWS_REGION ,
224
224
* maxRetries: 0,
225
- * credentials: {
226
- * secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
227
- * accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
228
- * },
229
225
* model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
230
226
* temperature: 0,
231
227
* maxTokens: undefined,
232
228
* // other params...
233
229
* });
230
+ *
231
+ * // You can also pass credentials in explicitly:
232
+ * const llmWithCredentials = new BedrockChat({
233
+ * region: process.env.BEDROCK_AWS_REGION,
234
+ * model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
235
+ * credentials: {
236
+ * secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
237
+ * accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
238
+ * },
239
+ * });
234
240
* ```
235
241
* </details>
236
242
*
@@ -542,8 +548,12 @@ export class BedrockChat
542
548
543
549
get lc_secrets ( ) : { [ key : string ] : string } | undefined {
544
550
return {
545
- "credentials.accessKeyId" : "BEDROCK_AWS_ACCESS_KEY_ID" ,
546
- "credentials.secretAccessKey" : "BEDROCK_AWS_SECRET_ACCESS_KEY" ,
551
+ "credentials.accessKeyId" : "AWS_ACCESS_KEY_ID" ,
552
+ "credentials.secretAccessKey" : "AWS_SECRET_ACCESS_KEY" ,
553
+ "credentials.sessionToken" : "AWS_SECRET_ACCESS_KEY" ,
554
+ awsAccessKeyId : "AWS_ACCESS_KEY_ID" ,
555
+ awsSecretAccessKey : "AWS_SECRET_ACCESS_KEY" ,
556
+ awsSessionToken : "AWS_SESSION_TOKEN" ,
547
557
} ;
548
558
}
549
559
@@ -566,7 +576,32 @@ export class BedrockChat
566
576
}
567
577
568
578
constructor ( fields ?: BedrockChatFields ) {
569
- super ( fields ?? { } ) ;
579
+ const awsAccessKeyId =
580
+ fields ?. awsAccessKeyId ?? getEnvironmentVariable ( "AWS_ACCESS_KEY_ID" ) ;
581
+ const awsSecretAccessKey =
582
+ fields ?. awsSecretAccessKey ??
583
+ getEnvironmentVariable ( "AWS_SECRET_ACCESS_KEY" ) ;
584
+ const awsSessionToken =
585
+ fields ?. awsSessionToken ?? getEnvironmentVariable ( "AWS_SESSION_TOKEN" ) ;
586
+
587
+ let credentials = fields ?. credentials ;
588
+ if ( credentials === undefined ) {
589
+ if ( awsAccessKeyId === undefined || awsSecretAccessKey === undefined ) {
590
+ throw new Error (
591
+ "Please set your AWS credentials in the 'credentials' field or set env vars AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, and optionally AWS_SESSION_TOKEN."
592
+ ) ;
593
+ }
594
+ credentials = {
595
+ accessKeyId : awsAccessKeyId ,
596
+ secretAccessKey : awsSecretAccessKey ,
597
+ sessionToken : awsSessionToken ,
598
+ } ;
599
+ }
600
+
601
+ // eslint-disable-next-line no-param-reassign
602
+ fields = { ...fields , awsAccessKeyId, awsSecretAccessKey, awsSessionToken } ;
603
+
604
+ super ( fields ) ;
570
605
571
606
this . model = fields ?. model ?? this . model ;
572
607
this . modelProvider = getModelProvider ( this . model ) ;
@@ -585,12 +620,6 @@ export class BedrockChat
585
620
}
586
621
this . region = region ;
587
622
588
- const credentials = fields ?. credentials ;
589
- if ( ! credentials ) {
590
- throw new Error (
591
- "Please set the AWS credentials in the 'credentials' field."
592
- ) ;
593
- }
594
623
this . credentials = credentials ;
595
624
596
625
this . temperature = fields ?. temperature ?? this . temperature ;
0 commit comments