|
| 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() |
0 commit comments