//
#nullable disable
using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using OpenAI;
namespace OpenAI.Chat
{
public partial class ChatCompletionCollectionOptions : IJsonModel
{
protected virtual ChatCompletionCollectionOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
{
return DeserializeChatCompletionCollectionOptions(document.RootElement, data, options);
}
default:
throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support reading '{options.Format}' format.");
}
}
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
switch (format)
{
case "J":
return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
default:
throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support writing '{options.Format}' format.");
}
}
BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
ChatCompletionCollectionOptions IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);
string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
if (Patch.Contains("$"u8))
{
writer.WriteRawValue(Patch.GetJson("$"u8));
return;
}
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
writer.WriteStartObject();
JsonModelWriteCore(writer, options);
writer.WriteEndObject();
}
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support writing '{format}' format.");
}
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
Patch.WriteTo(writer);
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
}
ChatCompletionCollectionOptions IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);
protected virtual ChatCompletionCollectionOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
{
string format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format;
if (format != "J")
{
throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support reading '{format}' format.");
}
using JsonDocument document = JsonDocument.ParseValue(ref reader);
return DeserializeChatCompletionCollectionOptions(document.RootElement, null, options);
}
internal static ChatCompletionCollectionOptions DeserializeChatCompletionCollectionOptions(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
string afterId = default;
int? pageSizeLimit = default;
ChatCompletionCollectionOrder? order = default;
IDictionary metadata = default;
string model = default;
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory.Empty : data.ToMemory());
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
foreach (var prop in element.EnumerateObject())
{
patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
}
return new ChatCompletionCollectionOptions(
afterId,
pageSizeLimit,
order,
metadata ?? new ChangeTrackingDictionary(),
model,
patch);
}
}
}