-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bedrock): include bedrock SDK (#328)
The standalone package is being deprecated in favour of `anthropic[bedrock]`
- Loading branch information
1 parent
6da7b64
commit 975ce95
Showing
11 changed files
with
522 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._client import AnthropicBedrock as AnthropicBedrock, AsyncAnthropicBedrock as AsyncAnthropicBedrock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.