Skip to content

Commit dd6399b

Browse files
authored
community[patch]: support AWS_... env vars (langchain-ai#6866)
1 parent f0c5d02 commit dd6399b

File tree

4 files changed

+97
-25
lines changed

4 files changed

+97
-25
lines changed

libs/langchain-community/src/chat_models/bedrock/index.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export interface BedrockChatFields
2121
*
2222
* ```bash
2323
* npm install @langchain/openai
24-
* export BEDROCK_AWS_REGION="your-aws-region"
25-
* export BEDROCK_AWS_SECRET_ACCESS_KEY="your-aws-secret-access-key"
26-
* export BEDROCK_AWS_ACCESS_KEY_ID="your-aws-access-key-id"
24+
* export AWS_REGION="your-aws-region"
25+
* export AWS_SECRET_ACCESS_KEY="your-aws-secret-access-key"
26+
* export AWS_ACCESS_KEY_ID="your-aws-access-key-id"
2727
* ```
2828
*
2929
* ## [Constructor args](/classes/langchain_community_chat_models_bedrock.BedrockChat.html#constructor)
@@ -60,15 +60,21 @@ export interface BedrockChatFields
6060
* const llm = new BedrockChat({
6161
* region: process.env.BEDROCK_AWS_REGION,
6262
* maxRetries: 0,
63-
* credentials: {
64-
* secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
65-
* accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
66-
* },
6763
* model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
6864
* temperature: 0,
6965
* maxTokens: undefined,
7066
* // other params...
7167
* });
68+
*
69+
* // You can also pass credentials in explicitly:
70+
* const llmWithCredentials = new BedrockChat({
71+
* region: process.env.BEDROCK_AWS_REGION,
72+
* model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
73+
* credentials: {
74+
* secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
75+
* accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
76+
* },
77+
* });
7278
* ```
7379
* </details>
7480
*

libs/langchain-community/src/chat_models/bedrock/web.ts

+47-18
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ export interface BedrockChatFields
183183
*
184184
* ```bash
185185
* 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"
189189
* ```
190190
*
191191
* ## [Constructor args](/classes/langchain_community_chat_models_bedrock.BedrockChat.html#constructor)
@@ -217,20 +217,26 @@ export interface BedrockChatFields
217217
* <summary><strong>Instantiate</strong></summary>
218218
*
219219
* ```typescript
220-
* import { BedrockChat } from '@langchain/community/chat_models/bedrock';
220+
* import { BedrockChat } from '@langchain/community/chat_models/bedrock/web';
221221
*
222222
* const llm = new BedrockChat({
223-
* region: process.env.BEDROCK_AWS_REGION,
223+
* region: process.env.AWS_REGION,
224224
* maxRetries: 0,
225-
* credentials: {
226-
* secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
227-
* accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
228-
* },
229225
* model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
230226
* temperature: 0,
231227
* maxTokens: undefined,
232228
* // other params...
233229
* });
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+
* });
234240
* ```
235241
* </details>
236242
*
@@ -542,8 +548,12 @@ export class BedrockChat
542548

543549
get lc_secrets(): { [key: string]: string } | undefined {
544550
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",
547557
};
548558
}
549559

@@ -566,7 +576,32 @@ export class BedrockChat
566576
}
567577

568578
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);
570605

571606
this.model = fields?.model ?? this.model;
572607
this.modelProvider = getModelProvider(this.model);
@@ -585,12 +620,6 @@ export class BedrockChat
585620
}
586621
this.region = region;
587622

588-
const credentials = fields?.credentials;
589-
if (!credentials) {
590-
throw new Error(
591-
"Please set the AWS credentials in the 'credentials' field."
592-
);
593-
}
594623
this.credentials = credentials;
595624

596625
this.temperature = fields?.temperature ?? this.temperature;

libs/langchain-community/src/chat_models/tests/chatbedrock.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,34 @@ test("Test Bedrock identifying params", async () => {
2626
model,
2727
});
2828
});
29+
30+
test("Test Bedrock serialization", async () => {
31+
delete process.env.AWS_ACCESS_KEY_ID;
32+
delete process.env.AWS_SECRET_ACCESS_KEY;
33+
const bedrock = new BedrockChat({
34+
region: "us-west-2",
35+
model: "anthropic.claude-3-sonnet-20240229-v1:0",
36+
credentials: {
37+
accessKeyId: "unused",
38+
secretAccessKey: "unused",
39+
sessionToken: "unused",
40+
},
41+
});
42+
43+
expect(JSON.stringify(bedrock)).toEqual(
44+
`{"lc":1,"type":"constructor","id":["langchain","chat_models","bedrock","BedrockChat"],"kwargs":{"region_name":"us-west-2","model_id":"anthropic.claude-3-sonnet-20240229-v1:0","credentials":{"accessKeyId":{"lc":1,"type":"secret","id":["AWS_ACCESS_KEY_ID"]},"secretAccessKey":{"lc":1,"type":"secret","id":["AWS_SECRET_ACCESS_KEY"]},"sessionToken":{"lc":1,"type":"secret","id":["AWS_SECRET_ACCESS_KEY"]}}}}`
45+
);
46+
});
47+
48+
test("Test Bedrock serialization from environment variables", async () => {
49+
process.env.AWS_ACCESS_KEY_ID = "foo";
50+
process.env.AWS_SECRET_ACCESS_KEY = "bar";
51+
const bedrock = new BedrockChat({
52+
region: "us-west-2",
53+
model: "anthropic.claude-3-sonnet-20240229-v1:0",
54+
});
55+
56+
expect(JSON.stringify(bedrock)).toEqual(
57+
`{"lc":1,"type":"constructor","id":["langchain","chat_models","bedrock","BedrockChat"],"kwargs":{"region_name":"us-west-2","model_id":"anthropic.claude-3-sonnet-20240229-v1:0","aws_access_key_id":{"lc":1,"type":"secret","id":["AWS_ACCESS_KEY_ID"]},"aws_secret_access_key":{"lc":1,"type":"secret","id":["AWS_SECRET_ACCESS_KEY"]},"credentials":{"accessKeyId":{"lc":1,"type":"secret","id":["AWS_ACCESS_KEY_ID"]},"secretAccessKey":{"lc":1,"type":"secret","id":["AWS_SECRET_ACCESS_KEY"]}}}}`
58+
);
59+
});

libs/langchain-community/src/utils/bedrock/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ export interface BaseBedrockInput {
168168
tagSuffix: string;
169169
streamProcessingMode: "SYNCHRONOUS" | "ASYNCHRONOUS";
170170
};
171+
172+
awsAccessKeyId?: string;
173+
174+
awsSecretAccessKey?: string;
175+
176+
awsSessionToken?: string;
171177
}
172178

173179
type Dict = { [key: string]: unknown };

0 commit comments

Comments
 (0)