Skip to content

Instantly share code, notes, and snippets.

@sergenes
Created December 10, 2023 15:40
Show Gist options
  • Save sergenes/2ff15c7fc036dcce77c8ac044db3269a to your computer and use it in GitHub Desktop.
Save sergenes/2ff15c7fc036dcce77c8ac044db3269a to your computer and use it in GitHub Desktop.
import anthropic_bedrock
from anthropic_bedrock import AnthropicBedrock
aws_access_key = os.getenv('AWS_ACCESS_KEY')
aws_secret_key = os.getenv('AWS_SECRET_KEY')
abclient = AnthropicBedrock(
# Authenticate by either providing the keys below or use the default AWS credential providers, such as
# using ~/.aws/credentials or the "AWS_SECRET_ACCESS_KEY" and "AWS_ACCESS_KEY_ID" environment variables.
aws_access_key=aws_access_key,
aws_secret_key=aws_secret_key,
# Temporary credentials can be used with aws_session_token.
# Read more at https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html.
# aws_session_token="<session_token>",
# aws_region changes the aws region to which the request is made. By default, we read AWS_REGION,
# and if that's not present, we default to us-east-1. Note that we do not read ~/.aws/config for the region.
aws_region="us-east-1",
)
def get_bedrock_anthropic_completion(aprompt, amodel="anthropic.claude-instant-v1"):
completion = abclient.completions.create(
model=amodel,
max_tokens_to_sample=1000,
temperature=0.2,
prompt=f"{anthropic_bedrock.HUMAN_PROMPT} ${aprompt} {anthropic_bedrock.AI_PROMPT}",
)
return completion.completion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment