Skip to content

Commit 8d57474

Browse files
fixed bug in guardrail setup
1 parent 3d777b3 commit 8d57474

File tree

2 files changed

+185
-71
lines changed

2 files changed

+185
-71
lines changed

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_05_fmops/05.01_fine-tuning-pipeline.ipynb

Lines changed: 125 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
" \n",
2525
"\n",
2626
"The following figure shows the overview of the solution.\n",
27+
"\n",
2728
"![](./ml-16670-arch-with-mlflow.png)"
2829
]
2930
},
@@ -48,7 +49,15 @@
4849
{
4950
"cell_type": "code",
5051
"execution_count": null,
51-
"metadata": {},
52+
"metadata": {
53+
"execution": {
54+
"iopub.execute_input": "2025-10-15T17:56:50.196738Z",
55+
"iopub.status.busy": "2025-10-15T17:56:50.196483Z",
56+
"iopub.status.idle": "2025-10-15T17:56:58.331883Z",
57+
"shell.execute_reply": "2025-10-15T17:56:58.331192Z",
58+
"shell.execute_reply.started": "2025-10-15T17:56:50.196718Z"
59+
}
60+
},
5261
"outputs": [],
5362
"source": [
5463
"%pip install -r ./scripts/requirements.txt --upgrade --quiet"
@@ -57,7 +66,15 @@
5766
{
5867
"cell_type": "code",
5968
"execution_count": null,
60-
"metadata": {},
69+
"metadata": {
70+
"execution": {
71+
"iopub.execute_input": "2025-10-15T17:56:58.332985Z",
72+
"iopub.status.busy": "2025-10-15T17:56:58.332771Z",
73+
"iopub.status.idle": "2025-10-15T17:56:58.338882Z",
74+
"shell.execute_reply": "2025-10-15T17:56:58.338429Z",
75+
"shell.execute_reply.started": "2025-10-15T17:56:58.332962Z"
76+
}
77+
},
6178
"outputs": [],
6279
"source": [
6380
"from IPython import get_ipython\n",
@@ -76,7 +93,15 @@
7693
{
7794
"cell_type": "code",
7895
"execution_count": null,
79-
"metadata": {},
96+
"metadata": {
97+
"execution": {
98+
"iopub.execute_input": "2025-10-15T18:10:38.890122Z",
99+
"iopub.status.busy": "2025-10-15T18:10:38.889793Z",
100+
"iopub.status.idle": "2025-10-15T18:10:40.611213Z",
101+
"shell.execute_reply": "2025-10-15T18:10:40.610667Z",
102+
"shell.execute_reply.started": "2025-10-15T18:10:38.890099Z"
103+
}
104+
},
80105
"outputs": [],
81106
"source": [
82107
"import os\n",
@@ -108,13 +133,21 @@
108133
{
109134
"cell_type": "code",
110135
"execution_count": null,
111-
"metadata": {},
136+
"metadata": {
137+
"execution": {
138+
"iopub.execute_input": "2025-10-15T18:12:29.689442Z",
139+
"iopub.status.busy": "2025-10-15T18:12:29.689175Z",
140+
"iopub.status.idle": "2025-10-15T18:12:30.791433Z",
141+
"shell.execute_reply": "2025-10-15T18:12:30.790896Z",
142+
"shell.execute_reply.started": "2025-10-15T18:12:29.689422Z"
143+
}
144+
},
112145
"outputs": [],
113146
"source": [
114147
"sagemaker_session = sagemaker.session.Session()\n",
115148
"role = sagemaker.get_execution_role()\n",
116149
"instance_type = \"ml.m5.xlarge\"\n",
117-
"pipeline_name = \"AIM405-qwen3-finetune-pipeline\"\n",
150+
"pipeline_name = \"qwen3-finetune-pipeline\"\n",
118151
"bucket_name = sagemaker_session.default_bucket()\n",
119152
"default_prefix = sagemaker_session.default_bucket_prefix\n",
120153
"if default_prefix:\n",
@@ -152,7 +185,15 @@
152185
{
153186
"cell_type": "code",
154187
"execution_count": null,
155-
"metadata": {},
188+
"metadata": {
189+
"execution": {
190+
"iopub.execute_input": "2025-10-15T18:12:30.838220Z",
191+
"iopub.status.busy": "2025-10-15T18:12:30.837950Z",
192+
"iopub.status.idle": "2025-10-15T18:12:31.008197Z",
193+
"shell.execute_reply": "2025-10-15T18:12:31.007542Z",
194+
"shell.execute_reply.started": "2025-10-15T18:12:30.838200Z"
195+
}
196+
},
156197
"outputs": [],
157198
"source": [
158199
"mlflow_tracking_server_arn = \"<REPLACE WITH YOUR ARN>\"\n",
@@ -180,7 +221,15 @@
180221
{
181222
"cell_type": "code",
182223
"execution_count": null,
183-
"metadata": {},
224+
"metadata": {
225+
"execution": {
226+
"iopub.execute_input": "2025-10-15T18:12:32.608898Z",
227+
"iopub.status.busy": "2025-10-15T18:12:32.608642Z",
228+
"iopub.status.idle": "2025-10-15T18:12:32.612632Z",
229+
"shell.execute_reply": "2025-10-15T18:12:32.612142Z",
230+
"shell.execute_reply.started": "2025-10-15T18:12:32.608878Z"
231+
}
232+
},
184233
"outputs": [],
185234
"source": [
186235
"%%writefile config.yaml\n",
@@ -203,7 +252,15 @@
203252
{
204253
"cell_type": "code",
205254
"execution_count": null,
206-
"metadata": {},
255+
"metadata": {
256+
"execution": {
257+
"iopub.execute_input": "2025-10-15T18:12:33.245143Z",
258+
"iopub.status.busy": "2025-10-15T18:12:33.244889Z",
259+
"iopub.status.idle": "2025-10-15T18:12:33.247752Z",
260+
"shell.execute_reply": "2025-10-15T18:12:33.247285Z",
261+
"shell.execute_reply.started": "2025-10-15T18:12:33.245122Z"
262+
}
263+
},
207264
"outputs": [],
208265
"source": [
209266
"# Set path to config file\n",
@@ -221,6 +278,13 @@
221278
"cell_type": "code",
222279
"execution_count": null,
223280
"metadata": {
281+
"execution": {
282+
"iopub.execute_input": "2025-10-15T18:12:34.760569Z",
283+
"iopub.status.busy": "2025-10-15T18:12:34.760310Z",
284+
"iopub.status.idle": "2025-10-15T18:12:35.448363Z",
285+
"shell.execute_reply": "2025-10-15T18:12:35.447833Z",
286+
"shell.execute_reply.started": "2025-10-15T18:12:34.760549Z"
287+
},
224288
"scrolled": true
225289
},
226290
"outputs": [],
@@ -362,7 +426,15 @@
362426
{
363427
"cell_type": "code",
364428
"execution_count": null,
365-
"metadata": {},
429+
"metadata": {
430+
"execution": {
431+
"iopub.execute_input": "2025-10-15T18:12:37.083014Z",
432+
"iopub.status.busy": "2025-10-15T18:12:37.082756Z",
433+
"iopub.status.idle": "2025-10-15T18:12:37.095488Z",
434+
"shell.execute_reply": "2025-10-15T18:12:37.094990Z",
435+
"shell.execute_reply.started": "2025-10-15T18:12:37.082991Z"
436+
}
437+
},
366438
"outputs": [],
367439
"source": [
368440
"%%bash\n",
@@ -440,7 +512,15 @@
440512
{
441513
"cell_type": "code",
442514
"execution_count": null,
443-
"metadata": {},
515+
"metadata": {
516+
"execution": {
517+
"iopub.execute_input": "2025-10-15T18:12:40.136027Z",
518+
"iopub.status.busy": "2025-10-15T18:12:40.135746Z",
519+
"iopub.status.idle": "2025-10-15T18:12:40.542117Z",
520+
"shell.execute_reply": "2025-10-15T18:12:40.541629Z",
521+
"shell.execute_reply.started": "2025-10-15T18:12:40.136005Z"
522+
}
523+
},
444524
"outputs": [],
445525
"source": [
446526
"from sagemaker.s3 import S3Uploader\n",
@@ -481,17 +561,33 @@
481561
{
482562
"cell_type": "code",
483563
"execution_count": null,
484-
"metadata": {},
564+
"metadata": {
565+
"execution": {
566+
"iopub.execute_input": "2025-10-15T18:12:43.868659Z",
567+
"iopub.status.busy": "2025-10-15T18:12:43.868403Z",
568+
"iopub.status.idle": "2025-10-15T18:12:44.216288Z",
569+
"shell.execute_reply": "2025-10-15T18:12:44.215721Z",
570+
"shell.execute_reply.started": "2025-10-15T18:12:43.868639Z"
571+
}
572+
},
485573
"outputs": [],
486574
"source": [
487575
"from steps import pipeline_utils\n",
488-
"guardrail_id, guardrail_version =pipeline_utils.get_or_create_guardrail()"
576+
"guardrail_id, guardrail_version = pipeline_utils.get_or_create_guardrail()"
489577
]
490578
},
491579
{
492580
"cell_type": "code",
493581
"execution_count": null,
494-
"metadata": {},
582+
"metadata": {
583+
"execution": {
584+
"iopub.execute_input": "2025-10-15T18:13:22.801488Z",
585+
"iopub.status.busy": "2025-10-15T18:13:22.801207Z",
586+
"iopub.status.idle": "2025-10-15T18:13:25.970853Z",
587+
"shell.execute_reply": "2025-10-15T18:13:25.970278Z",
588+
"shell.execute_reply.started": "2025-10-15T18:13:22.801468Z"
589+
}
590+
},
495591
"outputs": [],
496592
"source": [
497593
"from steps import (\n",
@@ -618,6 +714,13 @@
618714
"cell_type": "code",
619715
"execution_count": null,
620716
"metadata": {
717+
"execution": {
718+
"iopub.execute_input": "2025-10-15T18:13:25.972041Z",
719+
"iopub.status.busy": "2025-10-15T18:13:25.971610Z",
720+
"iopub.status.idle": "2025-10-15T18:13:38.971434Z",
721+
"shell.execute_reply": "2025-10-15T18:13:38.970889Z",
722+
"shell.execute_reply.started": "2025-10-15T18:13:25.972020Z"
723+
},
621724
"scrolled": true
622725
},
623726
"outputs": [],
@@ -637,7 +740,15 @@
637740
{
638741
"cell_type": "code",
639742
"execution_count": null,
640-
"metadata": {},
743+
"metadata": {
744+
"execution": {
745+
"iopub.execute_input": "2025-10-15T18:13:38.972413Z",
746+
"iopub.status.busy": "2025-10-15T18:13:38.972199Z",
747+
"iopub.status.idle": "2025-10-15T18:13:39.429711Z",
748+
"shell.execute_reply": "2025-10-15T18:13:39.429119Z",
749+
"shell.execute_reply.started": "2025-10-15T18:13:38.972394Z"
750+
}
751+
},
641752
"outputs": [],
642753
"source": [
643754
"execution = pipeline.start()"

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_05_fmops/steps/pipeline_utils.py

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -78,62 +78,65 @@ def invoke_sagemaker_endpoint(payload, endpoint_name):
7878
def get_or_create_guardrail():
7979
guardrail_client = boto3.client('bedrock')
8080
guardrail_name = "ExampleMedicalGuardrail"
81-
try:
82-
# Try to get the guardrail
83-
response = guardrail_client.list_guardrails()
84-
for guardrail in response.get('guardrails', []):
85-
if guardrail['name'] == guardrail_name:
86-
guardrail_id = guardrail['id']
87-
response = guardrail_client.get_guardrail(
88-
guardrailIdentifier=guardrail_id
89-
)
90-
guardrail_version = response["version"]
91-
print(f"Found Guardrail {guardrail_id}:{guardrail_version}")
92-
except botocore.exceptions.ClientError as e:
93-
if e.response['Error']['Code'] == 'ResourceNotFoundException':
94-
# Guardrail doesn't exist, create it
95-
try:
96-
guardrail = guardrail_client.create_guardrail(
97-
name="ExampleMedicalGuardrail",
98-
description='Example of a Guardrail for Medical Use Cases',
99-
topicPolicyConfig={
100-
'topicsConfig': [{
101-
'name': 'Block Pharmaceuticals',
102-
'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',
103-
'type': 'DENY',
81+
82+
guardrail_id = None
83+
guardrail_version = None
84+
85+
# Try to get the guardrail
86+
response = guardrail_client.list_guardrails()
87+
for guardrail in response.get('guardrails', []):
88+
if guardrail['name'] == guardrail_name:
89+
guardrail_id = guardrail['id']
90+
response = guardrail_client.get_guardrail(
91+
guardrailIdentifier=guardrail_id
92+
)
93+
guardrail_version = response["version"]
94+
print(f"Found Guardrail {guardrail_id}:{guardrail_version}")
95+
break
96+
97+
if not guardrail_id:
98+
try:
99+
guardrail = guardrail_client.create_guardrail(
100+
name="ExampleMedicalGuardrail",
101+
description='Example of a Guardrail for Medical Use Cases',
102+
topicPolicyConfig={
103+
'topicsConfig': [{
104+
'name': 'Block Pharmaceuticals',
105+
'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',
106+
'type': 'DENY',
107+
'inputAction': 'BLOCK',
108+
'outputAction': 'BLOCK',
109+
}]
110+
},
111+
sensitiveInformationPolicyConfig={
112+
'piiEntitiesConfig': [
113+
{
114+
'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',
115+
'action': 'BLOCK',
104116
'inputAction': 'BLOCK',
105-
'outputAction': 'BLOCK',
106-
}]
107-
},
108-
sensitiveInformationPolicyConfig={
109-
'piiEntitiesConfig': [
110-
{
111-
'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',
112-
'action': 'BLOCK',
113-
'inputAction': 'BLOCK',
114-
'outputAction': 'BLOCK'
115-
},
116-
]
117-
},
118-
contextualGroundingPolicyConfig={
119-
'filtersConfig': [
120-
{
121-
'type': 'RELEVANCE',
122-
'threshold': 0.9,
123-
'action': 'BLOCK',
124-
'enabled': True
125-
},
126-
]
127-
},
128-
blockedInputMessaging="ExampleMedicalGuardrail has blocked this input.",
129-
blockedOutputsMessaging="ExampleMedicalGuardrail has blocked this output."
130-
)
131-
guardrail_id = guardrail['guardrailId']
132-
guardrail_version = guardrail['version']
133-
134-
print(f"Created new guardrail '{guardrail_id}:{guardrail_version}'")
135-
except botocore.exceptions.ClientError as create_error:
136-
print(f"Error creating guardrail: {create_error}")
137-
else:
138-
print(f"Error checking guardrail: {e}")
117+
'outputAction': 'BLOCK'
118+
},
119+
]
120+
},
121+
contextualGroundingPolicyConfig={
122+
'filtersConfig': [
123+
{
124+
'type': 'RELEVANCE',
125+
'threshold': 0.9,
126+
'action': 'BLOCK',
127+
'enabled': True
128+
},
129+
]
130+
},
131+
blockedInputMessaging="ExampleMedicalGuardrail has blocked this input.",
132+
blockedOutputsMessaging="ExampleMedicalGuardrail has blocked this output."
133+
)
134+
135+
guardrail_id = guardrail['guardrailId']
136+
guardrail_version = guardrail['version']
137+
138+
print(f"Created new guardrail '{guardrail_id}:{guardrail_version}'")
139+
except botocore.exceptions.ClientError as create_error:
140+
print(f"Error creating guardrail: {create_error}")
141+
139142
return guardrail_id, guardrail_version

0 commit comments

Comments
 (0)