DEV Community

Guilherme Martin
Guilherme Martin

Posted on

Integrating with Google Gemini: Using a Built .NET SDK

🚀 Introduction

Google Gemini, renowned for its AI prowess, stands as a trailblazer in the realm of artificial intelligence. However, the absence of a dedicated .NET SDK presents a challenge for .NET developers. In this article, let's delve into bridging this gap with a custom .NET SDK tailored explicitly for Google Gemini.

🌟 Motivation

In the landscape of .NET development, the lack of an official SDK for Google Gemini can pose hurdles when interacting with the API. To overcome this, I embarked on creating a bespoke SDK, aiming to cater to the unique needs and functionalities of Google Gemini.

The drive behind this initiative stems from a desire to enrich the development experience for .NET developers engaging with Google Gemini. By encapsulating the intricacies of the Gemini API and furnishing a more idiomatic .NET interface, this SDK seeks to simplify the process of leveraging machine learning capabilities for content generation.

📦 How to Download?

Get started by installing the DotnetGeminiSDK NuGet package. Run the following command in the NuGet Package Manager Console:

Install-Package DotnetGeminiSDK

Or, if you prefer using the .NET CLI:

dotnet add package DotnetGeminiSDK

🔧 How to use?

To use the Gemini SDK, add the Gemini client to your service collection using GeminiServiceExtensions:

using DotnetGeminiSDK;
using Microsoft.Extensions.DependencyInjection;

public class Startup
    public void ConfigureServices(IServiceCollection services)
        services.AddGeminiClient(config =>
            config.ApiKey = "YOUR_GOOGLE_GEMINI_API_KEY";
            config.ImageBaseUrl = "CURRENTLY_IMAGE_BASE_URL";
            config.TextBaseUrl = "CURRENTLY_IMAGE_BASE_URL";
Enter fullscreen mode Exit fullscreen mode

When you incorporate the Gemini client, you can seamlessly inject it into your code for immediate use.

using DotnetGeminiSDK.Client.Interfaces;
using Microsoft.Extensions.DependencyInjection;

public class YourClass
    private readonly IGeminiClient _geminiClient;

    public YourClass(IGeminiClient geminiClient)
        _geminiClient = geminiClient;

    public async Task Example()
        var response = await _geminiClient.TextPrompt("Text for processing");
        // Process the response as needed
Enter fullscreen mode Exit fullscreen mode

🗝️ Existing Features

To demonstrate the use of the SDK, i will implement a simple API with two endpoints to showcase its versatility.

For using the SDK i've created a test api and put the GeminiClient in the constructor to receive it by dependency injection, you don't need to inject it again, when you call services.AddGeminiClient() it already inject it for you.

Gemini Dependency Injection

📝 Text Prompt

For the text prompt, we have two options: sending a single message or multiple messages in batch. Additionally, we can define options for GenerationConfig and SafetySettings.

In this simple example, we will build a simple api where we receive the text input and use the TextPrompt method to call the Gemini.

Gemini Text

As you see it's very simple, but you can customize it sending the optional parameters to method like GenerationConfig and SafetySetting.

Now let's test

Prompt Request


As you see, we have the full Gemini response, so you can use it as needed.

🏞️ Image Prompt

In this method we need to send three parameters, the text to be considered, the image in base64 or byte[] and the MimeType of the image using the SDK Enum: ImageMimeType, remembering that the formats accepted by Google are:

Enter fullscreen mode Exit fullscreen mode

To verify the capability of the image description feature, we've implemented a dedicated endpoint in our API. This endpoint allows you to submit an image along with a descriptive text, prompting Google Gemini to analyze the image and provide a description.


As the image above illustrates, I need to send the image in base64 format, its mimetype, and the message to Gemini. Therefore, let's choose a Jpeg image for this test.

Let's use this cute Shiba Inu photo to check if it works

Shiba Inu

Test Results

As we can see in bellow image, we have requested to Gemini to tell which kind of dog we have in the image, and he answered Shiba Inu.

It works!!


✅ Conclusion

In this article, I demonstrated how to use Gemini in a simple and quick manner. It's important to note that the SDK is experimental and may have issues or lack some features, but it is entirely usable. Feel free to explore, experiment, and leverage the capabilities provided by this SDK, adapting it as needed to meet your specific requirements.

Thanks for reading!

GitHub SDK Link:

DotnetGeminiSDK Repository

Top comments (0)