Skip to content

Commit

Permalink
feat(bedrock): include bedrock SDK (#328)
Browse files Browse the repository at this point in the history
The standalone package is being deprecated in favour
of `anthropic[bedrock]`
  • Loading branch information
stainless-bot committed Jan 31, 2024
1 parent 6da7b64 commit 975ce95
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 3 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ Streaming with `client.beta.messages.stream(...)` exposes [various helpers for y

Alternatively, you can use `client.beta.messages.create(..., stream=True)` which only returns an async iterable of the events in the stream and thus uses less memory (it does not build up a final message object for you).

## AWS Bedrock

This library also provides support for the [Anthropic Bedrock API](https://aws.amazon.com/bedrock/claude/) if you install this library with the `bedrock` extra, e.g. `pip install -U anthropic[bedrock]`.

You can then import and instantiate a separate `AnthropicBedrock` class, the rest of the API is the same.

```py
from anthropic import AI_PROMPT, HUMAN_PROMPT, AnthropicBedrock

client = AnthropicBedrock()

completion = client.completions.create(
model="anthropic.claude-instant-v1",
prompt=f"{HUMAN_PROMPT} hey!{AI_PROMPT}",
stop_sequences=[HUMAN_PROMPT],
max_tokens_to_sample=500,
temperature=0.5,
top_k=250,
top_p=0.5,
)
print(completion.completion)
```

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

## Token counting

You can estimate billing for a given request with the `client.count_tokens()` method, eg:
Expand Down
39 changes: 39 additions & 0 deletions examples/bedrock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env -S poetry run python

# Note: you must have installed `anthropic` with the `bedrock` extra
# e.g. `pip install -U anthropic[bedrock]`

from anthropic import AI_PROMPT, HUMAN_PROMPT, AnthropicBedrock

# Note: this assumes you have AWS credentials configured.
#
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
client = AnthropicBedrock()

print("------ standard response ------")
completion = client.completions.create(
model="anthropic.claude-instant-v1",
prompt=f"{HUMAN_PROMPT} hey!{AI_PROMPT}",
stop_sequences=[HUMAN_PROMPT],
max_tokens_to_sample=500,
temperature=0.5,
top_k=250,
top_p=0.5,
)
print(completion.completion)


question = """
Hey Claude! How can I recursively list all files in a directory in Python?
"""

print("------ streamed response ------")
stream = client.completions.create(
model="anthropic.claude-instant-v1",
prompt=f"{HUMAN_PROMPT} {question}{AI_PROMPT}",
max_tokens_to_sample=500,
stream=True,
)
for item in stream:
print(item.completion, end="")
print()
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ classifiers = [

[project.optional-dependencies]
vertex = ["google-auth >=2, <3"]
bedrock = ["boto3 >= 1.28.57", "botocore >= 1.31.57"]

[project.urls]
Homepage = "https://github.com/anthropics/anthropic-sdk-python"
Expand All @@ -59,7 +60,7 @@ dev-dependencies = [
"nox",
"dirty-equals>=0.6.0",
"importlib-metadata>=6.7.0",

"boto3-stubs >= 1"
]

[tool.rye.scripts]
Expand Down
10 changes: 9 additions & 1 deletion requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ annotated-types==0.6.0
anyio==4.1.0
argcomplete==3.1.2
attrs==23.1.0
boto3==1.28.58
boto3-stubs==1.28.41
botocore==1.31.58
botocore-stubs==1.34.31
cachetools==5.3.2
certifi==2023.7.22
charset-normalizer==3.3.2
Expand All @@ -29,6 +33,7 @@ huggingface-hub==0.16.4
idna==3.4
importlib-metadata==7.0.0
iniconfig==2.0.0
jmespath==1.0.1
mypy==1.7.1
mypy-extensions==1.0.0
nodeenv==1.8.0
Expand All @@ -51,14 +56,17 @@ requests==2.31.0
respx==0.20.2
rsa==4.9
ruff==0.1.9
s3transfer==0.7.0
six==1.16.0
sniffio==1.3.0
time-machine==2.9.0
tokenizers==0.14.0
tomli==2.0.1
tqdm==4.66.1
types-awscrt==0.20.3
types-s3transfer==0.10.0
typing-extensions==4.8.0
urllib3==2.1.0
urllib3==1.26.18
virtualenv==20.24.5
zipp==3.17.0
# The following packages are considered to be unsafe in a requirements file:
Expand Down
8 changes: 7 additions & 1 deletion requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
-e file:.
annotated-types==0.6.0
anyio==4.1.0
boto3==1.34.31
botocore==1.34.31
cachetools==5.3.2
certifi==2023.7.22
charset-normalizer==3.3.2
Expand All @@ -22,16 +24,20 @@ httpcore==1.0.2
httpx==0.25.2
huggingface-hub==0.16.4
idna==3.4
jmespath==1.0.1
packaging==23.2
pyasn1==0.5.1
pyasn1-modules==0.3.0
pydantic==2.4.2
pydantic-core==2.10.1
python-dateutil==2.8.2
pyyaml==6.0.1
requests==2.31.0
rsa==4.9
s3transfer==0.10.0
six==1.16.0
sniffio==1.3.0
tokenizers==0.14.0
tqdm==4.66.1
typing-extensions==4.8.0
urllib3==2.1.0
urllib3==1.26.18
1 change: 1 addition & 0 deletions src/anthropic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
]

from .lib.vertex import *
from .lib.bedrock import *
from .lib.streaming import *

_setup_logging()
Expand Down
1 change: 1 addition & 0 deletions src/anthropic/lib/bedrock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._client import AnthropicBedrock as AnthropicBedrock, AsyncAnthropicBedrock as AsyncAnthropicBedrock
42 changes: 42 additions & 0 deletions src/anthropic/lib/bedrock/_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import annotations

import httpx


def get_auth_headers(
*,
method: str,
url: str,
headers: httpx.Headers,
aws_access_key: str | None,
aws_secret_key: str | None,
aws_session_token: str | None,
region: str | None,
data: str | None,
) -> dict[str, str]:
import boto3
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest

session = boto3.Session(
region_name=region,
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key,
aws_session_token=aws_session_token,
)

# The connection header may be stripped by a proxy somewhere, so the receiver
# of this message may not see this header, so we remove it from the set of headers
# that are signed.
headers = headers.copy()
del headers["connection"]

request = AWSRequest(method=method.upper(), url=url, headers=headers, data=data)
credentials = session.get_credentials()

signer = SigV4Auth(credentials, "bedrock", session.region_name)
signer.add_auth(request)

prepped = request.prepare()

return dict(prepped.headers)
Loading

0 comments on commit 975ce95

Please sign in to comment.