AWS BedRock Claude API Tutorial

Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models, including Claude, via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI.

Claude AI is a powerful large language model developed by Anthropic. With its impressive capabilities, Claude AI can generate human-like text, making it an excellent tool for various applications, such as chatbots, content creation, and text summarization.

This article will guide you on how to use Claude AI using AWS BedRock, a managed service that allows you to run and scale AI models easily.

Get Access To Claude API

AWS BedRock Dashboard

To get access to Claude API via AWS Bedrock, you need to follow these steps:

IMPORTANT: CHECK THE PRICING. Regions like Ohio, and Oregon have less pricing than Asia.

  1. Visit the AWS Bedrock website and go to providers or Base models. Then find Claude and select “manage model access”
  2. VERY IMPORTANT: First, Submit your use case and Then request model access to the Anthropic Claude model.
  3. It may take several hours to up to 2 days to get access to the model. If you already have access to the Claude model, you don’t need to request access separately for Claude 2.1.

Get AWS Credentials

Go to Account -> Security Credentials

aws credentials for secret key
  • aws_access_key_id
  • aws_secret_access_key
  • region_name = ‘us-west-2’

Get ALL THREE by selecting “Create access key”

Initializing the Bedrock Client

Once you have the required credentials, you can initialize the Bedrock client using theΒ boto3Β library in Python. This client will allow you to interact with the Bedrock service and access the Claude AI model.

pip install boto3
bedrock = boto3.client( service_name='bedrock-runtime', 
region_name=region_name, 
aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, )

Setting Up Claude API

Go to the Chat, Select the Model, and define all the Configurations as you like. Then Send a message in the playground.

aws bedrock claude model playground

Next to the Load examples button, select the 3 dots. You will see View API Request. Select that and you will get a dictionary like the input inside summarize_text function below .

def summarize_text:
    input = {
      "modelId": "anthropic.claude-instant-v1",
      "contentType": "application/json",
      "accept": "*/*",
      "body": "summarize this text: "
    }
    # Make the API call
    response = bedrock.invoke_model(body=input["body"],
                                modelId=input["modelId"],
                                accept=input["accept"],
                                contentType=input["contentType"])

    response_body = json.loads(response['body'].read())

    return response_body['completion'].strip()

The other functions to make API is straight forward as given below. You can use it as you like.

Conclusion

In this article, you’ve learned how to use Claude AI with AWS BedRock to summarize text. With this powerful combination, you can build intelligent applications that can generate human-like text and provide valuable insights. Remember to always test and optimize your models for the best results. Happy coding!