Web Application Integration - gunpal5/Google_GenerativeAI GitHub Wiki

Introduction

This page provides instructions on integrating the Google Generative AI SDK into your .NET web applications using the Google_GenerativeAI.Web NuGet package. This package provides extension methods for the IServiceCollection interface, simplifying the setup and configuration of the Google Generative AI services within your application.


Installation

To begin, install the Google_GenerativeAI.Web NuGet package in your project:

Install-Package Google_GenerativeAI.Web

You can find the NuGet package here: Google_GenerativeAI.Web


Setup and Configuration

The Google_GenerativeAI.Web package offers several extension methods for IServiceCollection to configure the SDK:

AddGenerativeAI()

This method provides various overloads to register the Google Generative AI services with your dependency injection container.

1. Basic Configuration:

This overload uses environment variables and default values for configuration.

using GenerativeAI.Web;

public void ConfigureServices(IServiceCollection services)
{
    services.AddGenerativeAI();
    //... other services
}

2. Configuration with Options:

This overload allows you to pass a GenerativeAIOptions object to configure the services.

using GenerativeAI.Web;

public void ConfigureServices(IServiceCollection services)
{
    GenerativeAIOptions options = new GenerativeAIOptions()
    {
        ProjectId = "your-project-id",
        Region = "us-central1",
        Model = GoogleAIModels.DefaultGeminiModel, // Example: Explicitly set the model
        IsVertex = false, //Example: Set IsVertex flag
        ExpressMode = false // Example : Set express mode
    };

    services.AddGenerativeAI(options);
    //... other services
}

3. Configuration with IConfiguration Section:

This overload allows you to configure the services using a named section from your appsettings.json file. This is a recommended approach for managing configuration.

using GenerativeAI.Web;

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
    services.AddGenerativeAI(configuration.GetSection("GenerativeAI"));
    //... other services
}

Example appsettings.json:

{
  "GenerativeAI": {
    "ProjectId": "your-project-id",
    "Region": "us-central1",
    "Model": "gemini-pro", // Example: Set the model in configuration
    "IsVertex": false,
    "ExpressMode": false,
    "Credentials": {
        "ApiKey": "YOUR_API_KEY" // Or other credential configuration as needed
    }
  },
  // ... other configuration
}

GenerativeAIOptions Class:

public class GenerativeAIOptions : IGenerativeAIOptions
{
    public string? ProjectId { get; set; }
    public string? Region { get; set; }
    public IGoogleAuthenticator? Authenticator { get; set; }
    public GoogleAICredentials? Credentials { get; set; }
    public bool? IsVertex { get; set; }
    public string? Model { get; set; }
    public bool? ExpressMode { get; set; }
    public string? ApiVersion { get; set; }
}

4. Configuration with Action:

This overload allows you to configure the GenerativeAIOptions using an action delegate.

using GenerativeAI.Web;

public void ConfigureServices(IServiceCollection services)
{
    services.AddGenerativeAI(options =>
    {
        options.ProjectId = "your-project-id";
        options.Region = "us-central1";
        options.Model = "gemini-pro";  // Example: Setting model via action
        //... other options
    });
    //... other services
}

Authentication

The Google_GenerativeAI.Web package also provides extension methods to configure authentication for the Generative AI services:

  • WithAdc: Configures the Google Application Default Credentials (ADC) for authentication.
  • WithServiceAccount: Configures authentication using a Google Service Account.
  • WithOAuth: Configures authentication using Google OAuth.
using GenerativeAI.Web;

public void ConfigureServices(IServiceCollection services)
{
    services.AddGenerativeAI()
          .WithServiceAccount("path/to/your/service_account.json");
    //... other services
}

Usage

Once configured, you can inject the IGenerativeAiService into your controllers or other services to access the Generative AI functionalities.

using GenerativeAI.Core;
using Microsoft.AspNetCore.Mvc;

public class MyController : Controller
{
    private readonly IGenerativeAiService _generativeAIService;

    public MyController(IGenerativeAiService generativeAIService)
    {
        _generativeAIService = generativeAIService;
    }

    public async Task<IActionResult> GenerateText(string prompt)
    {
        var model = _generativeAIService.CreateInstance('gemini-1.5-flash');
        var response = await model .GenerateContentAsync(prompt);
        //... process the response
    }
}

Important Considerations

  1. Ensure that the necessary environment variables or configuration values are set correctly.
  2. Choose the appropriate authentication method based on your application's requirements.
  3. Refer to the Authentication page for more details on authentication options.

API Reference