Skip to content

Commit

Permalink
.Net: Remove JsonSchema.Net dependency from Microsoft.SemanticKernel.…
Browse files Browse the repository at this point in the history
…Abstractions/Core (#5635)

This removes the dependency, substituting in a lighter-weight schema
generator that better aligns with System.Text.Json and which is likely
to be built-in to STJ in a future release. All of these get removed:
<img width="520" alt="image"
src="https://github.com/microsoft/semantic-kernel/assets/2642209/d14005f8-f7a7-418f-ada4-a5faecb4d7ff">

The JsonSchema.Net dependency still exists from
Microsoft.SemanticKernel.Plugins.OpenApi in support of the validation
used in
https://github.com/microsoft/semantic-kernel/blob/9ab95132b5f460f1bf9a1d1e387fb18453a037f4/dotnet/src/Functions/Functions.OpenApi/Extensions/RestApiOperationResponseExtensions.cs#L48-L51
and we can subsequently decide what to do about that.

Note that the replacement highlighted some important issues with the
schema previously being generated. In particular, it didn't align with
how state would actually be serialized/deserialized, e.g. the schema
included public fields even though the JsonSerializerOptions being used
would ignore such fields.

This does incur one notable reduction in functionality: today
KernelJsonSchema validates that supplied text is indeed a valid JSON
schema... with this change, it only validates that it's valid JSON. I
think that's a reasonable tradeoff for now, and we should be able to add
back the stricter validation in the future when STJ provides support for
it. I don't have a good alternative right now other than keeping the
significant dependency.
  • Loading branch information
stephentoub committed Apr 8, 2024
1 parent 9b8a218 commit 0a9e74a
Show file tree
Hide file tree
Showing 22 changed files with 2,331 additions and 54 deletions.
2 changes: 1 addition & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageVersion Include="Handlebars.Net.Helpers" Version="2.4.1.4" />
<PackageVersion Include="Markdig" Version="0.36.2" />
<PackageVersion Include="Handlebars.Net" Version="2.1.5" />
<PackageVersion Include="JsonSchema.Net.Generation" Version="3.5.1" />
<PackageVersion Include="JsonSchema.Net" Version="5.4.2" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageVersion Include="Microsoft.Azure.Kusto.Data" Version="11.3.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using Json.Schema;
using Json.Schema.Generation;
using Microsoft.SemanticKernel.Connectors.Google.Core;

namespace Microsoft.SemanticKernel.Connectors.Google;
Expand Down Expand Up @@ -174,12 +172,7 @@ private static KernelJsonSchema GetDefaultSchemaForParameter(GeminiFunctionParam
// If there's a description, incorporate it.
if (!string.IsNullOrWhiteSpace(parameter.Description))
{
return KernelJsonSchema.Parse(
JsonSerializer.Serialize(
new JsonSchemaBuilder()
.FromType(parameter.ParameterType ?? typeof(string))
.Description(parameter.Description)
.Build()));
return KernelJsonSchemaBuilder.Build(null, typeof(string), parameter.Description);
}

// Otherwise, we can use a cached schema for a string with no description.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

using System;
using System.Collections.Generic;
using System.Text.Json;
using Azure.AI.OpenAI;
using Json.Schema;
using Json.Schema.Generation;

namespace Microsoft.SemanticKernel.Connectors.OpenAI;

Expand Down Expand Up @@ -176,11 +173,7 @@ private static KernelJsonSchema GetDefaultSchemaForTypelessParameter(string? des
// If there's a description, incorporate it.
if (!string.IsNullOrWhiteSpace(description))
{
return KernelJsonSchema.Parse(JsonSerializer.Serialize(
new JsonSchemaBuilder()
.FromType(typeof(string))
.Description(description!)
.Build()));
return KernelJsonSchemaBuilder.Build(null, typeof(string), description);
}

// Otherwise, we can use a cached schema for a string with no description.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Json.More;
using Microsoft.SemanticKernel.Experimental.Agents.Models;

namespace Microsoft.SemanticKernel.Experimental.Agents;
Expand Down Expand Up @@ -96,4 +95,17 @@ private static string ConvertType(Type? type)

return "object";
}

private static bool IsNumber(this Type type) =>
type == typeof(byte) ||
type == typeof(sbyte) ||
type == typeof(short) ||
type == typeof(ushort) ||
type == typeof(int) ||
type == typeof(uint) ||
type == typeof(long) ||
type == typeof(ulong) ||
type == typeof(float) ||
type == typeof(double) ||
type == typeof(decimal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" />
<PackageReference Include="Microsoft.OpenApi" />
<PackageReference Include="Microsoft.OpenApi.Readers" />
<PackageReference Include="JsonSchema.Net" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Json.Schema;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel.Memory;
Expand Down
9 changes: 9 additions & 0 deletions dotnet/src/InternalUtilities/src/Schema/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Suppressing code analysis diagnostics for code included as a source copy
[*.cs]
dotnet_diagnostic.CA1852.severity = none
dotnet_diagnostic.IDE0005.severity = none
dotnet_diagnostic.IDE0009.severity = none
dotnet_diagnostic.IDE0055.severity = none
dotnet_diagnostic.IDE0161.severity = none
dotnet_diagnostic.IDE1006.severity = none
dotnet_diagnostic.RCS1211.severity = none
Loading

0 comments on commit 0a9e74a

Please sign in to comment.