Skip to content

Commit 67485b7

Browse files
authored
docs(genai): Add Gemini Safety Sample (#13156)
* docs(genai): Add Gemini Safety Sample * Update safety sample to use latest SDK version and typing for SafetySetting/HttpOptions
1 parent 82073fc commit 67485b7

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

genai/safety/noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}

genai/safety/requirements-test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-api-core==2.24.0
2+
pytest==8.2.0

genai/safety/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==1.2.0

genai/safety/safety_with_txt.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
# https://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+
from google.genai.types import GenerateContentResponse
16+
17+
18+
def generate_content() -> GenerateContentResponse:
19+
# [START googlegenaisdk_safety_with_txt]
20+
from google import genai
21+
from google.genai.types import (
22+
GenerateContentConfig,
23+
HarmCategory,
24+
HarmBlockThreshold,
25+
HttpOptions,
26+
SafetySetting,
27+
)
28+
29+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
30+
31+
system_instruction = "Be as mean as possible."
32+
33+
prompt = """
34+
Write a list of 5 disrespectful things that I might say to the universe after stubbing my toe in the dark.
35+
"""
36+
37+
safety_settings = [
38+
SafetySetting(
39+
category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
40+
threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
41+
),
42+
SafetySetting(
43+
category=HarmCategory.HARM_CATEGORY_HARASSMENT,
44+
threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
45+
),
46+
SafetySetting(
47+
category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
48+
threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
49+
),
50+
SafetySetting(
51+
category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
52+
threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
53+
),
54+
]
55+
56+
response = client.models.generate_content(
57+
model="gemini-2.0-flash-001",
58+
contents=prompt,
59+
config=GenerateContentConfig(
60+
system_instruction=system_instruction,
61+
safety_settings=safety_settings,
62+
),
63+
)
64+
65+
# Response will be `None` if it is blocked.
66+
print(response.text)
67+
# Finish Reason will be `SAFETY` if it is blocked.
68+
print(response.candidates[0].finish_reason)
69+
# Safety Ratings show the levels for each filter.
70+
for safety_rating in response.candidates[0].safety_ratings:
71+
print(safety_rating)
72+
73+
# Example response:
74+
# None
75+
# FinishReason.SAFETY
76+
# blocked=None category=<HarmCategory.HARM_CATEGORY_HATE_SPEECH: 'HARM_CATEGORY_HATE_SPEECH'> probability=<HarmProbability.NEGLIGIBLE: 'NEGLIGIBLE'> probability_score=1.767152e-05 severity=<HarmSeverity.HARM_SEVERITY_NEGLIGIBLE: 'HARM_SEVERITY_NEGLIGIBLE'> severity_score=None
77+
# blocked=None category=<HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: 'HARM_CATEGORY_DANGEROUS_CONTENT'> probability=<HarmProbability.NEGLIGIBLE: 'NEGLIGIBLE'> probability_score=2.399503e-06 severity=<HarmSeverity.HARM_SEVERITY_NEGLIGIBLE: 'HARM_SEVERITY_NEGLIGIBLE'> severity_score=0.061250776
78+
# blocked=True category=<HarmCategory.HARM_CATEGORY_HARASSMENT: 'HARM_CATEGORY_HARASSMENT'> probability=<HarmProbability.MEDIUM: 'MEDIUM'> probability_score=0.64129734 severity=<HarmSeverity.HARM_SEVERITY_MEDIUM: 'HARM_SEVERITY_MEDIUM'> severity_score=0.2686556
79+
# blocked=None category=<HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: 'HARM_CATEGORY_SEXUALLY_EXPLICIT'> probability=<HarmProbability.NEGLIGIBLE: 'NEGLIGIBLE'> probability_score=5.2786977e-06 severity=<HarmSeverity.HARM_SEVERITY_NEGLIGIBLE: 'HARM_SEVERITY_NEGLIGIBLE'> severity_score=0.043162167
80+
81+
# [END googlegenaisdk_safety_with_txt]
82+
return response
83+
84+
85+
if __name__ == "__main__":
86+
generate_content()

genai/safety/test_safety_samples.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
# https://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+
import os
16+
17+
import safety_with_txt
18+
19+
20+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
21+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
22+
# The project name is included in the CICD pipeline
23+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
24+
25+
26+
def test_safety_with_txt() -> None:
27+
response = safety_with_txt.generate_content()
28+
assert response

0 commit comments

Comments
 (0)