|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2023 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +from absl.testing import absltest |
| 16 | + |
| 17 | +import google.generativeai as genai |
| 18 | + |
| 19 | + |
| 20 | +class UnitTests(absltest.TestCase): |
| 21 | + def test_safety_settings(self): |
| 22 | + # [START safety_settings] |
| 23 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 24 | + unsafe_prompt = "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them." |
| 25 | + response = model.generate_content(unsafe_prompt, |
| 26 | + safety_settings={ |
| 27 | + "HARASSMENT": "BLOCK_ONLY_HIGH" |
| 28 | + }) |
| 29 | + print(response.candidates[0].finish_reason) |
| 30 | + print(response.candidates[0].safety_ratings) |
| 31 | + # [END safety_settings] |
| 32 | + |
| 33 | + def test_safety_settings_multi(self): |
| 34 | + # [START safety_settings_multi] |
| 35 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 36 | + unsafe_prompt = "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them." |
| 37 | + response = model.generate_content( |
| 38 | + unsafe_prompt, |
| 39 | + safety_settings={ |
| 40 | + "HATE": "MEDIUM", |
| 41 | + "HARASSMENT": "BLOCK_ONLY_HIGH", |
| 42 | + }, |
| 43 | + ) |
| 44 | + |
| 45 | + try: |
| 46 | + print(response.text) |
| 47 | + except: |
| 48 | + print("No information generated by the model.") |
| 49 | + |
| 50 | + print(response.candidates[0].safety_ratings) |
| 51 | + # [END safety_settings_multi] |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + absltest.main() |
0 commit comments