This repository contains a .NET console application that demonstrates how to use the Google Gemini API to generate text-based content. The application uses the DotnetGeminiSDK to interact with the API, providing a simple example of how to integrate AI content generation into a .NET project.
- .NET 6.0 or higher
- DotnetGeminiSDK
- A valid Google Gemini API key
- Clone this repository:
git clone https://github.com/yourusername/gemmi.git- Navigate to the project directory:
cd gemmi- Open the project in Visual Studio 2022 (or your preferred .NET IDE).
The project requires two environment variables to be set:
API_BASE_URL: The base URL for the Google Gemini API.API_KEY: Your API key for accessing the Google Gemini API.
These environment variables can be configured in the launchSettings.json file as shown below:
"profiles": {
"Gemmi": {
"commandName": "Project",
"environmentVariables": {
"API_BASE_URL": "https://generativelanguage.googleapis.com/v1/models/gemini-pro",
"API_KEY": "YOUR_API_KEY_HERE"
}
},
"Container (Dockerfile)": {
"commandName": "Docker",
"environmentVariables": {
"API_BASE_URL": "https://generativelanguage.googleapis.com/v1/models/gemini-pro",
"API_KEY": "YOUR_API_KEY_HERE"
}
}
}Replace "YOUR_API_KEY_HERE" with your actual API key.
To run the application:
-
Build and run the project in Visual Studio.
-
The application will send a prompt to the Google Gemini API and output the generated content to the console.
The core logic is contained in Program.cs:
using System;
using System.Threading.Tasks;
using DotnetGeminiSDK;
using DotnetGeminiSDK.Client.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using DotnetGeminiSDK.Model.Response;
namespace Gemmi
{
internal class Program
{
private readonly IGeminiClient _geminiClient;
public Program(IGeminiClient geminiClient)
{
_geminiClient = geminiClient;
}
public async Task<GeminiMessageResponse> Example()
{
var response = await _geminiClient.TextPrompt("Bana insanlık tarihini anlat.");
return response;
}
public static async Task Main(string[] args)
{
var baseUrl = Environment.GetEnvironmentVariable("API_BASE_URL");
var apiKey = Environment.GetEnvironmentVariable("API_KEY");
var serviceProvider = new ServiceCollection()
.AddGeminiClient(config =>
{
config.TextBaseUrl = baseUrl;
config.ApiKey = apiKey;
})
.BuildServiceProvider();
var geminiClient = serviceProvider.GetRequiredService<IGeminiClient>();
Program program = new Program(geminiClient);
var response = await program.Example();
Console.WriteLine(response.Candidates[0].Content.Parts[0].Text.ToString());
Console.WriteLine("Application will exit automatically.");
}
}
}The project includes a Dockerfile for containerization:
- To build the Docker image:
docker build -t gemmi .- To run the container:
docker run --rm -it gemmiMake sure your environment variables are set correctly in the Docker environment.
Feel free to fork this repository, make changes, and submit pull requests. Any contributions are welcome!
This project is licensed under the MIT License. See the LICENSE file for more details.