Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community[patch]: support AWS_... env vars #6866

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes
  • Loading branch information
jacoblee93 committed Sep 24, 2024
commit abb7bb8ecc891a7548bd2bfcc1f354f5ad95065c
57 changes: 28 additions & 29 deletions libs/langchain-community/src/chat_models/bedrock/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,7 @@ export class BedrockChat

region: string;

credentials?: CredentialType;
awsAccessKeyID?: string;
awsSecretAccessKey?: string;
awsSessionToken?: string;
credentials: CredentialType;

temperature?: number | undefined = undefined;

Expand Down Expand Up @@ -543,6 +540,7 @@ export class BedrockChat
return {
"credentials.accessKeyId": "AWS_ACCESS_KEY_ID",
"credentials.secretAccessKey": "AWS_SECRET_ACCESS_KEY",
"credentials.sessionToken": "AWS_SECRET_ACCESS_KEY",
awsAccessKeyId: "AWS_ACCESS_KEY_ID",
awsSecretAccessKey: "AWS_SECRET_ACCESS_KEY",
awsSessionToken: "AWS_SESSION_TOKEN",
Expand All @@ -568,7 +566,32 @@ export class BedrockChat
}

constructor(fields?: BedrockChatFields) {
super(fields ?? {});
const awsAccessKeyId =
fields?.awsAccessKeyId ?? getEnvironmentVariable("AWS_ACCESS_KEY_ID");
const awsSecretAccessKey =
fields?.awsSecretAccessKey ??
getEnvironmentVariable("AWS_SECRET_ACCESS_KEY");
const awsSessionToken =
fields?.awsSessionToken ?? getEnvironmentVariable("AWS_SESSION_TOKEN");

let credentials = fields?.credentials;
if (credentials === undefined) {
if (awsAccessKeyId === undefined || awsSecretAccessKey === undefined) {
throw new Error(
"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."
);
}
credentials = {
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
sessionToken: awsSessionToken,
};
}

// eslint-disable-next-line no-param-reassign
fields = { ...fields, awsAccessKeyId, awsSecretAccessKey, awsSessionToken };

super(fields);

this.model = fields?.model ?? this.model;
this.modelProvider = getModelProvider(this.model);
Expand All @@ -587,30 +610,6 @@ export class BedrockChat
}
this.region = region;

this.awsAccessKeyID =
fields?.awsAccessKeyID || getEnvironmentVariable("AWS_ACCESS_KEY_ID");
this.awsSecretAccessKey =
fields?.awsSecretAccessKey ||
getEnvironmentVariable("AWS_SECRET_ACCESS_KEY");
this.awsSessionToken =
fields?.awsSessionToken || getEnvironmentVariable("AWS_SESSION_TOKEN");
const credentials = fields?.credentials ?? {
...(this.awsAccessKeyID !== undefined && {
accessKeyID: this.awsAccessKeyID,
}),
...(this.awsSecretAccessKey !== undefined && {
secretAccessKey: this.awsSecretAccessKey,
}),
...(this.awsSessionToken !== undefined && {
sessionToken: this.awsSessionToken,
}),
};

if (!credentials) {
throw new Error(
"Please set the AWS credentials in the 'credentials' field or set env vars AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, or AWS_SESSION_TOKEN."
);
}
this.credentials = credentials;

this.temperature = fields?.temperature ?? this.temperature;
Expand Down
29 changes: 29 additions & 0 deletions libs/langchain-community/src/chat_models/tests/chatbedrock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,32 @@ test("Test Bedrock identifying params", async () => {
model,
});
});

test("Test Bedrock serialization", async () => {
const bedrock = new BedrockChat({
region: "us-west-2",
model: "anthropic.claude-3-sonnet-20240229-v1:0",
credentials: {
accessKeyId: "unused",
secretAccessKey: "unused",
sessionToken: "unused",
},
});

expect(JSON.stringify(bedrock)).toEqual(
`{"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"]}},"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"]}}}`
);
});

test("Test Bedrock serialization with no parameters", async () => {
process.env.AWS_ACCESS_KEY_ID = "foo";
process.env.AWS_SECRET_ACCESS_KEY = "bar";
const bedrock = new BedrockChat({
region: "us-west-2",
model: "anthropic.claude-3-sonnet-20240229-v1:0",
});

expect(JSON.stringify(bedrock)).toEqual(
`{"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"]}}}}`
);
});
4 changes: 3 additions & 1 deletion libs/langchain-community/src/utils/bedrock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ export interface BaseBedrockInput {
streamProcessingMode: "SYNCHRONOUS" | "ASYNCHRONOUS";
};

awsAccessKeyID?: string;
awsAccessKeyId?: string;

awsSecretAccessKey?: string;

awsSessionToken?: string;
}

Expand Down
Loading