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

Add OpenAI serialization helper methods. #5697

Merged
merged 14 commits into from
Dec 10, 2024
Merged
4 changes: 4 additions & 0 deletions eng/MSBuild/Shared.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<ItemGroup Condition="'$(InjectSharedRentedSpan)' == 'true'">
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\RentedSpan\*.cs" LinkBase="Shared\RentedSpan" />
</ItemGroup>

<ItemGroup Condition="'$(InjectSharedServerSentEvents)' == 'true'">
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\ServerSentEvents\*.cs" LinkBase="Shared\ServerSentEvents" />
</ItemGroup>

<ItemGroup Condition="'$(InjectSharedNumericExtensions)' == 'true'">
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\NumericExtensions\*.cs" LinkBase="Shared\NumericExtensions" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.ClientModel.Primitives;

namespace Microsoft.Extensions.AI;

/// <summary>
/// Defines a set of helper methods for working with <see cref="IJsonModel{T}"/> types.
/// </summary>
internal static class JsonModelHelpers
{
public static BinaryData Serialize<TModel>(TModel value)
where TModel : IJsonModel<TModel>
{
return value.Write(ModelReaderWriterOptions.Json);
}

public static TModel Deserialize<TModel>(BinaryData data)
where TModel : IJsonModel<TModel>, new()
{
return JsonModelDeserializationWitness<TModel>.Value.Create(data, ModelReaderWriterOptions.Json);
}

private sealed class JsonModelDeserializationWitness<TModel>
where TModel : IJsonModel<TModel>, new()
{
public static readonly IJsonModel<TModel> Value = new TModel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
<NoWarn>$(NoWarn);CA1063;CA1508;CA2227;SA1316;S1121;S3358;EA0002;OPENAI002</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DisableNETStandardCompatErrors>true</DisableNETStandardCompatErrors>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup>
<InjectSharedEmptyCollections>true</InjectSharedEmptyCollections>
<InjectStringHashOnLegacy>true</InjectStringHashOnLegacy>
<InjectSharedServerSentEvents>true</InjectSharedServerSentEvents>
<InjectRequiredMemberOnLegacy>true</InjectRequiredMemberOnLegacy>
<InjectCompilerFeatureRequiredOnLegacy>true</InjectCompilerFeatureRequiredOnLegacy>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading