-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable bidirectional adapters between SK and Microsoft.Extensions.AI …
…interfaces
- Loading branch information
1 parent
ea5ceb1
commit 11fbef3
Showing
8 changed files
with
910 additions
and
3 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
618 changes: 617 additions & 1 deletion
618
dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionServiceExtensions.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
62 changes: 62 additions & 0 deletions
62
dotnet/src/SemanticKernel.Abstractions/AbstractionsJsonContext.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,62 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
|
||
namespace Microsoft.SemanticKernel; | ||
|
||
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web, | ||
UseStringEnumConverter = true, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
WriteIndented = true)] | ||
[JsonSerializable(typeof(IDictionary<string, object?>))] | ||
[JsonSerializable(typeof(JsonElement))] | ||
[JsonSerializable(typeof(PromptExecutionSettings))] | ||
internal sealed partial class AbstractionsJsonContext : JsonSerializerContext | ||
{ | ||
/// <summary>Gets the <see cref="JsonSerializerOptions"/> singleton used as the default in JSON serialization operations.</summary> | ||
private static readonly JsonSerializerOptions s_defaultToolJsonOptions = CreateDefaultToolJsonOptions(); | ||
|
||
/// <summary>Gets JSON type information for the specified type.</summary> | ||
/// <remarks> | ||
/// This first tries to get the type information from <paramref name="firstOptions"/>, | ||
/// falling back to <see cref="s_defaultToolJsonOptions"/> if it can't. | ||
/// </remarks> | ||
public static JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions? firstOptions) | ||
{ | ||
return firstOptions?.TryGetTypeInfo(type, out JsonTypeInfo? info) is true ? | ||
info : | ||
s_defaultToolJsonOptions.GetTypeInfo(type); | ||
} | ||
|
||
/// <summary>Creates the default <see cref="JsonSerializerOptions"/> to use for serialization-related operations.</summary> | ||
[UnconditionalSuppressMessage("AotAnalysis", "IL3050", Justification = "DefaultJsonTypeInfoResolver is only used when reflection-based serialization is enabled")] | ||
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "DefaultJsonTypeInfoResolver is only used when reflection-based serialization is enabled")] | ||
private static JsonSerializerOptions CreateDefaultToolJsonOptions() | ||
{ | ||
// If reflection-based serialization is enabled by default, use it, as it's the most permissive in terms of what it can serialize, | ||
// and we want to be flexible in terms of what can be put into the various collections in the object model. | ||
// Otherwise, use the source-generated options to enable trimming and Native AOT. | ||
|
||
if (JsonSerializer.IsReflectionEnabledByDefault) | ||
{ | ||
// Keep in sync with the JsonSourceGenerationOptions attribute on JsonContext above. | ||
JsonSerializerOptions options = new(JsonSerializerDefaults.Web) | ||
{ | ||
TypeInfoResolver = new DefaultJsonTypeInfoResolver(), | ||
Converters = { new JsonStringEnumConverter() }, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
WriteIndented = true, | ||
}; | ||
|
||
options.MakeReadOnly(); | ||
return options; | ||
} | ||
|
||
return Default.Options; | ||
} | ||
} |
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
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
22 changes: 22 additions & 0 deletions
22
dotnet/src/SemanticKernel.UnitTests/AI/ServiceConversionExtensionsTests.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,22 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.Embeddings; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.UnitTests.AI; | ||
|
||
public class ServiceConversionExtensionsTests | ||
{ | ||
[Fact] | ||
public void InvalidArgumentsThrow() | ||
{ | ||
Assert.Throws<ArgumentNullException>("service", () => ChatCompletionServiceExtensions.AsChatClient(null!)); | ||
Assert.Throws<ArgumentNullException>("client", () => ChatCompletionServiceExtensions.AsChatCompletionService(null!)); | ||
|
||
Assert.Throws<ArgumentNullException>("service", () => EmbeddingGenerationExtensions.AsEmbeddingGenerator<string, float>(null!)); | ||
Assert.Throws<ArgumentNullException>("generator", () => EmbeddingGenerationExtensions.AsEmbeddingGenerationService<string, float>(null!)); | ||
} | ||
} |