Set up the Amazon Bedrock API - Amazon Bedrock

Set up the Amazon Bedrock API

This section describes how to set up your environment to make Amazon Bedrock API calls and provides examples of common use-cases. You can access the Amazon Bedrock API using the AWS Command Line Interface (AWS CLI), an AWS SDK, or a SageMaker Notebook.

Before you can access Amazon Bedrock APIs, you need to request access to the foundation models that you plan to use.

For details about the API operations and parameters, see the Amazon Bedrock API Reference.

The following resources provide additional information about the Amazon Bedrock API.

Add model access

Important

Before you can use any of the foundation models, you must request access to that model. If you try to use the model (with the API or within the console) before you have requested access to it, you will receive an error message. For more information, see Model access.

Amazon Bedrock endpoints

To connect programmatically to an AWS service, you use an endpoint. Refer to the Amazon Bedrock endpoints and quotas chapter in the AWS General Reference for information about the endpoints that you can use for Amazon Bedrock.

Amazon Bedrock provides the following service endpoints.

Setting up the AWS CLI

  1. If you plan to use the CLI, install and configure the AWS CLI by following the steps at Install or update the latest version of the AWS Command Line Interface User Guide.

  2. Configure your AWS credentials using the aws configure CLI command by following the steps at Configure the AWS CLI.

Refer to the following references for AWS CLI commands and operations:

Setting up an AWS SDK

AWS software development kits (SDKs) are available for many popular programming languages. Each SDK provides an API, code examples, and documentation that make it easier for developers to build applications in their preferred language. SDKs automatically perform useful tasks for you, such as:

  • Cryptographically sign your service requests

  • Retry requests

  • Handle error responses

Refer to the following table to find general information about and code examples for each SDK, as well as the Amazon Bedrock API references for each SDK. You can also find code examples at Code examples for Amazon Bedrock using AWS SDKs.

Using SageMaker notebooks

You can use the SDK for Python (Boto3) to invoke Amazon Bedrock API operations from a SageMaker notebook.

Configure the SageMaker role

Add Amazon Bedrock permissions to the IAM role that will use this SageMaker notebook.

From the IAM console, perform these steps:

  1. Choose the IAM role, then choose Add Permissions and select Create Inline Policies from the dropdown list.

  2. Include the following permission.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "bedrock:*", "Resource": "*" } ] }

Add the following permissions to the trust relationships.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "bedrock.amazonaws.com" }, "Action": "sts:AssumeRole" }, { "Sid": "", "Effect": "Allow", "Principal": { "Service": "sagemaker.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

Test the Runtime setup

Add the following code to your notebook and run the code.

import boto3 import json bedrock = boto3.client(service_name='bedrock-runtime') body = json.dumps({ "prompt": "\n\nHuman:explain black holes to 8th graders\n\nAssistant:", "max_tokens_to_sample": 300, "temperature": 0.1, "top_p": 0.9, }) modelId = 'anthropic.claude-v2' accept = 'application/json' contentType = 'application/json' response = bedrock.invoke_model(body=body, modelId=modelId, accept=accept, contentType=contentType) response_body = json.loads(response.get('body').read()) # text print(response_body.get('completion'))

Test the Amazon Bedrock setup

Add the following code to your notebook and run the code.

import boto3 bedrock = boto3.client(service_name='bedrock') bedrock.get_foundation_model(modelIdentifier='anthropic.claude-v2')