Google Gen AI .NET SDK

repository·main·Indexed 40 days ago

https://github.com/googleapis/dotnet-genai

A programmatic interface for integrating Google's generative models into .NET applications. The SDK supports both the Gemini Developer API and the Gemini Enterprise Agent Platform, providing capabilities for text generation, streaming, and real-time audio-to-audio conversations via the Live API. It is distributed as the Google.GenAI NuGet package.

Tokens
12.4K
Snippets
47
Records
53
Agent score
99%

What's inside Google Gen AI .NET SDK

  1. Perform model tuning with client.Tunings

    main

    The client.Tunings module allows you to fine-tune models. This is primarily available for the Gemini Enterprise Agent Platform (enterprise: true).

    Supported tuning methods include:

    • Standard Tuning: Training on a dataset using TuneAsync.
    • Preference Tuning: Set Method = TuningMethod.PREFERENCE_TUNING in CreateTuningJobConfig.
    • Distillation: Set Method = TuningMethod.DISTILLATION and provide a BaseTeacherModel to train a student model.
    • Continuous Tuning: Pass the resource name of a previously tuned model as the baseModel parameter to continue training.
    // Example: Preference Tuning
    var config = new CreateTuningJobConfig {
        TunedModelDisplayName = "Tuned Model",
        Method = TuningMethod.PREFERENCE_TUNING,
        EpochCount = 1,
    };
    var tuningJob = await client.Tunings.TuneAsync(
        baseModel: "gemini-flash-latest",
        trainingDataset: trainingDataset,
        config: config
    );
  2. Understand Enums and Types in the SDK

    main

    Parameter types are located in the Google.GenAI.Types namespace.

    Enums:

    • Enums are implemented as structs for forward compatibility.
    • Values follow C# PascalCase conventions.
    • The underlying string value sent to the API is typically UPPER_SNAKE_CASE.
    • If you use unknown enum values, they are preserved as strings. You can check equality against static SDK values by comparing the underlying string (e.g., PersonGeneration.AllowAll == "ALLOW_ALL" is true).
  3. Manage cached content with client.Caches

    main

    The client.Caches module allows you to manage cached content to optimize performance and cost. Caching behavior depends on the platform:

    • Gemini Enterprise Agent Platform: Use Google Cloud Storage (GCS) URIs (gs://...).
    • Gemini API: Use File URIs (https://...).

    Key operations include creating, retrieving, updating, deleting, and listing caches.

    // Example: Creating a cache for Gemini API using File URIs
    var geminiClient = new Client(apiKey: apiKey);
    var geminiConfig = new CreateCachedContentConfig {
        Contents = new List<Content> {
            new Content {
                Role = "user",
                Parts = new List<Part> {
                    new Part { FileData = new FileData { FileUri = "https://generativelanguage.googleapis.com/v1beta/files/file-id", MimeType = "application/pdf" } }
                }
            }
        },
        DisplayName = "my-gemini-cache",
        Ttl = "600s"
    };
    var geminiResponse = await geminiClient.Caches.CreateAsync(model: "gemini-flash-latest", config: geminiConfig);
  4. Understand Client Patterns and API Scenarios in Stress Tests

    main

    The stress tests validate the SDK across different architectural styles and API modalities to ensure resource safety.

    Client Patterns

    PatternDescriptionUse Case
    SingletonSingle shared Client instance.Recommended for production. Checks for contention/thread-safety.
    Per-RequestNew Client per request, disposed immediately.Stress Test. Checks Dispose() logic and HTTP connection pooling cleanup.
    PoolPool of reusable Client instances.Alternative architecture. Checks reuse logic.

    API Scenarios

    1. GenerateContent: Unary HTTP requests (standard request/response).
    2. GenerateContentStream: Server-sent events (streaming). Tests stream reader disposal.
    3. Live API: Bidirectional WebSocket streaming. Tests AsyncSession lifecycles and connection state.
  5. Setup for RegisterFiles sample

    main

    To use the RegisterFiles functionality, you must configure both your Gemini API key and Google Cloud Application Default Credentials (ADC) with specific scopes to allow reading from GCS.

    1. Set your Gemini API key environment variable: export GEMINI_API_KEY="your-api-key"

    2. Authenticate with Google Cloud using the following command to ensure the necessary scopes for Cloud Platform and GCS read access are included: gcloud auth application-default login --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only"

    export GEMINI_API_KEY="your-api-key"
    
    gcloud auth application-default login --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only"
  6. Run the GenerateContentStreamSimpleText demo

    main

    To run the GenerateContentStreamSimpleText demonstration, ensure you are in the directory containing this README and have configured the required environment variables for Google AI Studio and Google Cloud. Then, execute the application using the .NET CLI.

    # 1. Set environment variables
    export GEMINI_API_KEY="your_api_key"
    export GOOGLE_CLOUD_PROJECT="your_project_id"
    export GOOGLE_CLOUD_LOCATION="your_location"
    
    # 2. Run the application
    dotnet run
  7. Create a client for Gemini Developer API

    main

    To use the Gemini Developer API, instantiate a Client by providing your API key. You can also use the GEMINI_API_KEY environment variable to avoid passing it explicitly in code.

    using Google.GenAI;
    
    // Explicitly passing the API key
    var client = new Client(apiKey: apiKey);
  8. Run the DemoApp quickstart

    main

    To run the DemoApp, ensure you are in the directory containing this README, configure your Google AI Studio API key, and use the dotnet CLI to run the project.

    1. Set your Google AI Studio API key to the environment variable GEMINI_API_KEY.
    2. Execute dotnet run.
    export GEMINI_API_KEY="your_api_key_here"
    dotnet run
  9. Set up credentials and environment variables for the Gemini API

    main

    Before running the demo application, you must authenticate with Google Cloud and configure the required environment variables.

    1. Authenticate: Run gcloud auth login and follow the prompts to set up Application Default Credentials (ADC).
    2. Configure Project: Set the GOOGLE_CLOUD_PROJECT environment variable to your Google Cloud project ID.
    3. Configure Location: Set the GOOGLE_CLOUD_LOCATION environment variable to your preferred Google Cloud location (e.g., us-central1).
    4. Configure API Key: Set the GEMINI_API_KEY environment variable with your valid Gemini API key.
    gcloud auth login
    export GOOGLE_CLOUD_PROJECT="your-project-id"
    export GOOGLE_CLOUD_LOCATION="your-location"
    export GEMINI_API_KEY="your-api-key"
  10. Interpret Stress Test Results and Leak Detection

    main

    The test suite uses a Plateau Pattern hypothesis: resource usage should grow during ramp-up but plateau once max concurrency is reached. Any linear growth trend after the plateau indicates a leak.

    Results are reported via Trend Analysis (slope + R²).

    • Healthy Result: Low $R^2$ (indicates noise/random fluctuation) or zero slope.
      • Example: Memory: slope=0.15 bytes/req, R2=0.045
    • Leak Detected: High slope combined with a high $R^2$ (indicates a consistent upward trajectory).
      • Example: Memory: slope=150.5 bytes/req, R2=0.95

    For definitive analysis, inspect the HTML Report Scatter Plots generated in the Reports/ directory. Look for a clear, consistent upward trajectory in the scatter plot.

  11. Run the Live Audio-to-Audio Realtime Demo

    main

    This demo implements a real-time, voice-to-voice conversation using Gemini's live API. The architecture uses a frontend web page (HTML/JS) to capture microphone audio, an ASP.NET Core server as a WebSocket relay, and the remote Gemini Live API for processing.

    To run the demo, follow these steps:

    1. Authentication: Run gcloud auth login and follow the prompts to set up Application Default Credentials (ADC).
    2. Environment Configuration: Set the following environment variables:
      • GOOGLE_CLOUD_PROJECT: Your Google Cloud project ID.
      • GOOGLE_CLOUD_LOCATION: Your Google Cloud location.
      • GEMINI_API_KEY: Your Gemini API key.
    3. Execution:
      • To test the Gemini API endpoint, run: dotnet run
      • To test the Vertex endpoint, run: dotnet run --vertex true
    4. Interaction: Open the URL provided in the terminal (typically http://localhost:5000) and interact with the browser by speaking.
    # Setup credentials
    gcloud auth login
    
    # Set environment variables
    export GOOGLE_CLOUD_PROJECT="your-project-id"
    export GOOGLE_CLOUD_LOCATION="your-location"
    export GEMINI_API_KEY="your-api-key"
    
    # Run Gemini API endpoint
    dotnet run
    
    # Run Vertex endpoint
    dotnet run --vertex true