There are more AWS SDK examples available in the AWS Doc SDK Examples
Amazon Bedrock examples using SDK for Python (Boto3)
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Python (Boto3) with Amazon Bedrock.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Actions
The following code example shows how to use GetFoundationModel
.
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Get details about a foundation model.
def get_foundation_model(self, model_identifier): """ Get details about an Amazon Bedrock foundation model. :return: The foundation model's details. """ try: return self.bedrock_client.get_foundation_model( modelIdentifier=model_identifier )["modelDetails"] except ClientError: logger.error( f"Couldn't get foundation models details for {model_identifier}" ) raise
-
For API details, see GetFoundationModel in AWS SDK for Python (Boto3) API Reference.
-
The following code example shows how to use ListFoundationModels
.
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. List the available Amazon Bedrock foundation models.
def list_foundation_models(self): """ List the available Amazon Bedrock foundation models. :return: The list of available bedrock foundation models. """ try: response = self.bedrock_client.list_foundation_models() models = response["modelSummaries"] logger.info("Got %s foundation models.", len(models)) return models except ClientError: logger.error("Couldn't list foundation models.") raise
-
For API details, see ListFoundationModels in AWS SDK for Python (Boto3) API Reference.
-
Scenarios
The following code example shows how to build and orchestrate generative AI applications with Amazon Bedrock and Step Functions.
- SDK for Python (Boto3)
-
The Amazon Bedrock Serverless Prompt Chaining scenario demonstrates how AWS Step Functions, Amazon Bedrock, and https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html can be used to build and orchestrate complex, serverless, and highly scalable generative AI applications. It contains the following working examples:
-
Write an analysis of a given novel for a literature blog. This example illustrates a simple, sequential chain of prompts.
-
Generate a short story about a given topic. This example illustrates how the AI can iteratively process a list of items that it previously generated.
-
Create an itinerary for a weekend vacation to a given destination. This example illustrates how to parallelize multiple distinct prompts.
-
Pitch movie ideas to a human user acting as a movie producer. This example illustrates how to parallelize the same prompt with different inference parameters, how to backtrack to a previous step in the chain, and how to include human input as part of the workflow.
-
Plan a meal based on ingredients the user has at hand. This example illustrates how prompt chains can incorporate two distinct AI conversations, with two AI personas engaging in a debate with each other to improve the final outcome.
-
Find and summarize today's highest trending GitHub repository. This example illustrates chaining multiple AI agents that interact with external APIs.
For complete source code and instructions to set up and run, see the full project on GitHub
. Services used in this example
Amazon Bedrock
Amazon Bedrock Runtime
Amazon Bedrock Agents
Amazon Bedrock Agents Runtime
Step Functions
-