forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Add GenAI support to Connectors.Onnx (microsoft#6518)
@stephentoub invite me to Contribute https://github.com/feiyun0112/SemanticKernel.Connectors.OnnxRuntimeGenAI to Microsoft.SemanticKernel.Connectors.Onnx [https://github.com/feiyun0112/SemanticKernel.Connectors.OnnxRuntimeGenAI/issues/4](https://github.com/feiyun0112/SemanticKernel.Connectors.OnnxRuntimeGenAI/issues/4) --------- Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com> Co-authored-by: Stephen Toub <stoub@microsoft.com> Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
- Loading branch information
1 parent
f703783
commit ffeb099
Showing
8 changed files
with
517 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
49 changes: 49 additions & 0 deletions
49
dotnet/src/Connectors/Connectors.Onnx.UnitTests/OnnxExtensionsTests.cs
This file contains 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,49 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.Connectors.Onnx; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Connectors.Onnx.UnitTests; | ||
|
||
/// <summary> | ||
/// Unit tests for <see cref="OnnxKernelBuilderExtensions"/>. | ||
/// </summary> | ||
public class OnnxExtensionsTests | ||
{ | ||
[Fact] | ||
public void AddOnnxRuntimeGenAIChatCompletionToServiceCollection() | ||
{ | ||
// Arrange | ||
var collection = new ServiceCollection(); | ||
collection.AddOnnxRuntimeGenAIChatCompletion("modelId", "modelPath"); | ||
|
||
// Act | ||
var kernelBuilder = collection.AddKernel(); | ||
var kernel = collection.BuildServiceProvider().GetRequiredService<Kernel>(); | ||
var service = kernel.GetRequiredService<IChatCompletionService>(); | ||
|
||
// Assert | ||
Assert.NotNull(service); | ||
Assert.IsType<OnnxRuntimeGenAIChatCompletionService>(service); | ||
} | ||
|
||
[Fact] | ||
public void AddOnnxRuntimeGenAIChatCompletionToKernelBuilder() | ||
{ | ||
// Arrange | ||
var collection = new ServiceCollection(); | ||
var kernelBuilder = collection.AddKernel(); | ||
kernelBuilder.AddOnnxRuntimeGenAIChatCompletion("modelId", "modelPath"); | ||
|
||
// Act | ||
var kernel = collection.BuildServiceProvider().GetRequiredService<Kernel>(); | ||
var service = kernel.GetRequiredService<IChatCompletionService>(); | ||
|
||
// Assert | ||
Assert.NotNull(service); | ||
Assert.IsType<OnnxRuntimeGenAIChatCompletionService>(service); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
.../src/Connectors/Connectors.Onnx.UnitTests/OnnxRuntimeGenAIPromptExecutionSettingsTests.cs
This file contains 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,97 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text.Json; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.Onnx; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Connectors.Onnx.UnitTests; | ||
|
||
/// <summary> | ||
/// Unit tests for <see cref="OnnxRuntimeGenAIPromptExecutionSettings"/>. | ||
/// </summary> | ||
public class OnnxRuntimeGenAIPromptExecutionSettingsTests | ||
{ | ||
[Fact] | ||
public void FromExecutionSettingsWhenAlreadyMistralShouldReturnSame() | ||
{ | ||
// Arrange | ||
var executionSettings = new OnnxRuntimeGenAIPromptExecutionSettings(); | ||
|
||
// Act | ||
var onnxExecutionSettings = OnnxRuntimeGenAIPromptExecutionSettings.FromExecutionSettings(executionSettings); | ||
|
||
// Assert | ||
Assert.Same(executionSettings, onnxExecutionSettings); | ||
} | ||
|
||
[Fact] | ||
public void FromExecutionSettingsWhenNullShouldReturnDefaultSettings() | ||
{ | ||
// Arrange | ||
PromptExecutionSettings? executionSettings = null; | ||
|
||
// Act | ||
var onnxExecutionSettings = OnnxRuntimeGenAIPromptExecutionSettings.FromExecutionSettings(executionSettings); | ||
|
||
// Assert | ||
Assert.Null(onnxExecutionSettings.TopK); | ||
Assert.Null(onnxExecutionSettings.TopP); | ||
Assert.Null(onnxExecutionSettings.Temperature); | ||
Assert.Null(onnxExecutionSettings.RepetitionPenalty); | ||
Assert.Null(onnxExecutionSettings.PastPresentShareBuffer); | ||
Assert.Null(onnxExecutionSettings.NumReturnSequences); | ||
Assert.Null(onnxExecutionSettings.NumBeams); | ||
Assert.Null(onnxExecutionSettings.NoRepeatNgramSize); | ||
Assert.Null(onnxExecutionSettings.MinTokens); | ||
Assert.Null(onnxExecutionSettings.MaxTokens); | ||
Assert.Null(onnxExecutionSettings.LengthPenalty); | ||
Assert.Null(onnxExecutionSettings.DiversityPenalty); | ||
Assert.Null(onnxExecutionSettings.EarlyStopping); | ||
Assert.Null(onnxExecutionSettings.DoSample); | ||
} | ||
|
||
[Fact] | ||
public void FromExecutionSettingsWhenSerializedHasPropertiesShouldPopulateSpecialized() | ||
{ | ||
// Arrange | ||
string jsonSettings = """ | ||
{ | ||
"top_k": 2, | ||
"top_p": 0.9, | ||
"temperature": 0.5, | ||
"repetition_penalty": 0.1, | ||
"past_present_share_buffer": true, | ||
"num_return_sequences": 200, | ||
"num_beams": 20, | ||
"no_repeat_ngram_size": 15, | ||
"min_tokens": 10, | ||
"max_tokens": 100, | ||
"length_penalty": 0.2, | ||
"diversity_penalty": 0.3, | ||
"early_stopping": false, | ||
"do_sample": true | ||
} | ||
"""; | ||
|
||
// Act | ||
var executionSettings = JsonSerializer.Deserialize<PromptExecutionSettings>(jsonSettings); | ||
var onnxExecutionSettings = OnnxRuntimeGenAIPromptExecutionSettings.FromExecutionSettings(executionSettings); | ||
|
||
// Assert | ||
Assert.Equal(2, onnxExecutionSettings.TopK); | ||
Assert.Equal(0.9f, onnxExecutionSettings.TopP); | ||
Assert.Equal(0.5f, onnxExecutionSettings.Temperature); | ||
Assert.Equal(0.1f, onnxExecutionSettings.RepetitionPenalty); | ||
Assert.True(onnxExecutionSettings.PastPresentShareBuffer); | ||
Assert.Equal(200, onnxExecutionSettings.NumReturnSequences); | ||
Assert.Equal(20, onnxExecutionSettings.NumBeams); | ||
Assert.Equal(15, onnxExecutionSettings.NoRepeatNgramSize); | ||
Assert.Equal(10, onnxExecutionSettings.MinTokens); | ||
Assert.Equal(100, onnxExecutionSettings.MaxTokens); | ||
Assert.Equal(0.2f, onnxExecutionSettings.LengthPenalty); | ||
Assert.Equal(0.3f, onnxExecutionSettings.DiversityPenalty); | ||
Assert.False(onnxExecutionSettings.EarlyStopping); | ||
Assert.True(onnxExecutionSettings.DoSample); | ||
} | ||
} |
This file contains 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 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.