Giter VIP home page Giter VIP logo

dotnet-gemini-sdk's Introduction

DotnetGeminiSDK

NuGet Version NuGet Downloads

Welcome to DotnetGeminiSDK, a .NET SDK for interacting with the Google Gemini API. This SDK empowers developers to harness the capabilities of machine learning models to generate creative content effortlessly.

Table of Contents

What is Google Gemini?

Google Gemini is an advanced AI platform that offers various interfaces for commands tailored to different use cases. It allows users to interact with machine learning models for generating content and responses to instructions. The platform supports free-form commands, structured commands, and chat-based requests. Additionally, Gemini provides the ability to adjust models for specific tasks, enhancing their performance for particular use cases.

Installation ๐Ÿ“ฆ

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

Configuration โš™๏ธ

To use the Gemini SDK, configure the GoogleGeminiConfig object. Add the Gemini client to your service collection using GeminiServiceExtensions:

Note

Only used when using the dependency injection method.

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";
        });
    }
}

How to use? ๐Ÿ”Ž

Dependency Injection

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
    }
}

Class Instantiation

If you don't want to use dependency injection, you can instantiate the GeminiClient class, as a constructor parameter, place your settings in the GoogleGeminiConfig instance.

using DotnetGeminiSDK.Client.Interfaces;

public class YourClass
{
    private readonly GeminiClient _geminiClient;

    public YourClass()
    {
        _geminiClient = new GeminiClient(new GoogleGeminiConfig(){ //Place your settings here });
    }

    public async Task Example()
    {
        var response = await _geminiClient.TextPrompt("Text for processing");
        // Process the response as needed
    }
}

Implemented features ๐Ÿ‘พ

  • Text Prompt
  • Stream Text Prompt
  • Multiple Text Prompt
  • Image Prompt
  • Counting Tokens
  • Get Model
  • List Models
  • Embedding
  • Batch Embedding

Usage ๐Ÿš€

Text Prompt ๐Ÿ“

Prompt the Gemini API with a text message using the TextPrompt method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var response = await geminiClient.TextPrompt("Write a story about a magic backpack");

Stream Text Prompt ๐Ÿ”

Prompt the Gemini API with a text message using the StreamTextPrompt method:

Note

This diffears from the text prompt, it receives the response as string and in chunks.

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var response = await geminiClient.StreamTextPrompt("Write a story about a magic backpack", (chunk) => {
  Console.WriteLine("Process your chunk of response here");
});

Multiple Text Prompt ๐Ÿ“š

Prompt the Gemini API with multiple text messages using the TextPrompt method with a list of Content objects:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();

var messages = new List<Content>
{
    new Content
    {
        Parts = new List<Part>
        {
            new Part
            {
                Text = "Write a story about a magic backpack"
            }
        }
    },
    // Add more Content objects as needed
};

var response = await geminiClient.TextPrompt(messages);

Get Model ๐Ÿ“’

Get the specific model details of Gemini using GetModel method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var response = await geminiClient.GetModel("gemini-model-v1");

List all models ๐Ÿ”–

Get all Gemini models using GetModels method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var response = await geminiClient.GetModels();

Count Tokens 1๏ธโƒฃ

Prompt the Gemini API with a text message using the CountTokens method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var response = await geminiClient.CountTokens("Write a story about a magic backpack");

Note

You can use list of messages and list of content to call this method too.

Image Prompt ๐Ÿ–ผ๏ธ

Using file

Prompt the Gemini API with an image and a text message using the ImagePrompt method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var image = File.ReadAllBytes("path/to/your/image.jpg");
var response = await geminiClient.ImagePrompt("Describe this image", image, ImageMimeType.Jpeg);

Using Base64 String

Prompt the Gemini API with an base64 string and a text message using the ImagePrompt method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var base64Image = "image-as-base64";
var response = await geminiClient.ImagePrompt("Describe this image in details", base64Image, ImageMimeType.Jpeg);

Embedded ๐Ÿชก

Prompt the Gemini API with a text message and using embedded technique using the EmbeddedContentsPrompt method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var response = await geminiClient.EmbeddedContentsPrompt("Write a story about a magic backpack");

Note

You can use list of messages and list of content to call this method too.

Batch Embedded ๐Ÿชก

Prompt the Gemini API with a text message and using batch embedded technique using the BatchEmbeddedContentsPrompt method:

var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
var response = await geminiClient.EmbeddedContentsPrompt("Write a story about a magic backpack");

Note

You can use list of messages and list of content to call this method too.

Contributing ๐Ÿค

Contributions are welcome! Feel free to open issues or pull requests to enhance the SDK.

License ๐Ÿ“œ

This project is licensed under the MIT License.

dotnet-gemini-sdk's People

Contributors

gsilvamartin avatar michael-herzog avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.