|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.SemanticKernel; |
| 9 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 10 | +using Microsoft.SemanticKernel.Connectors.Onnx; |
| 11 | +using SemanticKernel.IntegrationTests.TestSettings; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace SemanticKernel.IntegrationTests.Connectors.Onnx; |
| 15 | + |
| 16 | +public class OnnxRuntimeGenAIChatCompletionServiceTests : BaseIntegrationTest |
| 17 | +{ |
| 18 | + [Fact(Skip = "For manual verification only")] |
| 19 | + public async Task ItCanUseKernelInvokeAsyncAsync() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + var kernel = this.CreateAndInitializeKernel(); |
| 23 | + |
| 24 | + var func = kernel.CreateFunctionFromPrompt("List the two planets after '{{$input}}', excluding moons, using bullet points."); |
| 25 | + |
| 26 | + // Act |
| 27 | + var result = await func.InvokeAsync(kernel, new() { ["input"] = "Jupiter" }); |
| 28 | + |
| 29 | + // Assert |
| 30 | + Assert.NotNull(result); |
| 31 | + Assert.Contains("Saturn", result.GetValue<string>(), StringComparison.InvariantCultureIgnoreCase); |
| 32 | + Assert.Contains("Uranus", result.GetValue<string>(), StringComparison.InvariantCultureIgnoreCase); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact(Skip = "For manual verification only")] |
| 36 | + public async Task ItCanUseKernelInvokeStreamingAsyncAsync() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var kernel = this.CreateAndInitializeKernel(); |
| 40 | + |
| 41 | + var plugins = TestHelpers.ImportSamplePlugins(kernel, "ChatPlugin"); |
| 42 | + |
| 43 | + StringBuilder fullResult = new(); |
| 44 | + |
| 45 | + var prompt = "Where is the most famous fish market in Seattle, Washington, USA?"; |
| 46 | + |
| 47 | + // Act |
| 48 | + await foreach (var content in kernel.InvokeStreamingAsync<StreamingKernelContent>(plugins["ChatPlugin"]["Chat"], new() { ["input"] = prompt })) |
| 49 | + { |
| 50 | + fullResult.Append(content); |
| 51 | + } |
| 52 | + |
| 53 | + // Assert |
| 54 | + Assert.Contains("Pike Place", fullResult.ToString(), StringComparison.OrdinalIgnoreCase); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact(Skip = "For manual verification only")] |
| 58 | + public async Task ItCanUseServiceGetStreamingChatMessageContentsAsync() |
| 59 | + { |
| 60 | + using var chat = CreateService(); |
| 61 | + |
| 62 | + ChatHistory history = []; |
| 63 | + history.AddUserMessage("Where is the most famous fish market in Seattle, Washington, USA?"); |
| 64 | + |
| 65 | + StringBuilder fullResult = new(); |
| 66 | + |
| 67 | + await foreach (var content in chat.GetStreamingChatMessageContentsAsync(history)) |
| 68 | + { |
| 69 | + fullResult.Append(content); |
| 70 | + } |
| 71 | + |
| 72 | + // Assert |
| 73 | + Assert.Contains("Pike Place", fullResult.ToString(), StringComparison.OrdinalIgnoreCase); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact(Skip = "For manual verification only")] |
| 77 | + public async Task ItCanUseServiceGetChatMessageContentsAsync() |
| 78 | + { |
| 79 | + using var chat = CreateService(); |
| 80 | + |
| 81 | + ChatHistory history = []; |
| 82 | + history.AddUserMessage("Where is the most famous fish market in Seattle, Washington, USA?"); |
| 83 | + |
| 84 | + var content = await chat.GetChatMessageContentAsync(history); |
| 85 | + |
| 86 | + Assert.Contains("Pike Place", content.ToString(), StringComparison.OrdinalIgnoreCase); |
| 87 | + } |
| 88 | + |
| 89 | + private static OnnxRuntimeGenAIChatCompletionService CreateService() |
| 90 | + { |
| 91 | + Assert.NotNull(Configuration.ModelPath); |
| 92 | + Assert.NotNull(Configuration.ModelId); |
| 93 | + |
| 94 | + return new OnnxRuntimeGenAIChatCompletionService(Configuration.ModelId, Configuration.ModelPath); |
| 95 | + } |
| 96 | + |
| 97 | + #region internals |
| 98 | + |
| 99 | + private Kernel CreateAndInitializeKernel(HttpClient? httpClient = null) |
| 100 | + { |
| 101 | + Assert.NotNull(Configuration.ModelPath); |
| 102 | + Assert.NotNull(Configuration.ModelId); |
| 103 | + |
| 104 | + var kernelBuilder = base.CreateKernelBuilder(); |
| 105 | + |
| 106 | + kernelBuilder.AddOnnxRuntimeGenAIChatCompletion( |
| 107 | + modelId: Configuration.ModelId, |
| 108 | + modelPath: Configuration.ModelPath, |
| 109 | + serviceId: Configuration.ServiceId); |
| 110 | + |
| 111 | + return kernelBuilder.Build(); |
| 112 | + } |
| 113 | + |
| 114 | + private static OnnxConfiguration Configuration => new ConfigurationBuilder() |
| 115 | + .AddJsonFile(path: "testsettings.json", optional: true, reloadOnChange: true) |
| 116 | + .AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true) |
| 117 | + .AddEnvironmentVariables() |
| 118 | + .AddUserSecrets<OnnxRuntimeGenAIChatCompletionServiceTests>() |
| 119 | + .Build() |
| 120 | + .GetRequiredSection("Onnx") |
| 121 | + .Get<OnnxConfiguration>()!; |
| 122 | + |
| 123 | + #endregion |
| 124 | +} |
0 commit comments