SEMOSS / Semoss

Apache License 2.0
28 stars 5 forks source link

Add Support for Dynamic Safety Settings for Gemini Models #162

Closed cramaswa closed 1 month ago

cramaswa commented 1 month ago

This PR introduces enhancements to the VertexGenerativeModelClient and AbstractVertextAiTextGeneration classes to support the inclusion of safety settings. These settings can be passed directly in the Init Script text box of the Add Model user interface, allowing for dynamic configuration of safety parameters. The category names and thresholds are case-insensitive, ensuring flexibility in input.

Changes Made AbstractVertextAiTextGeneration Class: Added an optional safety_settings parameter to the constructor. The safety_settings parameter is stored in the instance variable self.safety_settings.

VertexGenerativeModelClient Class: Added logic to map the input safety settings to the corresponding enums (HarmCategory and HarmBlockThreshold) from gapic_content_types. The input category names and thresholds are converted to uppercase to match the enum names, making the input case-insensitive. Added error handling to ensure that invalid categories or thresholds do not break the logic. Invalid entries are logged, and the process continues with valid settings.

Sample Init Script with safety settings: import genai_client;${VAR_NAME} = genai_client.VertexClient(model_name = '${MODEL}', service_account_key_file = '${SERVICE_ACCOUNT_FILE}', region='${GCP_REGION}', chat_type='${CHAT_TYPE}', safety_settings={"HARM_CATEGORY_DANGEROUS_CONTENT": "BLOCK_NONE", "HARM_CATEGORY_HATE_SPEECH": "BLOCK_NONE", "HARM_CATEGORY_HARASSMENT": "BLOCK_NONE", "HARM_CATEGORY_SEXUALLY_EXPLICIT": "BLOCK_NONE"}) Note: safety_settings is optional and there is no restriction on the number of categories to be passed. You could choose to pass one or all the categories.

https://ai.google.dev/gemini-api/docs/safety-settings#code-examples