|
350 | 350 | "})" |
351 | 351 | ] |
352 | 352 | }, |
| 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 | + }, |
353 | 405 | { |
354 | 406 | "cell_type": "code", |
355 | 407 | "execution_count": null, |
|
361 | 413 | "try:\n", |
362 | 414 | " # Try to get the guardrail\n", |
363 | 415 | " response = guardrail_client.list_guardrails()\n", |
| 416 | + " guardrail_id = \"\"\n", |
364 | 417 | " for guardrail in response.get('guardrails', []):\n", |
365 | 418 | " if guardrail['name'] == guardrail_name:\n", |
366 | 419 | " 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", |
417 | 426 | " 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}\")" |
419 | 430 | ] |
420 | 431 | }, |
421 | 432 | { |
|
0 commit comments