Skip to content

Commit

Permalink
[.Net] Add Gemini samples to AutoGen.Net website + configure Gemini p…
Browse files Browse the repository at this point in the history
…ackage to be ready for release (#2917)

* update website

* fix buid error

* update
  • Loading branch information
LittleLittleCloud authored Jun 13, 2024
1 parent 004ba22 commit ca4f717
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Chat_With_Google_Gemini.cs

#region Using
using AutoGen.Core;
using AutoGen.Gemini.Middleware;
#endregion Using
using FluentAssertions;

namespace AutoGen.Gemini.Sample;
Expand All @@ -11,6 +12,7 @@ public class Chat_With_Google_Gemini
{
public static async Task RunAsync()
{
#region Create_Gemini_Agent
var apiKey = Environment.GetEnvironmentVariable("GOOGLE_GEMINI_API_KEY");

if (apiKey is null)
Expand All @@ -19,7 +21,6 @@ public static async Task RunAsync()
return;
}

#region Create_Gemini_Agent
var geminiAgent = new GeminiChatAgent(
name: "gemini",
model: "gemini-1.5-flash-001",
Expand All @@ -29,7 +30,9 @@ public static async Task RunAsync()
.RegisterPrintMessage();
#endregion Create_Gemini_Agent

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

#region verify_reply
reply.Should().BeOfType<TextMessage>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Chat_With_Vertex_Gemini.cs

#region Using
using AutoGen.Core;
using AutoGen.Gemini.Middleware;
#endregion Using
using FluentAssertions;

namespace AutoGen.Gemini.Sample;
Expand All @@ -11,6 +12,7 @@ public class Chat_With_Vertex_Gemini
{
public static async Task RunAsync()
{
#region Create_Gemini_Agent
var projectID = Environment.GetEnvironmentVariable("GCP_VERTEX_PROJECT_ID");

if (projectID is null)
Expand All @@ -19,7 +21,6 @@ public static async Task RunAsync()
return;
}

#region Create_Gemini_Agent
var geminiAgent = new GeminiChatAgent(
name: "gemini",
model: "gemini-1.5-flash-001",
Expand All @@ -30,7 +31,9 @@ public static async Task RunAsync()
.RegisterPrintMessage();
#endregion Create_Gemini_Agent

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

#region verify_reply
reply.Should().BeOfType<TextMessage>();
Expand Down
10 changes: 6 additions & 4 deletions dotnet/sample/AutoGen.Gemini.Sample/Function_Call_With_Gemini.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Function_Call_With_Gemini.cs

#region Using
using AutoGen.Core;
using AutoGen.Gemini.Middleware;
using FluentAssertions;
using Google.Cloud.AIPlatform.V1;
#endregion Using
using FluentAssertions;

namespace AutoGen.Gemini.Sample;

#region MovieFunction
public partial class MovieFunction
{
/// <summary>
Expand Down Expand Up @@ -58,8 +60,8 @@ public async Task<string> GetShowtimes(string location, string movie, string the

return result;
}

}
#endregion MovieFunction

/// <summary>
/// Modified from https://ai.google.dev/gemini-api/docs/function-calling
Expand All @@ -68,6 +70,7 @@ public partial class Function_Call_With_Gemini
{
public static async Task RunAsync()
{
#region Create_Gemini_Agent
var projectID = Environment.GetEnvironmentVariable("GCP_VERTEX_PROJECT_ID");

if (projectID is null)
Expand All @@ -90,7 +93,6 @@ public static async Task RunAsync()
{ movieFunction.GetShowtimesFunctionContract.Name!, movieFunction.GetShowtimesWrapper },
});

#region Create_Gemini_Agent
var geminiAgent = new GeminiChatAgent(
name: "gemini",
model: "gemini-1.5-flash-001",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Image_Chat_With_Vertex_Gemini.cs

#region Using
using AutoGen.Core;
using AutoGen.Gemini.Middleware;
#endregion Using
using FluentAssertions;

namespace AutoGen.Gemini.Sample;
Expand All @@ -11,6 +12,7 @@ public class Image_Chat_With_Vertex_Gemini
{
public static async Task RunAsync()
{
#region Create_Gemini_Agent
var projectID = Environment.GetEnvironmentVariable("GCP_VERTEX_PROJECT_ID");

if (projectID is null)
Expand All @@ -19,7 +21,6 @@ public static async Task RunAsync()
return;
}

#region Create_Gemini_Agent
var geminiAgent = new GeminiChatAgent(
name: "gemini",
model: "gemini-1.5-flash-001",
Expand Down
9 changes: 9 additions & 0 deletions dotnet/src/AutoGen.Gemini/AutoGen.Gemini.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<Import Project="$(RepoRoot)/nuget/nuget-package.props" />

<PropertyGroup>
<!-- NuGet Package Settings -->
<Title>AutoGen.Gemini</Title>
<Description>
This package provides the intergration with Gemini.
</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.AIPlatform.V1" Version="$(GoogleCloudAPIPlatformVersion)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using AutoGen.Core;

namespace AutoGen.Gemini.Middleware;
namespace AutoGen.Gemini;

public static class GeminiAgentExtension
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using static Google.Cloud.AIPlatform.V1.Candidate.Types;
using IMessage = AutoGen.Core.IMessage;

namespace AutoGen.Gemini.Middleware;
namespace AutoGen.Gemini;

public class GeminiMessageConnector : IStreamingMiddleware
{
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/AutoGen/AutoGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<ItemGroup>
<ProjectReference Include="..\AutoGen.LMStudio\AutoGen.LMStudio.csproj" />
<ProjectReference Include="..\AutoGen.Mistral\AutoGen.Mistral.csproj" />
<ProjectReference Include="..\AutoGen.Ollama\AutoGen.Ollama.csproj" />
<ProjectReference Include="..\AutoGen.Gemini\AutoGen.Gemini.csproj" />
<ProjectReference Include="..\AutoGen.SemanticKernel\AutoGen.SemanticKernel.csproj" />
<ProjectReference Include="..\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion dotnet/test/AutoGen.Gemini.Tests/GeminiAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using AutoGen.Gemini.Extension;
using static Google.Cloud.AIPlatform.V1.Part;
using Xunit.Abstractions;
using AutoGen.Gemini.Middleware;
namespace AutoGen.Gemini.Tests;

public class GeminiAgentTests
Expand Down
1 change: 0 additions & 1 deletion dotnet/test/AutoGen.Gemini.Tests/GeminiMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// GeminiMessageTests.cs

using AutoGen.Core;
using AutoGen.Gemini.Middleware;
using AutoGen.Tests;
using FluentAssertions;
using Google.Cloud.AIPlatform.V1;
Expand Down
31 changes: 31 additions & 0 deletions dotnet/website/articles/AutoGen.Gemini/Chat-with-google-gemini.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
This example shows how to use @AutoGen.Gemini.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](https://gemini.google.com/).

> [!NOTE]
> You can find the complete sample code [here](https://github.com/microsoft/autogen/blob/main/dotnet/sample/AutoGen.Gemini.Sample/Chat_With_Google_Gemini.cs)
> [!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](https://stackoverflow.com/questions/78007243/utilizing-gemini-through-vertex-ai-or-through-google-generative-ai))
### Step 1: Install AutoGen.Gemini

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

```bash
dotnet add package AutoGen.Gemini
```

### Step 2: Add using statement

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Chat_With_Google_Gemini.cs?name=Using)]

### Step 3: Create a Gemini agent

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Chat_With_Google_Gemini.cs?name=Create_Gemini_Agent)]

### Step 4: Chat with Gemini

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Chat_With_Google_Gemini.cs?name=Chat_With_Google_Gemini)]
32 changes: 32 additions & 0 deletions dotnet/website/articles/AutoGen.Gemini/Chat-with-vertex-gemini.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
This example shows how to use @AutoGen.Gemini.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](https://cloud.google.com/vertex-ai/docs).

> [!NOTE]
> You can find the complete sample code [here](https://github.com/microsoft/autogen/blob/main/dotnet/sample/AutoGen.Gemini.Sample/Chat_With_Vertex_Gemini.cs)
> [!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](https://stackoverflow.com/questions/78007243/utilizing-gemini-through-vertex-ai-or-through-google-generative-ai))
### Step 1: Install AutoGen.Gemini

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

```bash
dotnet add package AutoGen.Gemini
```

### Step 2: Add using statement

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Chat_With_Vertex_Gemini.cs?name=Using)]

### Step 3: Create a Gemini agent

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Chat_With_Vertex_Gemini.cs?name=Create_Gemini_Agent)]


### Step 4: Chat with Gemini

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Chat_With_Vertex_Gemini.cs?name=Chat_With_Vertex_Gemini)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
This example shows how to use @AutoGen.Gemini.GeminiChatAgent to make function call. This example is modified from [gemini-api function call example](https://ai.google.dev/gemini-api/docs/function-calling)

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](https://cloud.google.com/vertex-ai/docs).


> [!NOTE]
> You can find the complete sample code [here](https://github.com/microsoft/autogen/blob/main/dotnet/sample/AutoGen.Gemini.Sample/Function_Call_With_Gemini.cs)
### Step 1: Install AutoGen.Gemini and AutoGen.SourceGenerator

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

```bash
dotnet add package AutoGen.Gemini
dotnet add package AutoGen.SourceGenerator
```

The AutoGen.SourceGenerator package is required to generate the @AutoGen.Core.FunctionContract. For more information, please refer to [Create-type-safe-function-call](../Create-type-safe-function-call.md)

### Step 2: Add using statement
[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Function_call_with_gemini.cs?name=Using)]

### Step 3: Create `MovieFunction`

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Function_call_with_gemini.cs?name=MovieFunction)]

### Step 4: Create a Gemini agent

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Function_call_with_gemini.cs?name=Create_Gemini_Agent)]

