Skip to content

Commit 5ab1226

Browse files
AWSChrisChristopher Reesrlhagermbrmur
authored
Bedrock managed prompts examples (#7418)
* work on python hello bedrock example * fixed metadata * reformatted string * updated readme * updated readme * updated readme * updated readme * ran writeme script to update bedrock readme * removed region declaration * updated requirements.txt * updated requirements * added prompts * workon readme * changed scenario to match docs * updated JSON support to match flow docs example. Other celan up * made invoke_prompt function generic * clean up * added integ test * ongoing work * added metadata, other updates * Update bedrock-agent_metadata.yaml * Update bedrock-agent_metadata.yaml * Update bedrock-agent_metadata.yaml * Update bedrock-agent_metadata.yaml * Update bedrock-agent_metadata.yaml * Update bedrock-agent_metadata.yaml * Update bedrock-agent_metadata.yaml * updates for review feedback --------- Co-authored-by: Christopher Rees <[email protected]> Co-authored-by: Rachel Hagerman <[email protected]> Co-authored-by: Brian Murray <[email protected]>
1 parent a122e6b commit 5ab1226

14 files changed

+1087
-10
lines changed

.doc_gen/metadata/bedrock-agent_metadata.yaml

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ bedrock-agent_PrepareAgent:
186186
services:
187187
bedrock-agent: {PrepareAgent}
188188

189-
190189
bedrock-agent_GettingStartedWithBedrockAgents:
191190
title: An end-to-end example showing how to create and invoke &BRA; using an &AWS; SDK
192191
title_abbrev: Create and invoke an agent
@@ -211,8 +210,6 @@ bedrock-agent_GettingStartedWithBedrockAgents:
211210
services:
212211
bedrock-agent: {CreateAgent, CreateAgentActionGroup, CreateAgentAlias, DeleteAgent, DeleteAgentAlias, GetAgent, ListAgentActionGroups,
213212
ListAgents, ListAgentKnowledgeBases, PrepareAgent}
214-
215-
216213

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

245-
246242
bedrock-agent_CreateFlow:
247243
languages:
248244
Python:
@@ -433,3 +429,95 @@ bedrock-agent_ListFlowVersions:
433429

434430
services:
435431
bedrock-agent: {ListFlowVersions}
432+
433+
bedrock-agent_CreatePrompt:
434+
languages:
435+
Python:
436+
versions:
437+
- sdk_version: 3
438+
github: python/example_code/bedrock-agent
439+
excerpts:
440+
- description: Create an Amazon Bedrock managed prompt.
441+
snippet_tags:
442+
- python.example_code.bedrock.create_prompt
443+
444+
services:
445+
bedrock-agent: {CreatePrompt}
446+
447+
bedrock-agent_CreatePromptVersion:
448+
languages:
449+
Python:
450+
versions:
451+
- sdk_version: 3
452+
github: python/example_code/bedrock-agent
453+
excerpts:
454+
- description: Create a version of an Amazon Bedrock managed prompt.
455+
snippet_tags:
456+
- python.example_code.bedrock.create_prompt_version
457+
458+
services:
459+
bedrock-agent: {CreatePromptVersion}
460+
461+
bedrock-agent_GetPrompt:
462+
languages:
463+
Python:
464+
versions:
465+
- sdk_version: 3
466+
github: python/example_code/bedrock-agent
467+
excerpts:
468+
- description: Get an Amazon Bedrock managed prompt.
469+
snippet_tags:
470+
- python.example_code.bedrock.get_prompt
471+
472+
services:
473+
bedrock-agent: {GetPrompt}
474+
475+
bedrock-agent_DeletePrompt:
476+
languages:
477+
Python:
478+
versions:
479+
- sdk_version: 3
480+
github: python/example_code/bedrock-agent
481+
excerpts:
482+
- description: Delete an Amazon Bedrock managed prompt.
483+
snippet_tags:
484+
- python.example_code.bedrock.delete_prompt
485+
486+
services:
487+
bedrock-agent: {DeletePrompt}
488+
489+
bedrock-agent_ListPrompts:
490+
languages:
491+
Python:
492+
versions:
493+
- sdk_version: 3
494+
github: python/example_code/bedrock-agent
495+
excerpts:
496+
- description: List Amazon Bedrock managed prompts.
497+
snippet_tags:
498+
- python.example_code.bedrock.list_prompts
499+
500+
services:
501+
bedrock-agent: {ListPrompts}
502+
503+
bedrock-agent_GettingStartedWithBedrockPrompts:
504+
title: An end-to-end example showing how to create and invoke Amazon Bedrock managed prompts using an &AWS; SDK
505+
title_abbrev: Create and invoke a managed prompt
506+
synopsis_list:
507+
- Create a managed prompt.
508+
- Create a version of the prompt.
509+
- Invoke the prompt using the version.
510+
- Clean up resources (optional).
511+
category: Scenarios
512+
languages:
513+
Python:
514+
versions:
515+
- sdk_version: 3
516+
github: python/example_code/bedrock-agent
517+
excerpts:
518+
- description: Create and invoke a managed prompt.
519+
snippet_tags:
520+
- python.example_code.bedrock-agent-runtime.Scenario_GettingStartedBedrockManagedPrompts
521+
services:
522+
bedrock-agent: {CreatePrompt, CreatePromptVersion, DeletePrompt}
523+
bedrock-runtime: {Converse}

python/example_code/bedrock-agent/README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
## Overview
44

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

77
<!--custom.overview.start-->
88
<!--custom.overview.end-->
99

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

12+
13+
1214
## ⚠ Important
1315

1416
* 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/).
@@ -63,6 +65,13 @@ Code excerpts that show you how to call individual service functions.
6365
- [UpdateFlowAlias](flows/flow_alias.py#L55)
6466
- [DeleteFlowAlias](flows/flow_alias.py#L98)
6567
- [ListFlowAliases](flows/flow_alias.py#L132)
68+
- [FlowConversation](flows/flow-conversation.py)
69+
- [CreatePrompt](prompts/prompt.py#L32)
70+
- [CreatePromptVersion](prompts/prompt.py#L61)
71+
- [GetPrompt](prompts/prompt.py#L71)
72+
- [DeletePrompt](prompts/prompt.py#L154)
73+
- [ListPrompts](prompts/prompt.py#L187)
74+
6675

6776

6877

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

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

7787

7888
<!--custom.examples.start-->
@@ -144,6 +154,49 @@ python flows/list_flows.py
144154
The example first lists the flows in the current AWS Region. It
145155
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.
146156

157+
#### Create and invoke a managed prompt
158+
159+
This example shows you how to do the following:
160+
161+
- Create a managed prompt
162+
- Create a version of the prompt
163+
- Invoke the prompt using the version
164+
- Update the prompt
165+
- Create a new version
166+
- Invoke the updated prompt
167+
- Clean up resources (optional)
168+
169+
Start the example by running the following at a command prompt:
170+
171+
```
172+
python prompts/scenario_get_started_with_prompts.py
173+
```
174+
175+
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:
176+
177+
```
178+
python prompts/scenario_get_started_with_prompts.py --no-cleanup
179+
```
180+
181+
You can also specify a different AWS region or model ID:
182+
183+
```
184+
python prompts/scenario_get_started_with_prompts.py --region us-west-2 --model-id anthropic.claude-3-sonnet-20240229-v1:0
185+
```
186+
187+
#### List prompts
188+
189+
Shows how to list Amazon Bedrock managed prompts and versions of a prompt.
190+
191+
Start the example by running the following at a command prompt:
192+
193+
```
194+
python prompts/list_prompts.py
195+
```
196+
197+
The example first lists the prompts in the current AWS Region.
198+
199+
147200
### Tests
148201

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

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

173-
SPDX-License-Identifier: Apache-2.0
226+
SPDX-License-Identifier: Apache-2.0
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
Shows how to use the AWS SDK for Python (Boto3) with Amazon Bedrock
5+
to list Amazon Bedrock managed prompts.
6+
"""
7+
8+
import argparse
9+
import boto3
10+
import logging
11+
12+
from prompt import list_prompts
13+
14+
logging.basicConfig(
15+
level=logging.INFO,
16+
format='%(levelname)s: %(message)s'
17+
)
18+
logger = logging.getLogger(__name__)
19+
20+
def main():
21+
"""
22+
Lists Amazon Bedrock managed prompts.
23+
"""
24+
parser = argparse.ArgumentParser(
25+
description="Lists Amazon Bedrock managed prompts."
26+
)
27+
parser.add_argument(
28+
'--region',
29+
default='us-east-1',
30+
help="The AWS Region to use."
31+
)
32+
args = parser.parse_args()
33+
34+
# Use bedrock-agent client for prompt management
35+
bedrock_client = boto3.client('bedrock-agent', region_name=args.region)
36+
37+
try:
38+
39+
# List all prompts
40+
print("\n=== Amazon Bedrock Managed Prompts ===")
41+
prompts = list_prompts(bedrock_client)
42+
43+
if not prompts:
44+
print("No prompts found in this region.")
45+
return
46+
47+
for prompt in prompts:
48+
print(f"ID: {prompt['id']}")
49+
print(f"Name: {prompt['name']}")
50+
print(f"Description: {prompt.get('description', 'No description')}")
51+
print(f"ARN: {prompt.get('arn', 'No ARN')}")
52+
print(f"Created: {prompt.get('createdAt', 'Unknown')}")
53+
print(f"Updated: {prompt.get('updatedAt', 'Unknown')}")
54+
print(f"Version: {prompt.get('version', 'Unknown')}")
55+
print("-" * 40)
56+
57+
except Exception as e:
58+
logger.exception("Error listing prompts: %s", str(e))
59+
60+
if __name__ == "__main__":
61+
main()

0 commit comments

Comments
 (0)