Skip to content

Bedrock managed prompts examples #7418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
24f40b5
work on python hello bedrock example
AWSChris Jan 24, 2025
65ae500
fixed metadata
AWSChris Jan 24, 2025
11462df
removed AWS reference
AWSChris Jan 24, 2025
81e943e
reformatted string
AWSChris Jan 24, 2025
7bbb1a7
updated readme
AWSChris Jan 24, 2025
f93e0b0
updated readme
AWSChris Jan 24, 2025
2ba04d9
updated readme
AWSChris Jan 24, 2025
9fb3e7d
updated readme
AWSChris Jan 24, 2025
7944fe8
ran writeme script to update bedrock readme
AWSChris Jan 25, 2025
17f5a18
removed region declaration
AWSChris Jan 27, 2025
f0098bb
Merge branch 'awsdocs:main' into main
AWSChris Jan 28, 2025
6e564f8
Merge branch 'awsdocs:main' into main
AWSChris Jan 30, 2025
65c627c
Merge branch 'awsdocs:main' into main
AWSChris Jan 31, 2025
d90d701
Merge branch 'awsdocs:main' into main
AWSChris Feb 12, 2025
05cfaac
Merge branch 'awsdocs:main' into main
AWSChris Feb 13, 2025
8c5e8a9
Merge branch 'awsdocs:main' into main
AWSChris Feb 22, 2025
dd1f4d9
Merge branch 'awsdocs:main' into main
AWSChris Mar 3, 2025
a5ca8b2
Merge branch 'awsdocs:main' into main
AWSChris Mar 21, 2025
2ce3337
Merge branch 'awsdocs:main' into main
AWSChris Mar 26, 2025
dd00e5d
Merge branch 'awsdocs:main' into main
AWSChris Apr 22, 2025
ce6e369
updated requirements.txt
Apr 24, 2025
c2ce68d
updated requirements
AWSChris Apr 24, 2025
b09d603
added prompts
AWSChris Apr 24, 2025
4fde44e
workon readme
AWSChris Apr 24, 2025
33d59e9
changed scenario to match docs
AWSChris Apr 25, 2025
fd3014b
updated JSON support to match flow docs example. Other celan up
AWSChris Apr 25, 2025
bfdc39a
made invoke_prompt function generic
AWSChris Apr 25, 2025
9101109
clean up
AWSChris Apr 25, 2025
bb9d260
added integ test
AWSChris Apr 25, 2025
81f3b6a
ongoing work
AWSChris Apr 25, 2025
6a27d93
added metadata, other updates
AWSChris Apr 25, 2025
23dc966
Merge branch 'awsdocs:main' into main
AWSChris Apr 25, 2025
18d9792
main merge
AWSChris Apr 25, 2025
78e3f52
Update bedrock-agent_metadata.yaml
rlhagerm Apr 29, 2025
d1e5722
Update bedrock-agent_metadata.yaml
rlhagerm Apr 29, 2025
3be0cbc
Update bedrock-agent_metadata.yaml
rlhagerm Apr 29, 2025
daca037
Update bedrock-agent_metadata.yaml
rlhagerm Apr 29, 2025
6874f80
Update bedrock-agent_metadata.yaml
rlhagerm Apr 29, 2025
b79bb69
Update bedrock-agent_metadata.yaml
rlhagerm Apr 29, 2025
687d744
Update bedrock-agent_metadata.yaml
rlhagerm Apr 29, 2025
5f5b3dc
updates for review feedback
AWSChris Apr 29, 2025
722a3d0
Merge branch 'main' into prompts
brmur Apr 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 92 additions & 4 deletions .doc_gen/metadata/bedrock-agent_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ bedrock-agent_PrepareAgent:
services:
bedrock-agent: {PrepareAgent}


bedrock-agent_GettingStartedWithBedrockAgents:
title: An end-to-end example showing how to create and invoke &BRA; using an &AWS; SDK
title_abbrev: Create and invoke an agent
Expand All @@ -211,8 +210,6 @@ bedrock-agent_GettingStartedWithBedrockAgents:
services:
bedrock-agent: {CreateAgent, CreateAgentActionGroup, CreateAgentAlias, DeleteAgent, DeleteAgentAlias, GetAgent, ListAgentActionGroups,
ListAgents, ListAgentKnowledgeBases, PrepareAgent}



bedrock-agent_GettingStartedWithBedrockFlows:
title: An end-to-end example showing how to create and invoke an Amazon Bedrock flow using an &AWS; SDK
Expand Down Expand Up @@ -242,7 +239,6 @@ bedrock-agent_GettingStartedWithBedrockFlows:
GetFlowVersion, PrepareFlow}
bedrock-agent-runtime: {InvokeFlow}


bedrock-agent_CreateFlow:
languages:
Python:
Expand Down Expand Up @@ -433,3 +429,95 @@ bedrock-agent_ListFlowVersions:

services:
bedrock-agent: {ListFlowVersions}

bedrock-agent_CreatePrompt:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Create an Amazon Bedrock managed prompt.
snippet_tags:
- python.example_code.bedrock.create_prompt

services:
bedrock-agent: {CreatePrompt}

bedrock-agent_CreatePromptVersion:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Create a version of an Amazon Bedrock managed prompt.
snippet_tags:
- python.example_code.bedrock.create_prompt_version

services:
bedrock-agent: {CreatePromptVersion}

bedrock-agent_GetPrompt:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Get an Amazon Bedrock managed prompt.
snippet_tags:
- python.example_code.bedrock.get_prompt

