Skip to content
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

.Net: Onnx Bump 0.5.0 + Add missing integration tests #9639

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
<PackageVersion Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.66.1" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.19.2" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime" Version="1.20.0" />
<PackageVersion Include="FastBertTokenizer" Version="1.0.28" />
<PackageVersion Include="PdfPig" Version="0.1.9" />
<PackageVersion Include="Pinecone.NET" Version="2.1.1" />
Expand Down Expand Up @@ -150,8 +150,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!-- OnnxRuntimeGenAI -->
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.4.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.4.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.4.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.5.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.5.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.5.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Onnx;
using Microsoft.SemanticKernel.Embeddings;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Onnx;
using SemanticKernel.IntegrationTests.TestSettings;
using Xunit;

namespace SemanticKernel.IntegrationTests.Connectors.Onnx;

public class OnnxRuntimeGenAIChatCompletionServiceTests : BaseIntegrationTest
{
[Fact(Skip = "For manual verification only")]
public async Task ItCanUseKernelInvokeAsyncAsync()
{
// Arrange
var kernel = this.CreateAndInitializeKernel();

var func = kernel.CreateFunctionFromPrompt("List the two planets after '{{$input}}', excluding moons, using bullet points.");

// Act
var result = await func.InvokeAsync(kernel, new() { ["input"] = "Jupiter" });

// Assert
Assert.NotNull(result);
Assert.Contains("Saturn", result.GetValue<string>(), StringComparison.InvariantCultureIgnoreCase);
Assert.Contains("Uranus", result.GetValue<string>(), StringComparison.InvariantCultureIgnoreCase);
}

[Fact(Skip = "For manual verification only")]
public async Task ItCanUseKernelInvokeStreamingAsyncAsync()
{
// Arrange
var kernel = this.CreateAndInitializeKernel();

var plugins = TestHelpers.ImportSamplePlugins(kernel, "ChatPlugin");

StringBuilder fullResult = new();

var prompt = "Where is the most famous fish market in Seattle, Washington, USA?";

// Act
await foreach (var content in kernel.InvokeStreamingAsync<StreamingKernelContent>(plugins["ChatPlugin"]["Chat"], new() { ["input"] = prompt }))
{
fullResult.Append(content);
}

// Assert
Assert.Contains("Pike Place", fullResult.ToString(), StringComparison.OrdinalIgnoreCase);
}

[Fact(Skip = "For manual verification only")]
public async Task ItCanUseServiceGetStreamingChatMessageContentsAsync()
{
using var chat = CreateService();

ChatHistory history = [];
history.AddUserMessage("Where is the most famous fish market in Seattle, Washington, USA?");

StringBuilder fullResult = new();

await foreach (var content in chat.GetStreamingChatMessageContentsAsync(history))
{
fullResult.Append(content);
}

// Assert
Assert.Contains("Pike Place", fullResult.ToString(), StringComparison.OrdinalIgnoreCase);
}

[Fact(Skip = "For manual verification only")]
public async Task ItCanUseServiceGetChatMessageContentsAsync()
{
using var chat = CreateService();

ChatHistory history = [];
history.AddUserMessage("Where is the most famous fish market in Seattle, Washington, USA?");

var content = await chat.GetChatMessageContentAsync(history);

Assert.Contains("Pike Place", content.ToString(), StringComparison.OrdinalIgnoreCase);
}

private static OnnxRuntimeGenAIChatCompletionService CreateService()
{
Assert.NotNull(Configuration.ModelPath);
Assert.NotNull(Configuration.ModelId);

return new OnnxRuntimeGenAIChatCompletionService(Configuration.ModelId, Configuration.ModelPath);
}

#region internals

private Kernel CreateAndInitializeKernel(HttpClient? httpClient = null)
{
Assert.NotNull(Configuration.ModelPath);
Assert.NotNull(Configuration.ModelId);

var kernelBuilder = base.CreateKernelBuilder();

kernelBuilder.AddOnnxRuntimeGenAIChatCompletion(
modelId: Configuration.ModelId,
modelPath: Configuration.ModelPath,
serviceId: Configuration.ServiceId);

return kernelBuilder.Build();
}

private static OnnxConfiguration Configuration => new ConfigurationBuilder()
.AddJsonFile(path: "testsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddUserSecrets<OnnxRuntimeGenAIChatCompletionServiceTests>()
.Build()
.GetRequiredSection("Onnx")
.Get<OnnxConfiguration>()!;

#endregion
}
14 changes: 14 additions & 0 deletions dotnet/src/IntegrationTests/TestSettings/OnnxConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Diagnostics.CodeAnalysis;

namespace SemanticKernel.IntegrationTests.TestSettings;

[SuppressMessage("Performance", "CA1812:Internal class that is apparently never instantiated",
Justification = "Configuration classes are instantiated through IConfiguration.")]
internal sealed class OnnxConfiguration
{
public string? ModelId { get; set; }
public string? ModelPath { get; set; }
public string? ServiceId { get; internal set; }
}
Loading