|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Globalization; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Extensions.AI; |
| 11 | +using Microsoft.SemanticKernel; |
| 12 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 13 | +using Microsoft.SemanticKernel.Connectors.Google; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace SemanticKernel.Connectors.Google.UnitTests.Core.Gemini.Clients; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Unit tests for IChatClient-based function calling with Gemini using FunctionChoiceBehavior. |
| 20 | +/// </summary> |
| 21 | +public sealed class GeminiChatClientFunctionCallingTests : IDisposable |
| 22 | +{ |
| 23 | + private readonly HttpClient _httpClient; |
| 24 | + private readonly string _responseContent; |
| 25 | + private readonly string _responseContentWithFunction; |
| 26 | + private readonly HttpMessageHandlerStub _messageHandlerStub; |
| 27 | + private readonly GeminiFunction _timePluginDate, _timePluginNow; |
| 28 | + private readonly Kernel _kernelWithFunctions; |
| 29 | + private const string ChatTestDataFilePath = "./TestData/chat_one_response.json"; |
| 30 | + private const string ChatTestDataWithFunctionFilePath = "./TestData/chat_one_function_response.json"; |
| 31 | + |
| 32 | + public GeminiChatClientFunctionCallingTests() |
| 33 | + { |
| 34 | + this._responseContent = File.ReadAllText(ChatTestDataFilePath); |
| 35 | + this._responseContentWithFunction = File.ReadAllText(ChatTestDataWithFunctionFilePath) |
| 36 | + .Replace("%nameSeparator%", GeminiFunction.NameSeparator, StringComparison.Ordinal); |
| 37 | + this._messageHandlerStub = new HttpMessageHandlerStub(); |
| 38 | + this._messageHandlerStub.ResponseToReturn.Content = new StringContent( |
| 39 | + this._responseContent); |
| 40 | + |
| 41 | + this._httpClient = new HttpClient(this._messageHandlerStub, false); |
| 42 | + |
| 43 | + var kernelPlugin = KernelPluginFactory.CreateFromFunctions("TimePlugin", new[] |
| 44 | + { |
| 45 | + KernelFunctionFactory.CreateFromMethod((string? format = null) |
| 46 | + => DateTime.Now.Date.ToString(format, CultureInfo.InvariantCulture), "Date", "TimePlugin.Date"), |
| 47 | + KernelFunctionFactory.CreateFromMethod(() |
| 48 | + => DateTime.Now.ToString("", CultureInfo.InvariantCulture), "Now", "TimePlugin.Now", |
| 49 | + parameters: [new KernelParameterMetadata("param1") { ParameterType = typeof(string), Description = "desc", IsRequired = false }]), |
| 50 | + }); |
| 51 | + IList<KernelFunctionMetadata> functions = kernelPlugin.GetFunctionsMetadata(); |
| 52 | + |
| 53 | + this._timePluginDate = functions[0].ToGeminiFunction(); |
| 54 | + this._timePluginNow = functions[1].ToGeminiFunction(); |
| 55 | + |
| 56 | + this._kernelWithFunctions = new Kernel(); |
| 57 | + this._kernelWithFunctions.Plugins.Add(kernelPlugin); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public async Task ChatClientShouldConvertToIChatClientSuccessfullyAsync() |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + var chatCompletionService = this.CreateChatCompletionService(); |
| 65 | + |
| 66 | + // Act |
| 67 | + var chatClient = chatCompletionService.AsChatClient(); |
| 68 | + |
| 69 | + // Assert - Verify conversion works |
| 70 | + Assert.NotNull(chatClient); |
| 71 | + Assert.IsAssignableFrom<IChatClient>(chatClient); |
| 72 | + |
| 73 | + // Verify we can make a basic call through IChatClient |
| 74 | + var messages = new List<ChatMessage> |
| 75 | + { |
| 76 | + new(ChatRole.User, "What time is it?") |
| 77 | + }; |
| 78 | + |
| 79 | + var response = await chatClient.GetResponseAsync(messages); |
| 80 | + |
| 81 | + Assert.NotNull(response); |
| 82 | + Assert.NotEmpty(response.Messages); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task ChatClientShouldReceiveFunctionCallsInResponseAsync() |
| 87 | + { |
| 88 | + // Arrange |
| 89 | + this._messageHandlerStub.ResponseToReturn.Content = new StringContent(this._responseContentWithFunction); |
| 90 | + var chatCompletionService = this.CreateChatCompletionService(); |
| 91 | + var chatClient = chatCompletionService.AsChatClient(); |
| 92 | + |
| 93 | + var settings = new GeminiPromptExecutionSettings |
| 94 | + { |
| 95 | + FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: false) |
| 96 | + }; |
| 97 | + var chatOptions = settings.ToChatOptions(this._kernelWithFunctions); |
| 98 | + |
| 99 | + var messages = new List<ChatMessage> |
| 100 | + { |
| 101 | + new(ChatRole.User, "What time is it?") |
| 102 | + }; |
| 103 | + |
| 104 | + // Act |
| 105 | + var response = await chatClient.GetResponseAsync(messages, chatOptions); |
| 106 | + |
| 107 | + // Assert - Verify that FunctionCallContent is returned in the response |
| 108 | + Assert.NotNull(response); |
| 109 | + var functionCalls = response.Messages |
| 110 | + .SelectMany(m => m.Contents) |
| 111 | + .OfType<Microsoft.Extensions.AI.FunctionCallContent>() |
| 112 | + .ToList(); |
| 113 | + |
| 114 | + Assert.NotEmpty(functionCalls); |
| 115 | + var functionCall = functionCalls.First(); |
| 116 | + Assert.Contains(this._timePluginNow.FunctionName, functionCall.Name, StringComparison.OrdinalIgnoreCase); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public async Task ChatClientShouldStreamResponsesAsync() |
| 121 | + { |
| 122 | + // Arrange |
| 123 | + var chatCompletionService = this.CreateChatCompletionService(); |
| 124 | + var chatClient = chatCompletionService.AsChatClient(); |
| 125 | + |
| 126 | + var settings = new GeminiPromptExecutionSettings |
| 127 | + { |
| 128 | + FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() |
| 129 | + }; |
| 130 | + var chatOptions = settings.ToChatOptions(this._kernelWithFunctions); |
| 131 | + |
| 132 | + var messages = new List<ChatMessage> |
| 133 | + { |
| 134 | + new(ChatRole.User, "What time is it?") |
| 135 | + }; |
| 136 | + |
| 137 | + // Act |
| 138 | + var updates = new List<ChatResponseUpdate>(); |
| 139 | + await foreach (var update in chatClient.GetStreamingResponseAsync(messages, chatOptions)) |
| 140 | + { |
| 141 | + updates.Add(update); |
| 142 | + } |
| 143 | + |
| 144 | + // Assert - Verify that streaming works and returns updates |
| 145 | + Assert.NotEmpty(updates); |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + public async Task AsChatClientConvertsServiceToIChatClientAsync() |
| 150 | + { |
| 151 | + // Arrange |
| 152 | + var chatCompletionService = this.CreateChatCompletionService(); |
| 153 | + |
| 154 | + // Act |
| 155 | + var chatClient = chatCompletionService.AsChatClient(); |
| 156 | + |
| 157 | + // Assert |
| 158 | + Assert.NotNull(chatClient); |
| 159 | + Assert.IsAssignableFrom<IChatClient>(chatClient); |
| 160 | + } |
| 161 | + |
| 162 | + private GoogleAIGeminiChatCompletionService CreateChatCompletionService(HttpClient? httpClient = null) |
| 163 | + { |
| 164 | + return new GoogleAIGeminiChatCompletionService( |
| 165 | + modelId: "fake-model", |
| 166 | + apiKey: "fake-key", |
| 167 | + apiVersion: GoogleAIVersion.V1, |
| 168 | + httpClient: httpClient ?? this._httpClient); |
| 169 | + } |
| 170 | + |
| 171 | + public void Dispose() |
| 172 | + { |
| 173 | + this._httpClient.Dispose(); |
| 174 | + this._messageHandlerStub.Dispose(); |
| 175 | + } |
| 176 | +} |
0 commit comments