Google_GenerativeAI.Microsoft 2.0.14

dotnet add package Google_GenerativeAI.Microsoft --version 2.0.14                
NuGet\Install-Package Google_GenerativeAI.Microsoft -Version 2.0.14                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Google_GenerativeAI.Microsoft" Version="2.0.14" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Google_GenerativeAI.Microsoft --version 2.0.14                
#r "nuget: Google_GenerativeAI.Microsoft, 2.0.14"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Google_GenerativeAI.Microsoft as a Cake Addin
#addin nuget:?package=Google_GenerativeAI.Microsoft&version=2.0.14

// Install Google_GenerativeAI.Microsoft as a Cake Tool
#tool nuget:?package=Google_GenerativeAI.Microsoft&version=2.0.14                

Google_GenerativeAI.Microsoft

NuGet License: MIT

Google_GenerativeAI.Microsoft provides an IChatClient implementation (from Microsoft.Extensions.AI) for the Google Generative AI SDK (C# Google_GenerativeAI). It lets you use Google's Gemini models in apps built with Microsoft's AI abstractions.

Key Features

  • IChatClient Implementation: GenerativeAIChatClient class implements IChatClient.
  • Easy Integration: Works with .NET's dependency injection.
  • Streaming/Non-Streaming: Supports CompleteAsync and CompleteStreamingAsync.
  • Prompt History: Handles chat history within the IChatClient.
  • Configurable: Configure the underlying GenerativeModel.

Getting Started

1. Installation

dotnet add package Google_GenerativeAI.Microsoft

2. Register Services

Add GenerativeAIChatClient to your service collection (e.g., in Startup.cs or Program.cs). Store your API key securely (e.g., environment variables, user secrets). Do not hardcode it.

using Google_GenerativeAI.Microsoft;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;

// ...

public void ConfigureServices(IServiceCollection services)
{
    // Get API key from environment variables (recommended).
    string apiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY");

    // Basic configuration:
    services.AddScoped<IChatClient>(provider => new GenerativeAIChatClient(apiKey));    
}

3. Using the IChatClient

Inject IChatClient and use GenerateMessageAsync (non-streaming) or StreamGenerateMessageAsync (streaming).

using Microsoft.Extensions.AI;
using System.Threading.Tasks;
using System.Threading;

public class MyChatService
{
    private readonly IChatClient _chatClient;

    public MyChatService(IChatClient chatClient)
    {
        _chatClient = chatClient;
    }

    public async Task<string> GetChatResponse(string userMessage)
    {
        // IChatClient maintains history internally. Add messages for multi-turn context.
        var chatHistory = new List<ChatMessage>
        {
            new ChatMessage(AuthorRole.User, userMessage)
        };
        return (await _chatClient.GenerateMessageAsync(chatHistory)).Content;
    }

    public async Task StreamChatResponseToConsole(string userMessage, CancellationToken cancellationToken = default)
    {
         var chatHistory = new List<ChatMessage>
         {
             new ChatMessage(AuthorRole.User, userMessage)
         };

         await foreach (var chunk in _chatClient.StreamGenerateMessageAsync(chatHistory, cancellationToken: cancellationToken))
         {
             Console.Write(chunk.Content);
         }
         Console.WriteLine();
    }
}

Dependencies

Contributing

Contributions are welcome! Submit pull requests or open issues.

License

MIT License - see the LICENSE file.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.14 11 2/19/2025
2.0.11 40 2/18/2025
2.0.7 40 2/17/2025
2.0.6 36 2/17/2025
2.0.4 45 2/16/2025
2.0.2 41 2/16/2025
2.0.0 34 2/16/2025