Skip to content

feat(modelarmor): created samples for model armor #13187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
/generative_ai/**/* @GoogleCloudPlatform/generative-ai-devrel @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/iam/cloud-client/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/model_armor/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-modelarmor-team
/media_cdn/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/model_garden/**/* @GoogleCloudPlatform/generative-ai-devrel @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/model_garden/**/* @GoogleCloudPlatform/generative-ai-devrel @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/parametermanager/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-secrets-team @GoogleCloudPlatform/cloud-parameters-team
/privateca/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/recaptcha_enterprise/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/recaptcha-customer-obsession-reviewers @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
Expand Down
2 changes: 2 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ assign_issues_by:
- "api: cloudkms"
- "api: iam"
- "api: kms"
- "api: modelarmor"
- "api: parametermanager"
- "api: privateca"
- "api: recaptchaenterprise"
Expand Down Expand Up @@ -157,6 +158,7 @@ assign_prs_by:
- "api: cloudkms"
- "api: iam"
- "api: kms"
- "api: modelarmor"
- "api: parametermanager"
- "api: privateca"
- "api: recaptchaenterprise"
Expand Down
10 changes: 10 additions & 0 deletions model_armor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Sample Snippets for Model Armor API

## Quick Start

In order to run these samples, you first need to go through the following steps:

1. [Select or create a Cloud Platform project.](https://console.cloud.google.com/project)
2. [Enable billing for your project.](https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project)
3. [Enable the Model Armor API.](https://cloud.google.com/security-command-center/docs/get-started-model-armor#enable-model-armor)
4. [Setup Authentication.](https://googleapis.dev/python/google-api-core/latest/auth.html)
68 changes: 0 additions & 68 deletions model_armor/create_template.py

This file was deleted.

1 change: 0 additions & 1 deletion model_armor/requirements.txt

This file was deleted.

61 changes: 0 additions & 61 deletions model_armor/sanitize_user_prompt.py

This file was deleted.

84 changes: 84 additions & 0 deletions model_armor/snippets/create_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Sample code for creating a new model armor template.
"""

from google.cloud import modelarmor_v1


def create_model_armor_template(
project_id: str,
location: str,
template_id: str,
) -> modelarmor_v1.Template:
"""Create a new Model Armor template.

Args:
project_id (str): Google Cloud project ID.
location (str): Google Cloud location.
template_id (str): ID for the template to create.

Returns:
Template: The created template.
"""
# [START modelarmor_create_template]

from google.api_core.client_options import ClientOptions
from google.cloud import modelarmor_v1

# TODO(Developer): Uncomment these variables.
# project_id = "your-google-cloud-project-id"
# location = "us-central1"
# template_id = "template_id"

# Create the Model Armor client.
client = modelarmor_v1.ModelArmorClient(
transport="rest",
client_options=ClientOptions(
api_endpoint=f"modelarmor.{location}.rep.googleapis.com"
),
)

# Build the Model Armor template with your preferred filters.
# For more details on filters, please refer to the following doc:
# https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
template = modelarmor_v1.Template(
filter_config=modelarmor_v1.FilterConfig(
pi_and_jailbreak_filter_settings=modelarmor_v1.PiAndJailbreakFilterSettings(
filter_enforcement=modelarmor_v1.PiAndJailbreakFilterSettings.PiAndJailbreakFilterEnforcement.ENABLED,
confidence_level=modelarmor_v1.DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
),
malicious_uri_filter_settings=modelarmor_v1.MaliciousUriFilterSettings(
filter_enforcement=modelarmor_v1.MaliciousUriFilterSettings.MaliciousUriFilterEnforcement.ENABLED,
),
),
)

# Prepare the request for creating the template.
request = modelarmor_v1.CreateTemplateRequest(
parent=f"projects/{project_id}/locations/{location}",
template_id=template_id,
template=template,
)

# Create the template.
response = client.create_template(request=request)

# Print the new template name.
print(f"Created template: {response.name}")

# [END modelarmor_create_template]

return response
Loading