Table of Contents

This example shows how to use GeminiChatAgent to connect to Google AI Gemini and chat with Gemini model.

To run this example, you need to have a Google AI Gemini API key. For how to get a Google Gemini API key, please refer to Google Gemini.

Note

You can find the complete sample code here

Note

What's the difference between Google AI Gemini and Vertex AI Gemini?

Gemini is a series of large language models developed by Google. You can use it either from Google AI API or Vertex AI API. If you are relatively new to Gemini and wants to explore the feature and build some prototype for your chatbot app, Google AI APIs (with Google AI Studio) is a fast way to get started. While your app and idea matures and you'd like to leverage more MLOps tools that streamline the usage, deployment, and monitoring of models, you can move to Google Cloud Vertex AI which provides Gemini APIs along with many other features. Basically, to help you productionize your app. (reference)

Step 1: Install AutoGen.Gemini

First, install the AutoGen.Gemini package using the following command:

dotnet add package AutoGen.Gemini

Step 2: Add using statement

using AutoGen.Core;

Step 3: Create a Gemini agent

var apiKey = Environment.GetEnvironmentVariable("GOOGLE_GEMINI_API_KEY");

if (apiKey is null)
{
    Console.WriteLine("Please set GOOGLE_GEMINI_API_KEY environment variable.");
    return;
}

var geminiAgent = new GeminiChatAgent(
        name: "gemini",
        model: "gemini-1.5-flash-001",
        apiKey: apiKey,
        systemMessage: "You are a helpful C# engineer, put your code between ```csharp and ```, don't explain the code")
    .RegisterMessageConnector()
    .RegisterPrintMessage();

Step 4: Chat with Gemini

var reply = await geminiAgent.SendAsync("Can you write a piece of C# code to calculate 100th of fibonacci?");