Run example Amazon Bedrock API requests with the AWS Command Line Interface - Amazon Bedrock

Run example Amazon Bedrock API requests with the AWS Command Line Interface

This section guides you through trying out some common operations in Amazon Bedrock to test that your permissions and authentication are set up properly. Before you run the following examples, you should check that you have fulfilled the following prerequisites:

Prerequisites

Test that your permissions and access keys are set up properly for Amazon Bedrock, using the Amazon Bedrock role that you created. These examples assume that you have configured a default profile with your access keys. Note the following:

  • Minimally, you must configure a profile containing an AWS access key ID and an AWS secret access key.

  • If you're using temporary credentials, you must also include an AWS session token.

List the foundation models that Amazon Bedrock has to offer

The following example runs the ListFoundationModels operation using an Amazon Bedrock endpoint. ListFoundationModels lists the foundation models (FMs) that are available in Amazon Bedrock in your region. In a terminal, run the following command:

aws bedrock list-foundation-models --region us-east-1

If the command is successful, the response returns a list of foundation models that are available in Amazon Bedrock.

Submit a text prompt to a model and generate a text response with InvokeModel

The following example runs the InvokeModel operation using an Amazon Bedrock runtime endpoint. InvokeModel lets you submit a prompt to generate a model response. In a terminal, run the following command:

aws bedrock-runtime invoke-model \ --model-id amazon.titan-text-express-v1 \ --body '{"inputText": "Describe the purpose of a \"hello world\" program in one line.", "textGenerationConfig" : {"maxTokenCount": 512, "temperature": 0.5, "topP": 0.9}}' \ --cli-binary-format raw-in-base64-out \ --outfile invoke-model-output-text.txt

If the command is successful, the response generated by the model is written to the invoke-model-output-text.txt file. The text response is returned in the outputText field, alongside accompanying information.

Submit a text prompt to a model and generate a text response with Converse

The following example runs the Converse operation using an Amazon Bedrock runtime endpoint. Converse lets you submit a prompt to generate a model response. We recommend using Converse operation over InvokeModel when supported, because it unifies the inference request across Amazon Bedrock models and simplifies the management of multi-turn conversations. In a terminal, run the following command:

aws bedrock-runtime converse \ --model-id amazon.titan-text-express-v1 \ --messages '[{"role": "user", "content": [{"text": "Describe the purpose of a \"hello world\" program in one line."}]}]' \ --inference-config '{"maxTokens": 512, "temperature": 0.5, "topP": 0.9}' \ --region us-east-1

If the command is successful, the response generated by the model is returned in the text field, alongside accompanying information.