Table of Contents

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

To run this example, you need to have a project on Google Cloud with access to Vertex AI API. For more information please refer to Google Vertex AI.

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 projectID = Environment.GetEnvironmentVariable("GCP_VERTEX_PROJECT_ID");

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

var geminiAgent = new GeminiChatAgent(
        name: "gemini",
        model: "gemini-1.5-flash-001",
        location: "us-east1",
        project: projectID,
        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?");