Skip to content

Commit

Permalink
feat(bedrock): add profile argument to client (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
khalilfiremind committed Sep 9, 2024
1 parent 4c229dc commit 6ea5fce
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ message = client.messages.create(
print(message)
```

The bedrock client supports the following arguments for authentication

```py
AnthropicBedrock(
aws_profile='...',
aws_region='us-east'
aws_secret_key='...',
aws_access_key='...',
aws_session_token='...',
)
```

For a more fully fledged example see [`examples/bedrock.py`](https://github.com/anthropics/anthropic-sdk-python/blob/main/examples/bedrock.py).

## Google Vertex
Expand Down
4 changes: 4 additions & 0 deletions src/anthropic/lib/bedrock/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def _get_session(
aws_secret_key: str | None,
aws_session_token: str | None,
region: str | None,
profile: str | None,
) -> boto3.Session:
import boto3

return boto3.Session(
profile_name=profile,
region_name=region,
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key,
Expand All @@ -37,12 +39,14 @@ def get_auth_headers(
aws_secret_key: str | None,
aws_session_token: str | None,
region: str | None,
profile: str | None,
data: str | None,
) -> dict[str, str]:
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest

session = _get_session(
profile=profile,
region=region,
aws_access_key=aws_access_key,
aws_secret_key=aws_secret_key,
Expand Down
6 changes: 6 additions & 0 deletions src/anthropic/lib/bedrock/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(
aws_secret_key: str | None = None,
aws_access_key: str | None = None,
aws_region: str | None = None,
aws_profile: str | None = None,
aws_session_token: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
Expand All @@ -119,6 +120,7 @@ def __init__(
if aws_region is None:
aws_region = os.environ.get("AWS_REGION") or "us-east-1"
self.aws_region = aws_region
self.aws_profile = aws_profile

self.aws_session_token = aws_session_token

Expand Down Expand Up @@ -163,6 +165,7 @@ def _prepare_request(self, request: httpx.Request) -> None:
aws_secret_key=self.aws_secret_key,
aws_session_token=self.aws_session_token,
region=self.aws_region or "us-east-1",
profile=self.aws_profile,
data=data,
)
request.headers.update(headers)
Expand Down Expand Up @@ -233,6 +236,7 @@ def __init__(
aws_secret_key: str | None = None,
aws_access_key: str | None = None,
aws_region: str | None = None,
aws_profile: str | None = None,
aws_session_token: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
Expand All @@ -258,6 +262,7 @@ def __init__(
if aws_region is None:
aws_region = os.environ.get("AWS_REGION") or "us-east-1"
self.aws_region = aws_region
self.aws_profile = aws_profile

self.aws_session_token = aws_session_token

Expand Down Expand Up @@ -302,6 +307,7 @@ async def _prepare_request(self, request: httpx.Request) -> None:
aws_secret_key=self.aws_secret_key,
aws_session_token=self.aws_session_token,
region=self.aws_region or "us-east-1",
profile=self.aws_profile,
data=data,
)
request.headers.update(headers)
Expand Down

0 comments on commit 6ea5fce

Please sign in to comment.