Skip to content

Commit 7d9f834

Browse files
authored
.Net: Onnx Bump 0.5.0 + Add missing integration tests (#9639)
### Motivation and Context - Add missing integration tests - Resolves #9628
1 parent f7dc526 commit 7d9f834

File tree

4 files changed

+144
-4
lines changed

4 files changed

+144
-4
lines changed

dotnet/Directory.Packages.props

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
4040
<PackageVersion Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
4141
<PackageVersion Include="Microsoft.Identity.Client" Version="4.66.1" />
42-
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.19.2" />
42+
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.20.0" />
4343
<PackageVersion Include="FastBertTokenizer" Version="1.0.28" />
4444
<PackageVersion Include="PdfPig" Version="0.1.9" />
4545
<PackageVersion Include="Pinecone.NET" Version="2.1.1" />
@@ -150,8 +150,8 @@
150150
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
151151
</PackageReference>
152152
<!-- OnnxRuntimeGenAI -->
153-
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.4.0" />
154-
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.4.0" />
155-
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.4.0" />
153+
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.5.0" />
154+
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.5.0" />
155+
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.5.0" />
156156
</ItemGroup>
157157
</Project>

dotnet/src/IntegrationTests/Connectors/Onnx/BertOnnxTextEmbeddingGenerationServiceTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
using System.Security.Cryptography;
1010
using System.Text;
1111
using System.Threading.Tasks;
12+
using Microsoft.Extensions.DependencyInjection;
1213
using Microsoft.SemanticKernel;
14+
using Microsoft.SemanticKernel.ChatCompletion;
1315
using Microsoft.SemanticKernel.Connectors.Onnx;
1416
using Microsoft.SemanticKernel.Embeddings;
1517
using Xunit;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
namespace SemanticKernel.IntegrationTests.TestSettings;
6+
7+
[SuppressMessage("Performance", "CA1812:Internal class that is apparently never instantiated",
8+
Justification = "Configuration classes are instantiated through IConfiguration.")]
9+
internal sealed class OnnxConfiguration
10+
{
11+
public string? ModelId { get; set; }
12+
public string? ModelPath { get; set; }
13+
public string? ServiceId { get; internal set; }
14+
}

0 commit comments

Comments
 (0)