Skip to content

Commit c1b7f79

Browse files
committed
Fixed guardrail id bug
1 parent a8c1207 commit c1b7f79

File tree

3 files changed

+119
-137
lines changed

3 files changed

+119
-137
lines changed

workshops/fine-tuning-with-sagemakerai-and-bedrock/task_05_fmops/05.00_fmops_examples.ipynb

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,58 @@
350350
"})"
351351
]
352352
},
353+
{
354+
"metadata": {},
355+
"cell_type": "code",
356+
"outputs": [],
357+
"execution_count": null,
358+
"source": [
359+
"def create_guardrail(guardrail_client):\n",
360+
" try:\n",
361+
" guardrail = guardrail_client.create_guardrail(\n",
362+
" name=\"ExampleMedicalGuardrail\",\n",
363+
" description='Example of a Guardrail for Medical Use Cases',\n",
364+
" topicPolicyConfig={\n",
365+
" 'topicsConfig': [{\n",
366+
" 'name': 'Block Pharmaceuticals',\n",
367+
" 'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',\n",
368+
" 'type': 'DENY',\n",
369+
" 'inputAction': 'BLOCK',\n",
370+
" 'outputAction': 'BLOCK',\n",
371+
" }]\n",
372+
" },\n",
373+
" sensitiveInformationPolicyConfig={\n",
374+
" 'piiEntitiesConfig': [\n",
375+
" {\n",
376+
" 'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',\n",
377+
" 'action': 'BLOCK',\n",
378+
" 'inputAction': 'BLOCK',\n",
379+
" 'outputAction': 'BLOCK'\n",
380+
" },\n",
381+
" ]\n",
382+
" },\n",
383+
" contextualGroundingPolicyConfig={\n",
384+
" 'filtersConfig': [\n",
385+
" {\n",
386+
" 'type': 'RELEVANCE',\n",
387+
" 'threshold': 0.9,\n",
388+
" 'action': 'BLOCK',\n",
389+
" 'enabled': True\n",
390+
" },\n",
391+
" ]\n",
392+
" },\n",
393+
" blockedInputMessaging=\"ExampleMedicalGuardrail has blocked this input.\",\n",
394+
" blockedOutputsMessaging=\"ExampleMedicalGuardrail has blocked this output.\"\n",
395+
" )\n",
396+
" guardrail_id = guardrail['guardrailId']\n",
397+
" guardrail_version = guardrail['version']\n",
398+
"\n",
399+
" print(f\"Created new guardrail '{guardrail_id}:{guardrail_version}'\")\n",
400+
" return guardrail_id, guardrail_version\n",
401+
" except botocore.exceptions.ClientError as create_error:\n",
402+
" print(f\"Error creating guardrail: {create_error}\")"
403+
]
404+
},
353405
{
354406
"cell_type": "code",
355407
"execution_count": null,
@@ -361,61 +413,20 @@
361413
"try:\n",
362414
" # Try to get the guardrail\n",
363415
" response = guardrail_client.list_guardrails()\n",
416+
" guardrail_id = \"\"\n",
364417
" for guardrail in response.get('guardrails', []):\n",
365418
" if guardrail['name'] == guardrail_name:\n",
366419
" guardrail_id = guardrail['id']\n",
367-
" response = guardrail_client.get_guardrail(\n",
368-
" guardrailIdentifier=guardrail_id\n",
369-
" )\n",
370-
" guardrail_version = response[\"version\"]\n",
371-
" print(f\"Found Guardrail {guardrail_id}:{guardrail_version}\")\n",
372-
"except botocore.exceptions.ClientError as e:\n",
373-
" if e.response['Error']['Code'] == 'ResourceNotFoundException':\n",
374-
" # Guardrail doesn't exist, create it\n",
375-
" try:\n",
376-
" guardrail = guardrail_client.create_guardrail(\n",
377-
" name=\"ExampleMedicalGuardrail\",\n",
378-
" description='Example of a Guardrail for Medical Use Cases',\n",
379-
" topicPolicyConfig={\n",
380-
" 'topicsConfig': [{\n",
381-
" 'name': 'Block Pharmaceuticals',\n",
382-
" 'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',\n",
383-
" 'type': 'DENY',\n",
384-
" 'inputAction': 'BLOCK',\n",
385-
" 'outputAction': 'BLOCK',\n",
386-
" }] \n",
387-
" },\n",
388-
" sensitiveInformationPolicyConfig={\n",
389-
" 'piiEntitiesConfig': [\n",
390-
" {\n",
391-
" 'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',\n",
392-
" 'action': 'BLOCK',\n",
393-
" 'inputAction': 'BLOCK',\n",
394-
" 'outputAction': 'BLOCK'\n",
395-
" },\n",
396-
" ]\n",
397-
" },\n",
398-
" contextualGroundingPolicyConfig={\n",
399-
" 'filtersConfig': [\n",
400-
" {\n",
401-
" 'type': 'RELEVANCE',\n",
402-
" 'threshold': 0.9,\n",
403-
" 'action': 'BLOCK',\n",
404-
" 'enabled': True\n",
405-
" },\n",
406-
" ]\n",
407-
" },\n",
408-
" blockedInputMessaging=\"ExampleMedicalGuardrail has blocked this input.\",\n",
409-
" blockedOutputsMessaging=\"ExampleMedicalGuardrail has blocked this output.\"\n",
410-
" )\n",
411-
" guardrail_id = guardrail['guardrailId']\n",
412-
" guardrail_version = guardrail['version']\n",
413-
" \n",
414-
" print(f\"Created new guardrail '{guardrail_id}:{guardrail_version}'\")\n",
415-
" except botocore.exceptions.ClientError as create_error:\n",
416-
" print(f\"Error creating guardrail: {create_error}\")\n",
420+
" if guardrail_id != \"\":\n",
421+
" response = guardrail_client.get_guardrail(\n",
422+
" guardrailIdentifier=guardrail_id\n",
423+
" )\n",
424+
" guardrail_version = response[\"version\"]\n",
425+
" print(f\"Found Guardrail {guardrail_id}:{guardrail_version}\")\n",
417426
" else:\n",
418-
" print(f\"Error checking guardrail: {e}\")"
427+
" guardrail_id, guardrail_version = create_guardrail(guardrail_client)\n",
428+
"except botocore.exceptions.ClientError as e:\n",
429+
" print(f\"Error checking guardrail: {e}\")"
419430
]
420431
},
421432
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@
485485
"outputs": [],
486486
"source": [
487487
"from steps import pipeline_utils\n",
488-
"guardrail_id, guardrail_version =pipeline_utils.get_or_create_guardrail()"
488+
"guardrail_id, guardrail_version = pipeline_utils.get_or_create_guardrail()"
489489
]
490490
},
491491
{

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

Lines changed: 56 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -62,39 +62,6 @@ def convert_to_messages(sample, system_prompt=""):
6262

6363
return sample
6464

65-
66-
# Template dataset to add prompt to each sample
67-
# def template_dataset(sample):
68-
# try:
69-
# sample["text"] = PROMPT_TEMPLATE.format(question=sample["Question"],
70-
# complex_cot=sample["Complex_CoT"],
71-
# answer=sample["Response"])
72-
# return sample
73-
# except KeyError as e:
74-
# print(f"KeyError in template_dataset: {str(e)}")
75-
# # Provide default values for missing fields
76-
# missing_key = str(e).strip("'")
77-
# if missing_key == "Question":
78-
# sample["text"] = PROMPT_TEMPLATE.format(
79-
# question="[Missing question]",
80-
# complex_cot=sample.get("Complex_CoT", "[Missing CoT]"),
81-
# answer=sample.get("Response", "[Missing response]")
82-
# )
83-
# elif missing_key == "Complex_CoT":
84-
# sample["text"] = PROMPT_TEMPLATE.format(
85-
# question=sample["Question"],
86-
# complex_cot="[Missing CoT]",
87-
# answer=sample.get("Response", "[Missing response]")
88-
# )
89-
# elif missing_key == "Response":
90-
# sample["text"] = PROMPT_TEMPLATE.format(
91-
# question=sample["Question"],
92-
# complex_cot=sample.get("Complex_CoT", "[Missing CoT]"),
93-
# answer="[Missing response]"
94-
# )
95-
# return sample
96-
97-
9865
def invoke_sagemaker_endpoint(payload, endpoint_name):
9966
"""
10067
Invoke a SageMaker endpoint with the given payload.
@@ -123,65 +90,69 @@ def invoke_sagemaker_endpoint(payload, endpoint_name):
12390
return None, -1
12491

12592

93+
def create_guardrail(guardrail_client):
94+
try:
95+
guardrail = guardrail_client.create_guardrail(
96+
name="ExampleMedicalGuardrail",
97+
description='Example of a Guardrail for Medical Use Cases',
98+
topicPolicyConfig={
99+
'topicsConfig': [{
100+
'name': 'Block Pharmaceuticals',
101+
'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',
102+
'type': 'DENY',
103+
'inputAction': 'BLOCK',
104+
'outputAction': 'BLOCK',
105+
}]
106+
},
107+
sensitiveInformationPolicyConfig={
108+
'piiEntitiesConfig': [
109+
{
110+
'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',
111+
'action': 'BLOCK',
112+
'inputAction': 'BLOCK',
113+
'outputAction': 'BLOCK'
114+
},
115+
]
116+
},
117+
contextualGroundingPolicyConfig={
118+
'filtersConfig': [
119+
{
120+
'type': 'RELEVANCE',
121+
'threshold': 0.9,
122+
'action': 'BLOCK',
123+
'enabled': True
124+
},
125+
]
126+
},
127+
blockedInputMessaging="ExampleMedicalGuardrail has blocked this input.",
128+
blockedOutputsMessaging="ExampleMedicalGuardrail has blocked this output."
129+
)
130+
guardrail_id = guardrail['guardrailId']
131+
guardrail_version = guardrail['version']
132+
133+
print(f"Created new guardrail '{guardrail_id}:{guardrail_version}'")
134+
return guardrail_id, guardrail_version
135+
except botocore.exceptions.ClientError as create_error:
136+
print(f"Error creating guardrail: {create_error}")
137+
138+
126139
def get_or_create_guardrail():
127140
guardrail_client = boto3.client('bedrock')
128141
guardrail_name = "ExampleMedicalGuardrail"
129142
try:
130143
# Try to get the guardrail
131144
response = guardrail_client.list_guardrails()
145+
guardrail_id = ""
132146
for guardrail in response.get('guardrails', []):
133147
if guardrail['name'] == guardrail_name:
134148
guardrail_id = guardrail['id']
135-
response = guardrail_client.get_guardrail(
136-
guardrailIdentifier=guardrail_id
137-
)
138-
guardrail_version = response["version"]
139-
print(f"Found Guardrail {guardrail_id}:{guardrail_version}")
140-
except botocore.exceptions.ClientError as e:
141-
if e.response['Error']['Code'] == 'ResourceNotFoundException':
142-
# Guardrail doesn't exist, create it
143-
try:
144-
guardrail = guardrail_client.create_guardrail(
145-
name="ExampleMedicalGuardrail",
146-
description='Example of a Guardrail for Medical Use Cases',
147-
topicPolicyConfig={
148-
'topicsConfig': [{
149-
'name': 'Block Pharmaceuticals',
150-
'definition': 'This model cannot recommend one pharmaceutical over another. Generic prescriptions consistent with medical expertise and clinical diagnoses only.',
151-
'type': 'DENY',
152-
'inputAction': 'BLOCK',
153-
'outputAction': 'BLOCK',
154-
}]
155-
},
156-
sensitiveInformationPolicyConfig={
157-
'piiEntitiesConfig': [
158-
{
159-
'type': 'UK_NATIONAL_HEALTH_SERVICE_NUMBER',
160-
'action': 'BLOCK',
161-
'inputAction': 'BLOCK',
162-
'outputAction': 'BLOCK'
163-
},
164-
]
165-
},
166-
contextualGroundingPolicyConfig={
167-
'filtersConfig': [
168-
{
169-
'type': 'RELEVANCE',
170-
'threshold': 0.9,
171-
'action': 'BLOCK',
172-
'enabled': True
173-
},
174-
]
175-
},
176-
blockedInputMessaging="ExampleMedicalGuardrail has blocked this input.",
177-
blockedOutputsMessaging="ExampleMedicalGuardrail has blocked this output."
178-
)
179-
guardrail_id = guardrail['guardrailId']
180-
guardrail_version = guardrail['version']
181-
182-
print(f"Created new guardrail '{guardrail_id}:{guardrail_version}'")
183-
except botocore.exceptions.ClientError as create_error:
184-
print(f"Error creating guardrail: {create_error}")
149+
if guardrail_id != "":
150+
response = guardrail_client.get_guardrail(
151+
guardrailIdentifier=guardrail_id
152+
)
153+
guardrail_version = response["version"]
154+
print(f"Found Guardrail {guardrail_id}:{guardrail_version}")
185155
else:
186-
print(f"Error checking guardrail: {e}")
187-
return guardrail_id, guardrail_version
156+
return create_guardrail(guardrail_client)
157+
except botocore.exceptions.ClientError as e:
158+
print(f"Error checking guardrail: {e}")

0 commit comments

Comments
 (0)