Skip to content

Commit

Permalink
Fix ChatBedrock integration with Amazon Bedrock model
Browse files Browse the repository at this point in the history
Fixes langchain-ai#308

Update the `ChatBedrock` class to handle the `region_name` parameter correctly.

* Modify `libs/aws/langchain_aws/chat_models/bedrock.py` to ensure the `region_name` parameter is passed to the `ChatBedrockConverse` class and add logic to take `region_name` from `AWS_REGION` environment variable if not provided.
* Add test cases in `libs/aws/tests/integration_tests/chat_models/test_bedrock_converse.py` to confirm that `ChatBedrockConverse` works with the `amazon.nova-pro-v1:0` model and takes `region_name` from `AWS_REGION` environment variable if not provided.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/langchain-ai/langchain-aws/issues/308?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
SmartManoj committed Jan 26, 2025
1 parent ac7ec07 commit 0043fea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/aws/langchain_aws/chat_models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ def _as_converse(self) -> ChatBedrockConverse:
kwargs["temperature"] = self.temperature
return ChatBedrockConverse(
model=self.model_id,
region_name=self.region_name,
region_name=self.region_name or os.getenv("AWS_REGION"),
credentials_profile_name=self.credentials_profile_name,
aws_access_key_id=self.aws_access_key_id,
aws_secret_access_key=self.aws_secret_access_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def chat_model_class(self) -> Type[BaseChatModel]:

@property
def chat_model_params(self) -> dict:
return {"model": "us.amazon.nova-pro-v1:0"}
return {"model": "us.amazon.nova-pro-v1:0", "region_name": "us-east-1"}

@property
def standard_chat_model_params(self) -> dict:
Expand Down Expand Up @@ -242,3 +242,22 @@ def test_guardrails() -> None:
)
assert response.response_metadata["stopReason"] == "guardrail_intervened"
assert response.response_metadata["trace"] is not None


def test_chat_bedrock_converse_with_region_name() -> None:
model = ChatBedrockConverse(
model="us.amazon.nova-pro-v1:0", region_name="us-east-1", temperature=0
)
response = model.invoke("Create a list of 3 pop songs")
assert isinstance(response, BaseModel)
assert response.content is not None


def test_chat_bedrock_converse_with_aws_region_env() -> None:
import os

os.environ["AWS_REGION"] = "us-east-1"
model = ChatBedrockConverse(model="us.amazon.nova-pro-v1:0", temperature=0)
response = model.invoke("Create a list of 3 pop songs")
assert isinstance(response, BaseModel)
assert response.content is not None

0 comments on commit 0043fea

Please sign in to comment.