Smartstore.AI.GeminiClient
1.0.2
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
<PackageReference Include="Smartstore.AI.GeminiClient" Version="1.0.2" />
<PackageVersion Include="Smartstore.AI.GeminiClient" Version="1.0.2" />
<PackageReference Include="Smartstore.AI.GeminiClient" />
paket add Smartstore.AI.GeminiClient --version 1.0.2
#r "nuget: Smartstore.AI.GeminiClient, 1.0.2"
#:package Smartstore.AI.GeminiClient@1.0.2
#addin nuget:?package=Smartstore.AI.GeminiClient&version=1.0.2
#tool nuget:?package=Smartstore.AI.GeminiClient&version=1.0.2
AI Gemini client
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 | Versions 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. |
-
net8.0
-
net9.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.