services:
bedrock-agent: {GetPrompt}

bedrock-agent_DeletePrompt:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Delete an Amazon Bedrock managed prompt.
snippet_tags:
- python.example_code.bedrock.delete_prompt

services:
bedrock-agent: {DeletePrompt}

bedrock-agent_ListPrompts:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: List Amazon Bedrock managed prompts.
snippet_tags:
- python.example_code.bedrock.list_prompts

services:
bedrock-agent: {ListPrompts}

bedrock-agent_GettingStartedWithBedrockPrompts:
title: An end-to-end example showing how to create and invoke Amazon Bedrock managed prompts using an &AWS; SDK
title_abbrev: Create and invoke a managed prompt
synopsis_list:
- Create a managed prompt.
- Create a version of the prompt.
- Invoke the prompt using the version.
- Clean up resources (optional).
category: Scenarios
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Create and invoke a managed prompt.
snippet_tags:
- python.example_code.bedrock-agent-runtime.Scenario_GettingStartedBedrockManagedPrompts
services:
bedrock-agent: {CreatePrompt, CreatePromptVersion, DeletePrompt}
bedrock-runtime: {Converse}
57 changes: 55 additions & 2 deletions python/example_code/bedrock-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

## Overview

Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Bedrock Agents.
Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Bedrock agents, flows, and managed prompts.

<!--custom.overview.start-->
<!--custom.overview.end-->

_Amazon Bedrock Agents offer you the ability to build and configure autonomous agents in your application._



## ⚠ Important

* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
Expand Down Expand Up @@ -63,6 +65,13 @@ Code excerpts that show you how to call individual service functions.
- [UpdateFlowAlias](flows/flow_alias.py#L55)
- [DeleteFlowAlias](flows/flow_alias.py#L98)
- [ListFlowAliases](flows/flow_alias.py#L132)
- [FlowConversation](flows/flow-conversation.py)
- [CreatePrompt](prompts/prompt.py#L32)
- [CreatePromptVersion](prompts/prompt.py#L61)
- [GetPrompt](prompts/prompt.py#L71)
- [DeletePrompt](prompts/prompt.py#L154)
- [ListPrompts](prompts/prompt.py#L187)




Expand All @@ -73,6 +82,7 @@ functions within the same service.

- [Create and invoke an agent](scenario_get_started_with_agents.py)
- [Create and invoke a flow](flows/playlist_flow.py)
- [Create and invoke a managed prompt](prompts/scenario_get_started_with_prompts.py)


<!--custom.examples.start-->
Expand Down Expand Up @@ -144,6 +154,49 @@ python flows/list_flows.py
The example first lists the flows in the current AWS Region. It
then prompts for a flow ID, which you can get from the list of flows. Finally, the example lists the flow versions and flow aliases for the flow ID that you entered.

#### Create and invoke a managed prompt

This example shows you how to do the following:

- Create a managed prompt
- Create a version of the prompt
- Invoke the prompt using the version
- Update the prompt
- Create a new version
- Invoke the updated prompt
- Clean up resources (optional)

Start the example by running the following at a command prompt:

```
python prompts/scenario_get_started_with_prompts.py
```

By default, the example will clean up all resources it creates. If you want to keep the resources for further exploration, use the `--no-cleanup` flag:

```
python prompts/scenario_get_started_with_prompts.py --no-cleanup
```

You can also specify a different AWS region or model ID:

```
python prompts/scenario_get_started_with_prompts.py --region us-west-2 --model-id anthropic.claude-3-sonnet-20240229-v1:0
```

#### List prompts

Shows how to list Amazon Bedrock managed prompts and versions of a prompt.

Start the example by running the following at a command prompt:

```
python prompts/list_prompts.py
```

The example first lists the prompts in the current AWS Region.


### Tests

⚠ Running tests might result in charges to your AWS account.
Expand All @@ -170,4 +223,4 @@ in the `python` folder.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
61 changes: 61 additions & 0 deletions python/example_code/bedrock-agent/prompts/list_prompts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to use the AWS SDK for Python (Boto3) with Amazon Bedrock
to list Amazon Bedrock managed prompts.
"""

import argparse
import boto3
import logging

from prompt import list_prompts

logging.basicConfig(
level=logging.INFO,
format='%(levelname)s: %(message)s'
)
logger = logging.getLogger(__name__)

def main():
"""
Lists Amazon Bedrock managed prompts.
"""
parser = argparse.ArgumentParser(
description="Lists Amazon Bedrock managed prompts."
)
parser.add_argument(
'--region',
default='us-east-1',
help="The AWS Region to use."
)
args = parser.parse_args()

# Use bedrock-agent client for prompt management
bedrock_client = boto3.client('bedrock-agent', region_name=args.region)

try:

# List all prompts
print("\n=== Amazon Bedrock Managed Prompts ===")
prompts = list_prompts(bedrock_client)

if not prompts:
print("No prompts found in this region.")
return

for prompt in prompts:
print(f"ID: {prompt['id']}")
print(f"Name: {prompt['name']}")
print(f"Description: {prompt.get('description', 'No description')}")
print(f"ARN: {prompt.get('arn', 'No ARN')}")
print(f"Created: {prompt.get('createdAt', 'Unknown')}")
print(f"Updated: {prompt.get('updatedAt', 'Unknown')}")
print(f"Version: {prompt.get('version', 'Unknown')}")
print("-" * 40)

except Exception as e:
logger.exception("Error listing prompts: %s", str(e))

if __name__ == "__main__":
main()
Loading
Loading