Skip to content

Commit

Permalink
Add OpenAI serialization helper methods. (#5697)
Browse files Browse the repository at this point in the history
* Add a System.Net.ServerSentEvents polyfill.

* Move model mapping helpers to standalone class.

* Skeleton implementation of OpenAI serialization methods.

* Add unit testing.

* Also normalize escaped line endings.

* Fix unix test failures

* Exclude System.Net.Sse from code coverage

* Improve test coverage.

* Remove msbuild artifact

* Fix merge conflicts.

* Address feedback.

* Address feedback.

* Address feedback.

* Add function result name inference.
  • Loading branch information
eiriktsarpalis authored Dec 10, 2024
1 parent d5fca58 commit 8bc5f92
Show file tree
Hide file tree
Showing 21 changed files with 3,283 additions and 588 deletions.
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
31 changes: 31 additions & 0 deletions src/Libraries/Microsoft.Extensions.AI.OpenAI/JsonModelHelpers.cs
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

0 comments on commit 8bc5f92

Please sign in to comment.