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
48 stars 9 forks source link

Correct usage of systemInstruction #27

Open Setmaster opened 3 months ago

Setmaster commented 3 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 2 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