mscraftsman / generative-ai

Gemini AI SDK for .NET and ASP.NET Core enables developers to use Google's state-of-the-art generative AI models to build AI-powered features and applications.
https://mscraftsman.github.io/generative-ai/
Apache License 2.0
55 stars 11 forks source link

Correct usage of systemInstruction #27

Closed Setmaster closed 1 month ago

Setmaster commented 4 months ago

What is the correct way of using system instruction?

I tried ` var googleAI = new GoogleAI(apiKey: Environment.GetEnvironmentVariable("GOOGLE_API_KEY"));

        var systemInstruction = new List<Content>
        {
            new Content
            {
                Parts = new List<IPart>
                {
                    new TextData
                    {
                        Text = "You are a cat. Your name is Neko"
                    }
                }
            }
        };

        var model = googleAI.GenerativeModel(
            model: Environment.GetEnvironmentVariable("GOOGLE_AI_MODEL") ?? "gemini-1.5-flash",
            systemInstruction: systemInstruction
        );

`

But that gives errors. I tried it based on https://ai.google.dev/gemini-api/docs/system-instructions?lang=python

jochenkirstaetter commented 3 months ago

Hi @Setmaster

Apologies for the delay. A system instruction is used as a single Content (not as a list as you tried it), and passed into the method GenerativeModel. See the following code taken from the test case Generate_Content_SystemInstruction

// Arrange
var systemInstruction = new Content("You are a friendly pirate. Speak like one.");
var prompt = "Good morning! How are you?";
IGenerativeAI genAi = new GoogleAI(_fixture.ApiKey);
var model = genAi.GenerativeModel(_model, systemInstruction: systemInstruction);
var request = new GenerateContentRequest(prompt);

Hope this helps, JoKi

jochenkirstaetter commented 1 month ago

Hello @Setmaster

The README has been extended and explains now how to add a system instruction to the model.

Cheers, JoKi