-
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.
+412
−3
Open
Changes from all commits
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
178 changes: 178 additions & 0 deletions
178
...s/Connectors.Google.UnitTests/Core/Gemini/Clients/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,178 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.SemanticKernel; | ||
| using Microsoft.SemanticKernel.ChatCompletion; | ||
| using Microsoft.SemanticKernel.Connectors.Google; | ||
| using Microsoft.SemanticKernel.Connectors.Google.Core; | ||
| using Xunit; | ||
|
|
||
| namespace SemanticKernel.Connectors.Google.UnitTests.Core.Gemini.Clients; | ||
|
|
||
| /// <summary> | ||
| /// Unit tests for IChatClient-based function calling with Gemini using FunctionChoiceBehavior. | ||
| /// </summary> | ||
| public sealed class GeminiChatClientFunctionCallingTests : IDisposable | ||
| { | ||
| private readonly HttpClient _httpClient; | ||
| private readonly string _responseContent; | ||
| private readonly string _responseContentWithFunction; | ||
| private readonly HttpMessageHandlerStub _messageHandlerStub; | ||
| private readonly GeminiFunction _timePluginDate, _timePluginNow; | ||
| private readonly Kernel _kernelWithFunctions; | ||
| private const string ChatTestDataFilePath = "./TestData/chat_one_response.json"; | ||
| private const string ChatTestDataWithFunctionFilePath = "./TestData/chat_one_function_response.json"; | ||
|
|
||
| public GeminiChatClientFunctionCallingTests() | ||
| { | ||
| this._responseContent = File.ReadAllText(ChatTestDataFilePath); | ||
| this._responseContentWithFunction = File.ReadAllText(ChatTestDataWithFunctionFilePath) | ||
| .Replace("%nameSeparator%", GeminiFunction.NameSeparator, StringComparison.Ordinal); | ||
| this._messageHandlerStub = new HttpMessageHandlerStub(); | ||
| this._messageHandlerStub.ResponseToReturn.Content = new StringContent( | ||
| this._responseContent); | ||
|
|
||
| this._httpClient = new HttpClient(this._messageHandlerStub, false); | ||
|
|
||
| var kernelPlugin = KernelPluginFactory.CreateFromFunctions("TimePlugin", new[] | ||
| { | ||
| KernelFunctionFactory.CreateFromMethod((string? format = null) | ||
| => DateTime.Now.Date.ToString(format, CultureInfo.InvariantCulture), "Date", "TimePlugin.Date"), | ||
| KernelFunctionFactory.CreateFromMethod(() | ||
| => DateTime.Now.ToString("", CultureInfo.InvariantCulture), "Now", "TimePlugin.Now", | ||
| parameters: [new KernelParameterMetadata("param1") { ParameterType = typeof(string), Description = "desc", IsRequired = false }]), | ||
| }); | ||
| IList<KernelFunctionMetadata> functions = kernelPlugin.GetFunctionsMetadata(); | ||
|
|
||
| this._timePluginDate = functions[0].ToGeminiFunction(); | ||
| this._timePluginNow = functions[1].ToGeminiFunction(); | ||
|
|
||
| this._kernelWithFunctions = new Kernel(); | ||
| this._kernelWithFunctions.Plugins.Add(kernelPlugin); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ChatClientShouldConvertToIChatClientSuccessfullyAsync() | ||
| { | ||
| // Arrange | ||
| var chatCompletionService = this.CreateChatCompletionService(); | ||
|
|
||
| // Act | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| // Assert - Verify conversion works | ||
| Assert.NotNull(chatClient); | ||
| Assert.IsAssignableFrom<IChatClient>(chatClient); | ||
|
|
||
| // Verify we can make a basic call through IChatClient | ||
| var messages = new List<ChatMessage> | ||
| { | ||
| new(ChatRole.User, "What time is it?") | ||
| }; | ||
|
|
||
| var response = await chatClient.GetResponseAsync(messages); | ||
|
|
||
| Assert.NotNull(response); | ||
| Assert.NotEmpty(response.Messages); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ChatClientShouldReceiveFunctionCallsInResponseAsync() | ||
| { | ||
| // Arrange | ||
| this._messageHandlerStub.ResponseToReturn.Content = new StringContent(this._responseContentWithFunction); | ||
| var chatCompletionService = this.CreateChatCompletionService(); | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| var settings = new GeminiPromptExecutionSettings | ||
| { | ||
| FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: false) | ||
| }; | ||
| var chatOptions = settings.ToChatOptions(this._kernelWithFunctions); | ||
|
|
||
| var messages = new List<ChatMessage> | ||
| { | ||
| new(ChatRole.User, "What time is it?") | ||
| }; | ||
|
|
||
| // Act | ||
| var response = await chatClient.GetResponseAsync(messages, chatOptions); | ||
|
|
||
| // Assert - Verify that FunctionCallContent is returned in the response | ||
| Assert.NotNull(response); | ||
| var functionCalls = response.Messages | ||
| .SelectMany(m => m.Contents) | ||
| .OfType<Microsoft.Extensions.AI.FunctionCallContent>() | ||
| .ToList(); | ||
|
|
||
| Assert.NotEmpty(functionCalls); | ||
| var functionCall = functionCalls.First(); | ||
| Assert.Contains(this._timePluginNow.FunctionName, functionCall.Name, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ChatClientShouldStreamResponsesAsync() | ||
| { | ||
| // Arrange | ||
| var chatCompletionService = this.CreateChatCompletionService(); | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| var settings = new GeminiPromptExecutionSettings | ||
| { | ||
| FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() | ||
| }; | ||
| var chatOptions = settings.ToChatOptions(this._kernelWithFunctions); | ||
|
|
||
| var messages = new List<ChatMessage> | ||
| { | ||
| new(ChatRole.User, "What time is it?") | ||
| }; | ||
|
|
||
| // Act | ||
| var updates = new List<ChatResponseUpdate>(); | ||
| await foreach (var update in chatClient.GetStreamingResponseAsync(messages, chatOptions)) | ||
| { | ||
| updates.Add(update); | ||
| } | ||
|
|
||
| // Assert - Verify that streaming works and returns updates | ||
| Assert.NotEmpty(updates); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AsChatClientConvertsServiceToIChatClientAsync() | ||
| { | ||
| // Arrange | ||
| var chatCompletionService = this.CreateChatCompletionService(); | ||
|
|
||
| // Act | ||
| var chatClient = chatCompletionService.AsChatClient(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(chatClient); | ||
| Assert.IsAssignableFrom<IChatClient>(chatClient); | ||
| } | ||
|
|
||
| private GoogleAIGeminiChatCompletionService CreateChatCompletionService(HttpClient? httpClient = null) | ||
| { | ||
| return new GoogleAIGeminiChatCompletionService( | ||
| modelId: "fake-model", | ||
| apiKey: "fake-key", | ||
| apiVersion: GoogleAIVersion.V1, | ||
| httpClient: httpClient ?? this._httpClient); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this._httpClient.Dispose(); | ||
| this._messageHandlerStub.Dispose(); | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.