openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Embeddings/OpenAIEmbeddingCollection.Serialization.cs
98lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text; |
| 5 | using System.Text.Json; |
| 6 | |
| 7 | namespace OpenAI.Embeddings; |
| 8 | |
| 9 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Embeddings.OpenAIEmbeddingCollection>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 10 | public partial class OpenAIEmbeddingCollection : IJsonModel<OpenAIEmbeddingCollection> |
| 11 | { |
| 12 | // CUSTOM: |
| 13 | // - Serialized the Items property. |
| 14 | // - Recovered the deserialization of SerializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
| 15 | void IJsonModel<OpenAIEmbeddingCollection>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 16 | => CustomSerializationHelpers.SerializeInstance(this, SerializeOpenAIEmbeddingCollection, writer, options); |
| 17 | |
| 18 | internal static void SerializeOpenAIEmbeddingCollection(OpenAIEmbeddingCollection instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 19 | { |
| 20 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 21 | if (instance.Patch.Contains("$"u8)) |
| 22 | { |
| 23 | writer.WriteRawValue(instance.Patch.GetJson("$"u8)); |
| 24 | return; |
| 25 | } |
| 26 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 27 | |
| 28 | writer.WriteStartObject(); |
| 29 | writer.WritePropertyName("data"u8); |
| 30 | writer.WriteStartArray(); |
| 31 | foreach (var item in instance.Items) |
| 32 | { |
| 33 | writer.WriteObjectValue<OpenAIEmbedding>(item, options); |
| 34 | } |
| 35 | writer.WriteEndArray(); |
| 36 | writer.WritePropertyName("model"u8); |
| 37 | writer.WriteStringValue(instance.Model); |
| 38 | writer.WritePropertyName("object"u8); |
| 39 | writer.WriteStringValue(instance.Object.ToString()); |
| 40 | writer.WritePropertyName("usage"u8); |
| 41 | writer.WriteObjectValue<EmbeddingTokenUsage>(instance.Usage, options); |
| 42 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 43 | instance.Patch.WriteTo(writer); |
| 44 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 45 | writer.WriteEndObject(); |
| 46 | } |
| 47 | |
| 48 | // CUSTOM: Recovered the deserialization of SerializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
| 49 | internal static OpenAIEmbeddingCollection DeserializeOpenAIEmbeddingCollection(JsonElement element, BinaryData data, ModelReaderWriterOptions options = null) |
| 50 | { |
| 51 | options ??= new ModelReaderWriterOptions("W"); |
| 52 | |
| 53 | if (element.ValueKind == JsonValueKind.Null) |
| 54 | { |
| 55 | return null; |
| 56 | } |
| 57 | IReadOnlyList<OpenAIEmbedding> embedding = default; |
| 58 | string model = default; |
| 59 | string @object = default; |
| 60 | EmbeddingTokenUsage usage = default; |
| 61 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 62 | JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 63 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 64 | foreach (var property in element.EnumerateObject()) |
| 65 | { |
| 66 | if (property.NameEquals("data"u8)) |
| 67 | { |
| 68 | List<OpenAIEmbedding> array = new List<OpenAIEmbedding>(); |
| 69 | foreach (var item in property.Value.EnumerateArray()) |
| 70 | { |
| 71 | array.Add(OpenAIEmbedding.DeserializeOpenAIEmbedding(item, item.GetUtf8Bytes(), options)); |
| 72 | } |
| 73 | embedding = array; |
| 74 | continue; |
| 75 | } |
| 76 | if (property.NameEquals("model"u8)) |
| 77 | { |
| 78 | model = property.Value.GetString(); |
| 79 | continue; |
| 80 | } |
| 81 | if (property.NameEquals("object"u8)) |
| 82 | { |
| 83 | @object = property.Value.GetString(); |
| 84 | continue; |
| 85 | } |
| 86 | if (property.NameEquals("usage"u8)) |
| 87 | { |
| 88 | usage = EmbeddingTokenUsage.DeserializeEmbeddingTokenUsage(property.Value, property.Value.GetUtf8Bytes(), options); |
| 89 | continue; |
| 90 | } |
| 91 | if (true) |
| 92 | { |
| 93 | patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(property.Name)], property.Value.GetUtf8Bytes()); |
| 94 | } |
| 95 | } |
| 96 | return new OpenAIEmbeddingCollection(embedding, model, @object, usage, patch); |
| 97 | } |
| 98 | } |
| 99 | |