-
Notifications
You must be signed in to change notification settings - Fork 4.3k
.Net: Add FunctionChoiceBehavior support to Google Gemini connector #13256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/fix-gemini-integration-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
852deac
Initial plan
Copilot 86da0d1
Add FunctionChoiceBehavior support to Google Gemini connector
Copilot 94e5600
Add tests for FunctionChoiceBehavior support and fix conversion logic
Copilot 56cf4ea
Address code review comments: optimize Kernel creation and improve ex…
Copilot 78dc4ef
Add IChatClient end-to-end tests for Gemini function calling
Copilot d83275a
Replace integration tests with IChatClient-based unit tests for Gemin…
Copilot 9a3d5bf
Update tests to use only IChatClient methods instead of IChatCompleti…
Copilot 86b25a8
Use static readonly field for shared empty Kernel instance
Copilot 34cef15
Remove unnecessary using System directive from GeminiStreamingChatMes…
Copilot c08886a
Merge branch 'main' into copilot/fix-gemini-integration-issue
stephentoub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
239 changes: 239 additions & 0 deletions
239
dotnet/src/IntegrationTests/Connectors/Google/Gemini/GeminiChatClientFunctionCallingTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.SemanticKernel; | ||
| using Microsoft.SemanticKernel.ChatCompletion; | ||
| using Microsoft.SemanticKernel.Connectors.Google; | ||
| using xRetry; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace SemanticKernel.IntegrationTests.Connectors.Google.Gemini; | ||
|
|
||
| public sealed class GeminiChatClientFunctionCallingTests(ITestOutputHelper output) : TestsBase(output) | ||
| { | ||
| private const string SkipMessage = "This test is for manual verification."; | ||
|
|
||
| [RetryTheory(Skip = SkipMessage)] | ||
| [InlineData(ServiceType.GoogleAI, true)] | ||
| [InlineData(ServiceType.VertexAI, false)] | ||
| public async Task ChatClientWithAutoFunctionChoiceBehaviorCallsKernelFunctionAsync(ServiceType serviceType, bool isBeta) | ||
| { | ||
| // Arrange | ||
| var chatCompletionService = this.GetChatService(serviceType, isBeta); | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| var kernel = new Kernel(); | ||
| kernel.ImportPluginFromType<LightsPlugin>(); | ||
|
|
||
| var settings = new GeminiPromptExecutionSettings | ||
| { | ||
| FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() | ||
| }; | ||
| var chatOptions = settings.ToChatOptions(kernel); | ||
|
|
||
| var messages = new List<ChatMessage> | ||
| { | ||
| new(ChatRole.User, "Turn on the table lamp") | ||
| }; | ||
|
|
||
| // Act | ||
| var response = await chatClient.GetResponseAsync(messages, chatOptions); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
|
|
||
| // The response should indicate the function was called | ||
| // Since we're using auto-invoke, the function result should be in the chat history | ||
| var responseText = string.Join(" ", response.Messages.Select(m => m.Text)); | ||
| this.Output.WriteLine($"Response: {responseText}"); | ||
| } | ||
|
|
||
| [RetryTheory(Skip = SkipMessage)] | ||
| [InlineData(ServiceType.GoogleAI, true)] | ||
| [InlineData(ServiceType.VertexAI, false)] | ||
| public async Task ChatClientWithAutoFunctionChoiceBehaviorInvokesMultipleFunctionsAsync(ServiceType serviceType, bool isBeta) | ||
| { | ||
| // Arrange | ||
| var chatCompletionService = this.GetChatService(serviceType, isBeta); | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| var kernel = new Kernel(); | ||
| kernel.ImportPluginFromType<LightsPlugin>(); | ||
|
|
||
| var settings = new GeminiPromptExecutionSettings | ||
| { | ||
| FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: true) | ||
| }; | ||
| var chatOptions = settings.ToChatOptions(kernel); | ||
|
|
||
| var messages = new List<ChatMessage> | ||
| { | ||
| new(ChatRole.User, "Get the list of available lights and turn on the floor lamp") | ||
| }; | ||
|
|
||
| // Act | ||
| var response = await chatClient.GetResponseAsync(messages, chatOptions); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
|
|
||
| // The response should indicate both functions were called | ||
| var responseText = string.Join(" ", response.Messages.Select(m => m.Text)); | ||
| this.Output.WriteLine($"Response: {responseText}"); | ||
|
|
||
| // Verify the response mentions the lights and the floor lamp being turned on | ||
| Assert.NotEmpty(responseText); | ||
| } | ||
|
|
||
| [RetryTheory(Skip = SkipMessage)] | ||
| [InlineData(ServiceType.GoogleAI, true)] | ||
| [InlineData(ServiceType.VertexAI, false)] | ||
| public async Task ChatClientWithManualFunctionChoiceBehaviorReturnsFunctionCallsAsync(ServiceType serviceType, bool isBeta) | ||
| { | ||
| // Arrange | ||
| var chatCompletionService = this.GetChatService(serviceType, isBeta); | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| var kernel = new Kernel(); | ||
| kernel.ImportPluginFromType<LightsPlugin>(); | ||
|
|
||
| var settings = new GeminiPromptExecutionSettings | ||
| { | ||
| FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: false) | ||
| }; | ||
| var chatOptions = settings.ToChatOptions(kernel); | ||
|
|
||
| var messages = new List<ChatMessage> | ||
| { | ||
| new(ChatRole.User, "Turn on the ceiling light") | ||
| }; | ||
|
|
||
| // Act | ||
| var response = await chatClient.GetResponseAsync(messages, chatOptions); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(response); | ||
|
|
||
| // Extract function calls from the response | ||
| var functionCalls = response.Messages | ||
| .SelectMany(m => m.Contents) | ||
| .OfType<Microsoft.Extensions.AI.FunctionCallContent>() | ||
| .ToList(); | ||
|
|
||
| Assert.NotNull(functionCalls); | ||
| Assert.NotEmpty(functionCalls); | ||
|
|
||
| var functionCall = functionCalls.First(); | ||
| this.Output.WriteLine($"Function call: {functionCall.Name}"); | ||
|
|
||
| // Verify the function name contains the expected plugin and function | ||
| Assert.Contains("TurnOn", functionCall.Name, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| [RetryTheory(Skip = SkipMessage)] | ||
| [InlineData(ServiceType.GoogleAI, true)] | ||
| [InlineData(ServiceType.VertexAI, false)] | ||
| public async Task ChatClientStreamingWithAutoFunctionChoiceBehaviorCallsKernelFunctionAsync(ServiceType serviceType, bool isBeta) | ||
| { | ||
| // Arrange | ||
| var chatCompletionService = this.GetChatService(serviceType, isBeta); | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| var kernel = new Kernel(); | ||
| kernel.ImportPluginFromType<LightsPlugin>(); | ||
|
|
||
| var settings = new GeminiPromptExecutionSettings | ||
| { | ||
| FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: true) | ||
| }; | ||
| var chatOptions = settings.ToChatOptions(kernel); | ||
|
|
||
| var messages = new List<ChatMessage> | ||
| { | ||
| new(ChatRole.User, "Get the list of available lights") | ||
| }; | ||
|
|
||
| string result = ""; | ||
|
|
||
| // Act | ||
| await foreach (var update in chatClient.GetStreamingResponseAsync(messages, chatOptions)) | ||
| { | ||
| foreach (var content in update.Contents) | ||
| { | ||
| if (content is Microsoft.Extensions.AI.TextContent textContent) | ||
| { | ||
| result += textContent.Text; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Assert | ||
| Assert.NotEmpty(result); | ||
| this.Output.WriteLine($"Streaming response: {result}"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A plugin that provides light control functionality. | ||
| /// </summary> | ||
| #pragma warning disable CA1812 // Avoid uninstantiated internal classes | ||
| private sealed class LightsPlugin | ||
| #pragma warning restore CA1812 | ||
| { | ||
| private readonly Dictionary<int, string> _lights = new() | ||
| { | ||
| { 1, "Table Lamp" }, | ||
| { 2, "Floor Lamp" }, | ||
| { 3, "Ceiling Light" } | ||
| }; | ||
|
|
||
| private readonly HashSet<int> _lightsOn = new(); | ||
|
|
||
| [KernelFunction] | ||
| [Description("Get a list of available lights")] | ||
| public string GetLights() | ||
| { | ||
| return string.Join(", ", this._lights.Select(kv => $"{kv.Key}: {kv.Value}")); | ||
| } | ||
|
|
||
| [KernelFunction] | ||
| [Description("Turn on a specific light")] | ||
| public string TurnOn([Description("The ID of the light to turn on")] int lightId) | ||
| { | ||
| if (!this._lights.TryGetValue(lightId, out var lightName)) | ||
| { | ||
| return $"Light {lightId} not found"; | ||
| } | ||
|
|
||
| this._lightsOn.Add(lightId); | ||
| return $"Turned on {lightName}"; | ||
| } | ||
|
|
||
| [KernelFunction] | ||
| [Description("Turn off a specific light")] | ||
| public string TurnOff([Description("The ID of the light to turn off")] int lightId) | ||
| { | ||
| if (!this._lights.TryGetValue(lightId, out var lightName)) | ||
| { | ||
| return $"Light {lightId} not found"; | ||
| } | ||
|
|
||
| this._lightsOn.Remove(lightId); | ||
| return $"Turned off {lightName}"; | ||
| } | ||
|
|
||
| [KernelFunction] | ||
| [Description("Get the status of all lights")] | ||
| public string GetStatus() | ||
| { | ||
| var status = this._lights.Select(kv => | ||
| $"{kv.Value}: {(this._lightsOn.Contains(kv.Key) ? "On" : "Off")}"); | ||
| return string.Join(", ", status); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.