gunpal5 / Google_GenerativeAI

Unofficial C# Google Generative AI SDK (Google Gemini) with function calls support
MIT License
34 stars 4 forks source link

Question About Defining Custom Safety Settings in Version 1.12 #3

Closed VinceMack closed 6 months ago

VinceMack commented 6 months ago

In your Enums.cs file, I noticed that you’ve defined enumeration types for safety settings. However, it appears there isn’t a straightforward way to specify these safety settings for the model which, from what I can tell, appears to be using the defaults from Google. Does version 1.12 of the your unofficial SDK accommodate customizing the safety settings?

gunpal5 commented 6 months ago

Hi,

You can pass safety settings for Model in constructor. e.g.

var model = new GenerativeModel(apiKey, new ModelParams()
{
    SafetySettings = new[]
    {
        new SafetySetting()
        {
            Category = HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
            Threshold = HarmBlockThreshold.BLOCK_ONLY_HIGH
        }
    }
});

Alternatively, you can create a request manually to specify the safety settings. e.g.

var model = new GenerativeModel(apiKey);

var res = await model.GenerateContentAsync(new GenerateContentRequest()
{
    Contents = new[] { RequestExtensions.FormatGenerateContentInput("Tell me about terrorism") },
    SafetySettings = new[]
    {
        new SafetySetting()
        {
            Category = HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
            Threshold = HarmBlockThreshold.BLOCK_ONLY_HIGH
        }
    }
});

if you have any suggestion to simplify this, I would happily implement it into the library.