### Guide Model Behavior with System Instructions Source: https://github.com/mscraftsman/generative-ai/blob/main/docs/docs/text-generation.md Provide system instructions to guide the model's responses. This example sets the model to respond as a friendly pirate. ```csharp using Mscc.GenerativeAI; var systemInstruction = new Content("You are a friendly pirate. Speak like one."); var prompt = "Good morning! How are you?"; var googleAI = new GoogleAI(apiKey: "your API key"); var model = googleAI.GenerativeModel(model: Model.Gemini25Flash, systemInstruction: systemInstruction); var request = new GenerateContentRequest(prompt); var response = await model.GenerateContent(request); Console.WriteLine(response.Text); ``` -------------------------------- ### Run the Application Source: https://github.com/mscraftsman/generative-ai/blob/main/samples/Console.Gemini.FunctionCalling/README.md Execute the .NET application to start the interactive console experience. ```bash dotnet run ``` -------------------------------- ### User Input Example Source: https://github.com/mscraftsman/generative-ai/blob/main/samples/Console.Gemini.FunctionCalling/README.md Provide input to the application to test function calling. Questions that can be answered by the defined tools will trigger tool calls. ```text User > What is my name? ``` -------------------------------- ### Install Mscc.GenerativeAI.Web via NuGet Package Manager Source: https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Web/README.md Install the Mscc.GenerativeAI.Web NuGet package using the Package Manager Console in Visual Studio. ```powershell PM> Install-Package Mscc.GenerativeAI.Web ``` -------------------------------- ### Make first Gemini API request using C# Source: https://github.com/mscraftsman/generative-ai/blob/main/docs/index.md This example demonstrates how to initialize the Gemini client and use the GenerateContent method to send a request to the Gemini API. It assumes the GEMINI_API_KEY environment variable is set. ```csharp using Mscc.GenerativeAI; # The client gets the API key from the environment variable `GEMINI_API_KEY`. var googleAI = new GoogleAI(); // or pass in apiKey parameter var model = googleAI.GenerativeModel(model: Model.Gemini25Flash); var response = await model.GenerateContent("Explain how AI works in a few words"); Console.WriteLine(response.Text); ``` -------------------------------- ### Add Mscc.GenerativeAI.Web Package Source: https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Web/README.md Install the Mscc.GenerativeAI.Web NuGet package using the dotnet CLI. ```text > dotnet add package Mscc.GenerativeAI.Web ``` -------------------------------- ### Generate Content from Text and Image Input Source: https://github.com/mscraftsman/generative-ai/blob/main/README.md This example shows how to process an image along with a text prompt. It requires the `Mscc.GenerativeAI` namespace and a valid API key. ```csharp using Mscc.GenerativeAI; var apiKey = "your_api_key"; var prompt = "Parse the time and city from the airport board shown in this image into a list, in Markdown"; var googleAI = new GoogleAI(apiKey: apiKey); var model = googleAI.GenerativeModel(model: Model.Gemini25Flash); var request = new GenerateContentRequest(prompt); await request.AddMedia("https://raw.githubusercontent.com/mscraftsman/generative-ai/refs/heads/main/tests/Mscc.GenerativeAI/payload/timetable.png"); var response = await model.GenerateContent(request); Console.WriteLine(response.Text); ``` -------------------------------- ### Configure Generative AI with Configuration Section Path Source: https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Web/README.md This approach simplifies configuration by directly providing the name of the configuration section as a string. It's useful for straightforward setups where the section name is known. ```csharp var builder = WebApplication.CreateBuilder(args); builder.Services.AddGenerativeAI("Gemini"); var app = builder.Build(); // ... ``` -------------------------------- ### Upload and Use Large Files for Content Generation Source: https://github.com/mscraftsman/generative-ai/blob/main/README.md This example shows how to upload large media files using the File API and then reference them in a prompt for content generation. Ensure the `Mscc.GenerativeAI` namespace and necessary file paths are correctly set. ```csharp using Mscc.GenerativeAI; var apiKey = "your_api_key"; var prompt = "Make a short story from the media resources. The media resources are:"; IGenerativeAI genAi = new GoogleAI(apiKey); var model = genAi.GenerativeModel(Model.Gemini25Flash); // Upload your large image(s). // Instead of discarding you could also use the response and access `response.Text`. var filePath = Path.Combine(Environment.CurrentDirectory, "verylarge.png"); var displayName = "My very large image"; _ = await model.UploadMedia(filePath, displayName); // Create the prompt with references to File API resources. var request = new GenerateContentRequest(prompt); var files = await model.ListFiles(); foreach (var file in files.Where(x => x.MimeType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))) { Console.WriteLine($"File: {file.Name}"); request.AddMedia(file); } var response = await model.GenerateContent(request); Console.WriteLine(response.Text); ``` -------------------------------- ### Navigate to Sample Directory Source: https://github.com/mscraftsman/generative-ai/blob/main/samples/Console.Gemini.FunctionCalling/README.md Change your current directory to the sample's location within the project structure. ```bash cd samples/Console.Gemini.FunctionCalling ``` -------------------------------- ### Pack the main SDK for .NET Source: https://github.com/mscraftsman/generative-ai/blob/main/AGENTS.md Create NuGet packages for the main SDK targeting .NET. The output packages will be placed in the 'output/' directory. ```bash dotnet pack -c Release ./src/Mscc.GenerativeAI/Mscc.GenerativeAI.csproj -o output/ ``` -------------------------------- ### Install Mscc.GenerativeAI via .NET CLI Source: https://github.com/mscraftsman/generative-ai/blob/main/README.md Use this command to add the Mscc.GenerativeAI package to your .NET project from the command line. ```text > dotnet add package Mscc.GenerativeAI ``` -------------------------------- ### Create new ASP.NET Core Web Project Source: https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Web/README.md Use the dotnet CLI to create a new minimal API project. ```text > dotnet new web -o Web.Minimal.Api ``` -------------------------------- ### Build the entire solution Source: https://github.com/mscraftsman/generative-ai/blob/main/AGENTS.md Use this command to build the complete .NET solution. It compiles all projects within the GenerativeAI.sln file in Release configuration. ```bash dotnet build ./GenerativeAI.sln -c Release ``` -------------------------------- ### Add Mscc.GenerativeAI package using dotnet CLI Source: https://github.com/mscraftsman/generative-ai/blob/main/docs/index.md Install the Mscc.GenerativeAI NuGet package using the dotnet command-line tool in your .NET project folder. ```text dotnet add package Mscc.GenerativeAI ``` -------------------------------- ### Configure Generative AI with Action Source: https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Web/README.md Configure the service by providing an Action delegate that accepts a TOptions object. This allows for programmatic setting of options, including sensitive values like API keys, directly within your code. ```csharp var builder = WebApplication.CreateBuilder(args); builder.Services.AddGenerativeAI(options => { // User defined option values options.ProjectId = string.Empty; options.Model = GenerativeAI.Types.Model.GeminiProVision; options.Credentials.ApiKey = "YOUR_API_KEY"; }); var app = builder.Build(); // ... ``` -------------------------------- ### Run all tests using Makefile Source: https://github.com/mscraftsman/generative-ai/blob/main/AGENTS.md Conveniently run all test projects within the solution using the provided Makefile. This is the recommended approach for comprehensive testing. ```bash make test ``` -------------------------------- ### Add Mscc.GenerativeAI package using Package Manager Console Source: https://github.com/mscraftsman/generative-ai/blob/main/docs/index.md Install the Mscc.GenerativeAI NuGet package using the Package Manager Console in Visual Studio. ```powershell Install-Package Mscc.GenerativeAI ``` -------------------------------- ### Pack the Google Cloud Client Library SDK Source: https://github.com/mscraftsman/generative-ai/blob/main/AGENTS.md Create NuGet packages for the SDK version that utilizes the Google Cloud Client Library. The generated packages will be saved in the 'output/' directory. ```bash dotnet pack -c Release ./src/Mscc.GenerativeAI.Google/Mscc.GenerativeAI.Google.csproj -o output/ ``` -------------------------------- ### Generate Content with Multimodal Input (Image) Source: https://github.com/mscraftsman/generative-ai/blob/main/docs/docs/text-generation.md Combine text prompts with media files, such as images, for richer context. This example parses information from an airport board image. ```csharp using Mscc.GenerativeAI; var prompt = "Parse the time and city from the airport board shown in this image into a list, in Markdown"; var googleAI = new GoogleAI(); var model = googleAI.GenerativeModel(model: Model.Gemini25Flash); var request = new GenerateContentRequest(prompt); await request.AddMedia("https://raw.githubusercontent.com/mscraftsman/generative-ai/refs/heads/main/tests/Mscc.GenerativeAI/payload/timetable.png"); var response = await model.GenerateContent(request); Console.WriteLine(response.Text); ``` -------------------------------- ### Streaming Content Generation with Tools Source: https://github.com/mscraftsman/generative-ai/blob/main/src/Mscc.GenerativeAI.Microsoft/README.md Configure the IChatClient for streaming responses and enable function invocation and OpenTelemetry. This example demonstrates using a custom tool to retrieve age information. ```csharp using Microsoft.Extensions.AI; using Mscc.GenerativeAI.Microsoft; using System.ComponentModel // assuming credentials are set up in environment variables as instructed above. IChatClient chatClient = new GeminiChatClient() .AsIChatClient("gemini-2.5-flash") .AsBuilder() .UseFunctionInvocation() .UseOpenTelemetry() .Build(); ChatOptions options = new() { Tools = [AIFunctionFactory.Create(([Description("The name of the person whose age is to be retrieved")] string personName) => personName switch { "Alice" => 30, "Bob" => 25, _ => 35 }, "get_person_age", "Gets the age of the specified person"); }; await foreach (var update in chatClient.GetStreamingResponseAsync("How much older is Alice than Bob?", options)) { Console.Write(update); } ``` -------------------------------- ### Initiate and Manage Chat Conversations Source: https://github.com/mscraftsman/generative-ai/blob/main/README.md This snippet demonstrates how to start a multi-turn chat conversation. It uses the default Gemini Pro model and allows for managing chat history, including rewinding. ```csharp using Mscc.GenerativeAI; var apiKey = "your_api_key"; var googleAI = new GoogleAI(apiKey); var model = googleAI.GenerativeModel(); // using default model: gemini-2.5-pro var chat = model.StartChat(); // optionally pass a previous history in the constructor. // Instead of discarding you could also use the response and access `response.Text`. _ = await chat.SendMessage("Hello, fancy brainstorming about IT?"); _ = await chat.SendMessage("In one sentence, explain how a computer works to a young child."); _ = await chat.SendMessage("Okay, how about a more detailed explanation to a high schooler?"); _ = await chat.SendMessage("Lastly, give a thorough definition for a CS graduate."); // A chat session keeps every response in its history. chat.History.ForEach(c => Console.WriteLine($"{c.Role}: {c.Text}")); // Last request/response pair can be removed from the history. var latest = chat.Rewind(); Console.WriteLine($"{latest.Sent} - {latest.Received}"); ```