Smartstore.AI.GeminiClient 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Smartstore.AI.GeminiClient --version 1.0.2
                    
NuGet\Install-Package Smartstore.AI.GeminiClient -Version 1.0.2
                    
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="Smartstore.AI.GeminiClient" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Smartstore.AI.GeminiClient" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Smartstore.AI.GeminiClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Smartstore.AI.GeminiClient --version 1.0.2
                    
#r "nuget: Smartstore.AI.GeminiClient, 1.0.2"
                    
#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.
#:package Smartstore.AI.GeminiClient@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Smartstore.AI.GeminiClient&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Smartstore.AI.GeminiClient&version=1.0.2
                    
Install as a Cake Tool

AI Gemini client

NuGet Version NuGet Downloads

This project provides a .NET client library designed to facilitate interaction with the Gemini API. It offers developers a streamlined approach to integrating Gemini's powerful AI capabilities into their .NET applications. Key features include:

  • Simplified API calls: Abstracts the complexities of direct API interaction.
  • Data handling: Efficiently manages request and response data.
  • Flexibility: Adaptable to various .NET environments.

Implemented features

  • Generate text or image content (including image input).
  • Generate content as stream.
  • Generate images using Imagen.
  • File operations: Upload, get file list, delete file.
  • Get list of available Gemini models.

Quickstart

Get a Google Gemini API key. To install the Smartstore.AI.GeminiClient NuGet package, run the following command in the NuGet Package Manager Console:

Install-Package Smartstore.AI.GeminiClient

Or, if you prefer using the .NET CLI:

dotnet add package Smartstore.AI.GeminiClient

Usage

You can seamlessly integrate the GeminiAIClient into your code using dependency injection.


// "services" is of type IServiceCollection.
services.AddHttpClient<GeminiAIClient>()
	.ConfigureHttpClient(client =>
	{
		client.Timeout = TimeSpan.FromSeconds(30);
	});

Alternatively, you can instantiate the GeminiAIClient class manually. To do this, simply pass an HttpClient instance to the constructor.

using System.Net.Http;
using Smartstore.AI.GeminiClient;

public class MyHttpClass
{
	private readonly IHttpClientFactory _httpClientFactory;

	public MyHttpClass(IHttpClientFactory httpClientFactory)
	{
		_httpClientFactory = httpClientFactory;
	}

	public void MyHttpMethod()
	{
		var client = new GeminiAIClient(_httpClientFactory.CreateClient());
		//...
	}
}

Configuration

The GeminiConfig class is used to provide GeminiAIClient with the data required for communication with Gemini:

var config = new GeminiConfig(
	"Your Google Gemini API key", 
	"AI model name", 
	"Gemini API base URL (optional)");

A method call of the client is always made by passing an instance of GeminiConfig.

Examples

Generate text

public class GeminiProvider(GeminiAIClient client)
{
	private readonly GeminiAIClient _client = client;
	
	public async Task<string> ChatAsync(
		string message /*e.g. How does AI work?*/,
		CancellationToken cancelToken = default)
	{
		var config = new GeminiConfig("my-api-key...", "gemini-2.0-flash");
		var contents = new List<GeminiContent>
		{
			new GeminiContent
			{
				Role = "user",
				Parts = [new GeminiPart
				{
					Text = message
				}]
			}
		};

		// Give the AI model additional context to help it understand the task
		// or follow specific guidelines (optional).
		var instructions = new List<GeminiPart>
		{
			new GeminiPart
			{
				Text = "Please answer in a friendly tone."
			}
		};

		// Control how the AI model generates responses (optional).
		var generationConfig = new GeminiGenerationConfig
		{
			Temperature = 0.8f,
			MaxOutputTokens = 500
		};

		var request = new GeminiGenerateContentRequest
		{
			Contents = contents,
			SystemInstruction = new GeminiContent { Parts = instructions },
			GenerationConfig = generationConfig
		};

		var response = await _client.GenerateContentAsync(config, request, cancelToken);
		var candidate = response.Candidates.FirstOrDefault();
		
		return candidate?.Content?.Parts?.FirstOrDefault()?.Text;	
	}
}

Streaming output

public class GeminiProvider(GeminiAIClient client)
{
	private readonly GeminiAIClient _client = client;
	
	public async IAsyncEnumerable<string> ChatAsStreamAsync(
		string message /*e.g. How does AI work?*/,
		[EnumeratorCancellation] CancellationToken cancelToken = default)
	{
		var config = new GeminiConfig("my-api-key...", "gemini-2.0-flash");
		var contents = new List<GeminiContent>
		{
			new GeminiContent
			{
				Role = "user",
				Parts = [new GeminiPart
				{
					Text = message
				}]
			}
		};

		// Give the AI model additional context to help it understand the task
		// or follow specific guidelines (optional).
		var instructions = new List<GeminiPart>
		{
			new GeminiPart
			{
				Text = "Please answer in a friendly tone."
			}
		};

		var request = new GeminiGenerateContentRequest
		{
			Contents = contents,
			SystemInstruction = new GeminiContent { Parts = instructions }
		};

		await foreach (var response in _client.GenerateContentAsStreamAsync(config, request, cancelToken))
		{
			var candidate = response.Candidates.FirstOrDefault();
			var answer = candidate?.Content?.Parts?.FirstOrDefault()?.Text;
			if (answer != null)
			{
				yield return answer;
			}
		}
	}
}
Product Compatible and additional computed target framework versions.
.NET 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
1.0.5 705 12/2/2025
1.0.4 581 12/1/2025
1.0.3 157 11/28/2025
1.0.2 220 11/3/2025
1.0.1 207 10/21/2025
1.0.0 270 4/2/2025