### Step 5: Single turn function call

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Function_call_with_gemini.cs?name=Single_turn)]

### Step 6: Multi-turn function call

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Function_call_with_gemini.cs?name=Multi_turn)]

25 changes: 25 additions & 0 deletions dotnet/website/articles/AutoGen.Gemini/Image-chat-with-gemini.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This example shows how to use @AutoGen.Gemini.GeminiChatAgent for image 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](https://cloud.google.com/vertex-ai/docs).


> [!NOTE]
> You can find the complete sample code [here](https://github.com/microsoft/autogen/blob/main/dotnet/sample/AutoGen.Gemini.Sample/Image_Chat_With_Vertex_Gemini.cs)
### Step 1: Install AutoGen.Gemini

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

```bash
dotnet add package AutoGen.Gemini
```

### Step 2: Add using statement
[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Image_Chat_With_Vertex_Gemini.cs?name=Using)]

### Step 3: Create a Gemini agent

[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Image_Chat_With_Vertex_Gemini.cs?name=Create_Gemini_Agent)]

### Step 4: Send image to Gemini
[!code-csharp[](../../../sample/AutoGen.Gemini.Sample/Image_Chat_With_Vertex_Gemini.cs?name=Send_Image_Request)]
12 changes: 12 additions & 0 deletions dotnet/website/articles/AutoGen.Gemini/Overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# AutoGen.Gemini Overview

