Skip to content

Commit c909f8d

Browse files
refactor: (GenAI) Reorganized Safety Sample (Group С) (#12614)
* Created new Safety folder and new Sample with test * Remove stream parameter and fix the response.
1 parent ec304b7 commit c909f8d

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2024 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+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
18+
19+
20+
def generate_text() -> str:
21+
# [START generativeaionvertexai_gemini_safety_settings]
22+
import vertexai
23+
24+
from vertexai.generative_models import (
25+
GenerativeModel,
26+
HarmCategory,
27+
HarmBlockThreshold,
28+
Part,
29+
SafetySetting,
30+
)
31+
32+
# TODO(developer): Update and un-comment below line
33+
# PROJECT_ID = "your-project-id"
34+
vertexai.init(project=PROJECT_ID, location="us-central1")
35+
36+
model = GenerativeModel("gemini-1.5-flash-001")
37+
38+
# Safety config
39+
safety_config = [
40+
SafetySetting(
41+
category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
42+
threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
43+
),
44+
SafetySetting(
45+
category=HarmCategory.HARM_CATEGORY_HARASSMENT,
46+
threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
47+
),
48+
]
49+
50+
image_file = Part.from_uri(
51+
"gs://cloud-samples-data/generative-ai/image/scones.jpg", "image/jpeg"
52+
)
53+
54+
# Generate content
55+
response = model.generate_content(
56+
[image_file, "What is in this image?"],
57+
safety_settings=safety_config,
58+
)
59+
60+
print(response.text)
61+
print(response.candidates[0].safety_ratings)
62+
# Example response:
63+
# The image contains a beautiful arrangement of blueberry scones, flowers, coffee, and blueberries.
64+
# The scene is set on a rustic blue background. The image evokes a sense of comfort and indulgence.
65+
# ...
66+
67+
# [END generativeaionvertexai_gemini_safety_settings]
68+
return response.text
69+
70+
71+
if __name__ == "__main__":
72+
generate_text()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2024 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 safety_config_example
16+
17+
18+
def test_gemini_safety_config_example() -> None:
19+
text = safety_config_example.generate_text()
20+
assert len(text) > 0

0 commit comments

Comments
 (0)