GeminiSharp 0.0.1.4
See the version list below for details.
dotnet add package GeminiSharp --version 0.0.1.4
NuGet\Install-Package GeminiSharp -Version 0.0.1.4
<PackageReference Include="GeminiSharp" Version="0.0.1.4" />
<PackageVersion Include="GeminiSharp" Version="0.0.1.4" />
<PackageReference Include="GeminiSharp" />
paket add GeminiSharp --version 0.0.1.4
#r "nuget: GeminiSharp, 0.0.1.4"
#:package GeminiSharp@0.0.1.4
#addin nuget:?package=GeminiSharp&version=0.0.1.4
#tool nuget:?package=GeminiSharp&version=0.0.1.4
GeminiSharp
GeminiSharp is a C# client SDK for interacting with Google's Gemini API, enabling seamless integration of Gemini's powerful text generation capabilities into your .NET applications. It provides a simple, flexible, and robust interface for generating content using Gemini models.
Features
- Easy-to-use C# Client: Provides a straightforward API for interacting with Google's Gemini API from your .NET applications.
- Text Generation & Structured Output: Supports both free-form text generation and structured output using JSON schema.
- API Key Authentication: Securely authenticate with the Gemini API using your API key.
- Configurable Model Selection: Easily specify the Gemini model to use for content generation.
- Configurable Base URL: Customize the base URL for future flexibility and alternative endpoint support.
- Error Handling: Robustly handles API errors and exceptions, providing informative error messages.
- NuGet Package Support: Simple installation via NuGet package manager.
Current Status
GeminiSharp initially focused only on text generation. I have now added support for structured output generation using JSON schemas. Future development will include:
- Vision support 📷
- Audio understanding 🎧
- Code execution 💻
- Document processing 📄
Stay tuned for updates! 🚀
Installation
You can install GeminiSharp via NuGet:
dotnet add package GeminiSharp
📚 Supported .NET Versions
.NET Version | Supported |
---|---|
.NET 6 | ✅ Yes |
.NET 7 | ✅ Yes |
.NET 8 | ✅ Yes |
Usage
Basic Example
using System;
using System.Net.Http;
using System.Threading.Tasks;
using GeminiSharp.Client;
class Program
{
static async Task Main()
{
using var httpClient = new HttpClient();
var apiKey = "your-gemini-api-key"; // Replace with your actual API key
var geminiClient = new GeminiClient(httpClient, apiKey);
try
{
string model= "gemini-2.0-flash";
var response = await geminiClient.GenerateContentAsync(model, "Hello, Gemini! What is Falcon 9?");
Console.WriteLine(response?.Candidates?[0].Content);
}
catch (GeminiApiException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Structured Response Example
Structured output generation is a new feature in GeminiSharp. For detailed documentation, see Structured Output Documentation.
using GeminiSharp.Client;
using GeminiSharp.Helpers;
using System.Text.Json;
var schema = JsonSchemaHelper.GenerateSchema<PlayerStats>();
var response = await geminiClient.GenerateStructuredContentAsync<PlayerStats>(
"gemini-2.0", "Provide cricket player stats for Virat Kohli", schema);
Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions { WriteIndented = true }));
API Error Handling
The SDK throws GeminiApiException for API errors. You can catch and inspect the error details:
try
{
using var httpClient = new HttpClient();
var apiKey = "your-gemini-api-key"; // Replace with your actual API key
var geminiClient = new GeminiClient(httpClient, apiKey);
var response = await geminiClient.GenerateContentAsync("invalid-model", "Test");
}
catch (GeminiApiException ex)
{
Console.WriteLine($"API Error: {ex.Message}");
Console.WriteLine($"Status Code: {ex.StatusCode}");
}
ASP.NET Core API Example
using GeminiSharp.Client;
using GeminiSharp.API;
using Microsoft.AspNetCore.Mvc;
namespace geminisdktest.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class GeminiSDKController : ControllerBase
{
public class GenerateTextRequest
{
public string Prompt { get; set; } = string.Empty;
}
[HttpPost("generate")]
public async Task<IActionResult> GenerateText(
[FromBody] GenerateTextRequest request,
[FromHeader(Name = "GeminiApiKey")] string apiKey,
[FromHeader(Name = "Gemini-Model")] string? model)
{
if (string.IsNullOrWhiteSpace(request.Prompt))
return BadRequest(new { error = "Prompt cannot be empty." });
if (string.IsNullOrWhiteSpace(apiKey))
return BadRequest(new { error = "API key is required." });
model ??= "gemini-1.5-flash"; // Default model
using var httpClient = new HttpClient();
var geminiClient = new GeminiClient(httpClient, apiKey);
try
{
var result = await geminiClient.GenerateContentAsync(model, request.Prompt);
return Ok(new { Response = result });
}
catch (GeminiApiException ex)
{
return StatusCode((int)ex.StatusCode, ex.ErrorResponse);
}
catch (Exception ex)
{
return StatusCode(500, new { error = "Internal Server Error", details = ex.Message });
}
}
}
}
Configuring the Base URL
You can configure the base URL for the Gemini API when creating the GeminiClient:
using System.Net.Http;
using GeminiSharp.Client;
// Use a custom base URL. Useful for testing or staging environments.
var customBaseUrl = "https://your-custom-gemini-api.com";
using var httpClient = new HttpClient();
var apiKey = "your-gemini-api-key"; // Replace with your actual API key
var geminiClient = new GeminiClient(httpClient, apiKey, customBaseUrl);
Contributing
We welcome contributions! To get started, follow these steps:
- Fork the repository.
- Create a new branch (
feature-branch-name
). - Make your changes and commit with a clear message.
- Push your branch to your fork.
- Open a Pull Request (PR) and describe your changes.
Feel free to discuss ideas or report issues in the issues section.
Thank you for contributing! 🚀
License
This project is licensed under the MIT License.
Author
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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 was computed. 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. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.