Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions libs/langchain-community/src/chat_models/bedrock/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { defaultProvider } from "@aws-sdk/credential-provider-node";
import { defaultProvider, DefaultProviderInit } from "@aws-sdk/credential-provider-node";

import type { BaseChatModelParams } from "@langchain/core/language_models/chat_models";

import { BaseBedrockInput } from "../../utils/bedrock.js";
import { BedrockChat as BaseBedrockChat } from "./web.js";

export interface BedrockChatFields extends Partial<BaseBedrockInput>, BaseChatModelParams, Partial<DefaultProviderInit> {}

/**
* @example
* ```typescript
Expand All @@ -21,10 +23,32 @@ export class BedrockChat extends BaseBedrockChat {
return "BedrockChat";
}

constructor(fields?: Partial<BaseBedrockInput> & BaseChatModelParams) {
constructor(fields?: BedrockChatFields) {
const {
profile,
filepath,
configFilepath,
ignoreCache,
mfaCodeProvider,
roleAssumer,
roleArn,
webIdentityTokenFile,
roleAssumerWithWebIdentity,
...rest
} = fields ?? {};
super({
...fields,
credentials: fields?.credentials ?? defaultProvider(),
...rest,
credentials: rest?.credentials ?? defaultProvider({
profile,
filepath,
configFilepath,
ignoreCache,
mfaCodeProvider,
roleAssumer,
roleArn,
webIdentityTokenFile,
roleAssumerWithWebIdentity,
}),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import { test, expect } from "@jest/globals";
import { HumanMessage } from "@langchain/core/messages";
import { BedrockChat } from "../bedrock/web.js";
import { BedrockChat as BedrockChatWeb } from "../bedrock/web.js";
import { BedrockChat } from "../bedrock/index.js";

// void testChatModel(
// "Test Bedrock chat model: Llama2 13B v1",
Expand Down Expand Up @@ -147,7 +148,7 @@ async function testChatModel(
test(title, async () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey team, I've reviewed the code and noticed that the recent change in chatbedrock.int.test.ts accesses an environment variable using process.env. I've flagged this for your review to ensure it aligns with our best practices. Let me know if you need further assistance!

const region = process.env.BEDROCK_AWS_REGION ?? defaultRegion;

const bedrock = new BedrockChat({
const bedrock = new BedrockChatWeb({
maxTokens: 200,
region,
model,
Expand Down Expand Up @@ -181,7 +182,7 @@ async function testChatStreamingModel(
test(title, async () => {
const region = process.env.BEDROCK_AWS_REGION ?? defaultRegion;

const bedrock = new BedrockChat({
const bedrock = new BedrockChatWeb({
maxTokens: 200,
region,
model,
Expand Down Expand Up @@ -224,7 +225,7 @@ async function testChatHandleLLMNewToken(
const region = process.env.BEDROCK_AWS_REGION ?? defaultRegion;
const tokens: string[] = [];

const bedrock = new BedrockChat({
const bedrock = new BedrockChatWeb({
maxTokens: 200,
region,
model,
Expand Down Expand Up @@ -260,7 +261,7 @@ test.skip.each([
])("Test Bedrock base chat model: %s", async (model) => {
const region = process.env.BEDROCK_AWS_REGION ?? "us-east-1";

const bedrock = new BedrockChat({
const bedrock = new BedrockChatWeb({
region,
model,
maxRetries: 0,
Expand All @@ -277,3 +278,13 @@ test.skip.each([

expect(res.content.length).toBeGreaterThan(1);
});

test.skip("new credential fields", async () => {
const model = new BedrockChat({
filepath: "/Users/bracesproul/code/lang-chain-ai/langchainjs/libs/langchain-community/src/chat_models/tests/aws_credentials",
model: "anthropic.claude-3-sonnet-20240229-v1:0",
region: process.env.BEDROCK_AWS_REGION
})
const res = await model.invoke(["Why is the sky blue? Be VERY concise!"]);
console.log("res", res)
})