AutoGen.Gemini is a package that provides seamless integration with Google Gemini. It provides the following agent:

- @AutoGen.Gemini.GeminiChatAgent: The agent that connects to Google Gemini or Vertex AI Gemini. It supports chat, multi-modal chat, and function call.

AutoGen.Gemini also provides the following middleware:
- @AutoGen.Gemini.GeminiMessageConnector: The middleware that converts the Gemini message to AutoGen built-in message type.

## Examples

You can find more examples under the [gemini sample project](https://github.com/microsoft/autogen/tree/main/dotnet/sample/AutoGen.Gemini.Sample)
1 change: 1 addition & 0 deletions dotnet/website/articles/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ AutoGen.Net provides the following packages, you can choose to install one or mo
- `AutoGen.Anthropic`: This package provides the integration agents for [Anthropic](https://www.anthropic.com/api)
- `AutoGen.LMStudio`: This package provides the integration agents from LM Studio.
- `AutoGen.SemanticKernel`: This package provides the integration agents over semantic kernel.
- `AutoGen.Gemini`: This package provides the integration agents from [Google Gemini](https://gemini.google.com/).
- `AutoGen.SourceGenerator`: This package carries a source generator that adds support for type-safe function definition generation.
- `AutoGen.DotnetInteractive`: This packages carries dotnet interactive support to execute dotnet code snippet.

Expand Down
17 changes: 17 additions & 0 deletions dotnet/website/articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
href: AutoGen.SemanticKernel/SemanticKernelAgent-support-more-messages.md
- name: Use kernel plugin in other agents
href: AutoGen.SemanticKernel/Use-kernel-plugin-in-other-agents.md

- name: AutoGen.Ollama
items:
- name: Examples
Expand All @@ -87,6 +88,22 @@
href: AutoGen.Ollama/Chat-with-LLaMA.md
- name: MultiModal Chat with LLaVA
href: AutoGen.Ollama/Chat-with-LLaVA.md

- name: AutoGen.Gemini
items:
- name: Overview
href: AutoGen.Gemini/Overview.md
- name: Examples
items:
- name: Chat with Google AI Gemini
href: AutoGen.Gemini/Chat-with-google-gemini.md
- name: Chat with Vertex AI Gemini
href: AutoGen.Gemini/Chat-with-vertex-gemini.md
- name: Function call with Gemini
href: AutoGen.Gemini/Function-call-with-gemini.md
- name: Image chat with Gemini
href: AutoGen.Gemini/Image-chat-with-gemini.md

- name: AutoGen.Mistral
items:
- name: Overview
Expand Down

0 comments on commit ca4f717

Please sign in to comment.