forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_alias.py
More file actions
182 lines (141 loc) · 5.41 KB
/
Copy pathflow_alias.py
File metadata and controls
182 lines (141 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# 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 the Amazon Bedrock Agents Runtime
to use an alias with an Amazon Bedrock flow.
"""
import logging
from botocore.exceptions import ClientError
# Create a logger instance.
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.bedrock-agent.create_flow_alias]
def create_flow_alias(client, flow_id, flow_version, name, description):
"""
Creates an alias for an Amazon Bedrock flow.
Args:
client: bedrock agent boto3 client.
flow_id (str): The identifier of the flow.
Returns:
str: The ID for the flow alias.
"""
try:
logger.info("Creating flow alias for flow: %s.", flow_id)
response = client.create_flow_alias(
flowIdentifier=flow_id,
name=name,
description=description,
routingConfiguration=[
{
"flowVersion": flow_version
}
]
)
logger.info("Successfully created flow alias for %s.", flow_id)
return response['id']
except ClientError as e:
logging.exception("Client error creating alias for flow: %s - %s",
flow_id, str(e))
raise
except Exception as e:
logging.exception("Unexpected error creating alias for flow : %s - %s",
flow_id, str(e))
raise
# snippet-end:[python.example_code.bedrock-agent.create_flow_alias]
# snippet-start:[python.example_code.bedrock-agent.update_flow_alias]
def update_flow_alias(client, flow_id, alias_id, flow_version, name, description):
"""
Updates an alias for an Amazon Bedrock flow.
Args:
client: bedrock agent boto3 client.
flow_id (str): The identifier of the flow.
Returns:
str: The response from UpdateFlowAlias.
"""
try:
logger.info("Updating flow alias %s for flow: %s.", alias_id, flow_id)
response = client.update_flow_alias(
aliasIdentifier=alias_id,
flowIdentifier=flow_id,
name=name,
description=description,
routingConfiguration=[
{
"flowVersion": flow_version
}
]
)
logger.info("Successfully updated flow alias %s for %s.", alias_id, flow_id)
return response
except ClientError as e:
logging.exception("Client error updating alias %s for flow: %s - %s",
alias_id, flow_id, str(e))
raise
except Exception as e:
logging.exception("Unexpected error updating alias %s for flow : %s - %s",
alias_id, flow_id, str(e))
raise
# snippet-end:[python.example_code.bedrock-agent.update_flow_alias]
# snippet-start:[python.example_code.bedrock-agent.delete_flow_alias]
def delete_flow_alias(client, flow_id, flow_alias_id):
"""
Deletes an Amazon Bedrock flow alias.
Args:
client: bedrock agent boto3 client.
flow_id (str): The identifier of the flow.
Returns:
dict: The response from the call to DetectFLowAlias
"""
try:
logger.info("Deleting flow alias %s for flow: %s.", flow_alias_id, flow_id)
# Delete the flow alias.
response = client.delete_flow_alias(
aliasIdentifier=flow_alias_id,
flowIdentifier=flow_id
)
logging.info("Successfully deleted flow version for %s.", flow_id)
return response
except ClientError as e:
logging.exception("Client error deleting flow version: %s", str(e))
raise
except Exception as e:
logging.exception("Unexpected deleting flow version: %s", str(e))
raise
# snippet-end:[python.example_code.bedrock-agent.delete_flow_alias]
# snippet-start:[python.example_code.bedrock-agent.list_flow_aliases]
def list_flow_aliases(client, flow_id):
"""
Lists the aliases of an Amazon Bedrock flow.
Args:
client: bedrock agent boto3 client.
flow_id (str): The identifier of the flow.
Returns:
dict: The response from ListFlowAliases.
"""
try:
finished = False
logger.info("Listing flow aliases for flow: %s.", flow_id)
print(f"Aliases for flow: {flow_id}")
response = client.list_flow_aliases(
flowIdentifier=flow_id,
maxResults=10)
while finished is False:
for alias in response['flowAliasSummaries']:
print(f"Alias Name: {alias['name']}")
print(f"ID: {alias['id']}")
print(f"Description: {alias.get('description', 'No description')}\n")
if 'nextToken' in response:
next_token = response['nextToken']
response = client.list_flow_aliases(maxResults=10,
nextToken=next_token)
else:
finished = True
logging.info("Successfully listed flow aliases for flow %s.",
flow_id)
return response
except ClientError as e:
logging.exception("Client error listing flow aliases: %s", str(e))
raise
except Exception as e:
logging.exception("Unexpected error listing flow aliases: %s", str(e))
raise
# snippet-end:[python.example_code.bedrock-agent.list_flow_aliases]