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
<PackageReference Include="Google_GenerativeAI.Microsoft" Version="2.0.14" />
paket add Google_GenerativeAI.Microsoft --version 2.0.14
#r "nuget: Google_GenerativeAI.Microsoft, 2.0.14"
// 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
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 implementsIChatClient
.- Easy Integration: Works with .NET's dependency injection.
- Streaming/Non-Streaming: Supports
CompleteAsync
andCompleteStreamingAsync
. - 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
- Google_GenerativeAI (Unofficial C# Google Generative AI SDK)
- Microsoft.Extensions.AI
Contributing
Contributions are welcome! Submit pull requests or open issues.
License
MIT License - see the LICENSE file.
Product | Versions 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. |
-
.NETFramework 4.6.2
- Google_GenerativeAI (>= 2.0.14)
- Microsoft.Extensions.AI (>= 9.3.0-preview.1.25114.11)
-
.NETStandard 2.0
- Google_GenerativeAI (>= 2.0.14)
- Microsoft.Extensions.AI (>= 9.3.0-preview.1.25114.11)
-
net8.0
- Google_GenerativeAI (>= 2.0.14)
- Microsoft.Extensions.AI (>= 9.3.0-preview.1.25114.11)
-
net9.0
- Google_GenerativeAI (>= 2.0.14)
- Microsoft.Extensions.AI (>= 9.3.0-preview.1.25114.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.