-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAI serialization helper methods. (#5697)
* 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
1 parent
d5fca58
commit 8bc5f92
Showing
21 changed files
with
3,283 additions
and
588 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
31 changes: 31 additions & 0 deletions
31
src/Libraries/Microsoft.Extensions.AI.OpenAI/JsonModelHelpers.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,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(); | ||
} | ||
} |
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.