|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +Sample code for creating a new model armor template. |
| 16 | +""" |
| 17 | + |
| 18 | +from google.cloud import modelarmor_v1 |
| 19 | + |
| 20 | + |
| 21 | +def create_model_armor_template( |
| 22 | + project_id: str, |
| 23 | + location: str, |
| 24 | + template_id: str, |
| 25 | +) -> modelarmor_v1.Template: |
| 26 | + """Create a new Model Armor template. |
| 27 | +
|
| 28 | + Args: |
| 29 | + project_id (str): Google Cloud project ID. |
| 30 | + location (str): Google Cloud location. |
| 31 | + template_id (str): ID for the template to create. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + Template: The created template. |
| 35 | + """ |
| 36 | + # [START modelarmor_create_template] |
| 37 | + |
| 38 | + from google.api_core.client_options import ClientOptions |
| 39 | + from google.cloud import modelarmor_v1 |
| 40 | + |
| 41 | + # TODO(Developer): Uncomment these variables. |
| 42 | + # project_id = "your-google-cloud-project-id" |
| 43 | + # location = "us-central1" |
| 44 | + # template_id = "template_id" |
| 45 | + |
| 46 | + # Create the Model Armor client. |
| 47 | + client = modelarmor_v1.ModelArmorClient( |
| 48 | + transport="rest", |
| 49 | + client_options=ClientOptions( |
| 50 | + api_endpoint=f"modelarmor.{location}.rep.googleapis.com" |
| 51 | + ), |
| 52 | + ) |
| 53 | + |
| 54 | + # Build the Model Armor template with your preferred filters. |
| 55 | + # For more details on filters, please refer to the following doc: |
| 56 | + # https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters |
| 57 | + template = modelarmor_v1.Template( |
| 58 | + filter_config=modelarmor_v1.FilterConfig( |
| 59 | + pi_and_jailbreak_filter_settings=modelarmor_v1.PiAndJailbreakFilterSettings( |
| 60 | + filter_enforcement=modelarmor_v1.PiAndJailbreakFilterSettings.PiAndJailbreakFilterEnforcement.ENABLED, |
| 61 | + confidence_level=modelarmor_v1.DetectionConfidenceLevel.MEDIUM_AND_ABOVE, |
| 62 | + ), |
| 63 | + malicious_uri_filter_settings=modelarmor_v1.MaliciousUriFilterSettings( |
| 64 | + filter_enforcement=modelarmor_v1.MaliciousUriFilterSettings.MaliciousUriFilterEnforcement.ENABLED, |
| 65 | + ), |
| 66 | + ), |
| 67 | + ) |
| 68 | + |
| 69 | + # Prepare the request for creating the template. |
| 70 | + request = modelarmor_v1.CreateTemplateRequest( |
| 71 | + parent=f"projects/{project_id}/locations/{location}", |
| 72 | + template_id=template_id, |
| 73 | + template=template, |
| 74 | + ) |
| 75 | + |
| 76 | + # Create the template. |
| 77 | + response = client.create_template(request=request) |
| 78 | + |
| 79 | + # Print the new template name. |
| 80 | + print(f"Created template: {response.name}") |
| 81 | + |
| 82 | + # [END modelarmor_create_template] |
| 83 | + |
| 84 | + return response |
0 commit comments