openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Assistants/AssistantModificationOptions.Serialization.cs
327lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel.Primitives; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Text.Json; |
| 9 | using OpenAI; |
| 10 | using OpenAI.Chat; |
| 11 | |
| 12 | namespace OpenAI.Assistants |
| 13 | { |
| 14 | public partial class AssistantModificationOptions : IJsonModel<AssistantModificationOptions> |
| 15 | { |
| 16 | void IJsonModel<AssistantModificationOptions>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 17 | { |
| 18 | writer.WriteStartObject(); |
| 19 | JsonModelWriteCore(writer, options); |
| 20 | writer.WriteEndObject(); |
| 21 | } |
| 22 | |
| 23 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 24 | { |
| 25 | string format = options.Format == "W" ? ((IPersistableModel<AssistantModificationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 26 | if (format != "J") |
| 27 | { |
| 28 | throw new FormatException($"The model {nameof(AssistantModificationOptions)} does not support writing '{format}' format."); |
| 29 | } |
| 30 | if (Optional.IsDefined(Name) && _additionalBinaryDataProperties?.ContainsKey("name") != true) |
| 31 | { |
| 32 | writer.WritePropertyName("name"u8); |
| 33 | writer.WriteStringValue(Name); |
| 34 | } |
| 35 | if (Optional.IsDefined(Description) && _additionalBinaryDataProperties?.ContainsKey("description") != true) |
| 36 | { |
| 37 | writer.WritePropertyName("description"u8); |
| 38 | writer.WriteStringValue(Description); |
| 39 | } |
| 40 | if (Optional.IsDefined(Instructions) && _additionalBinaryDataProperties?.ContainsKey("instructions") != true) |
| 41 | { |
| 42 | writer.WritePropertyName("instructions"u8); |
| 43 | writer.WriteStringValue(Instructions); |
| 44 | } |
| 45 | if (Optional.IsCollectionDefined(Metadata) && _additionalBinaryDataProperties?.ContainsKey("metadata") != true) |
| 46 | { |
| 47 | writer.WritePropertyName("metadata"u8); |
| 48 | writer.WriteStartObject(); |
| 49 | foreach (var item in Metadata) |
| 50 | { |
| 51 | writer.WritePropertyName(item.Key); |
| 52 | if (item.Value == null) |
| 53 | { |
| 54 | writer.WriteNullValue(); |
| 55 | continue; |
| 56 | } |
| 57 | writer.WriteStringValue(item.Value); |
| 58 | } |
| 59 | writer.WriteEndObject(); |
| 60 | } |
| 61 | if (Optional.IsDefined(Temperature) && _additionalBinaryDataProperties?.ContainsKey("temperature") != true) |
| 62 | { |
| 63 | writer.WritePropertyName("temperature"u8); |
| 64 | writer.WriteNumberValue(Temperature.Value); |
| 65 | } |
| 66 | if (Optional.IsDefined(Model) && _additionalBinaryDataProperties?.ContainsKey("model") != true) |
| 67 | { |
| 68 | writer.WritePropertyName("model"u8); |
| 69 | writer.WriteStringValue(Model); |
| 70 | } |
| 71 | if (Optional.IsCollectionDefined(DefaultTools) && _additionalBinaryDataProperties?.ContainsKey("tools") != true) |
| 72 | { |
| 73 | writer.WritePropertyName("tools"u8); |
| 74 | writer.WriteStartArray(); |
| 75 | foreach (ToolDefinition item in DefaultTools) |
| 76 | { |
| 77 | writer.WriteObjectValue(item, options); |
| 78 | } |
| 79 | writer.WriteEndArray(); |
| 80 | } |
| 81 | if (Optional.IsDefined(ToolResources) && _additionalBinaryDataProperties?.ContainsKey("tool_resources") != true) |
| 82 | { |
| 83 | writer.WritePropertyName("tool_resources"u8); |
| 84 | writer.WriteObjectValue(ToolResources, options); |
| 85 | } |
| 86 | if (Optional.IsDefined(ResponseFormat) && _additionalBinaryDataProperties?.ContainsKey("response_format") != true) |
| 87 | { |
| 88 | writer.WritePropertyName("response_format"u8); |
| 89 | writer.WriteObjectValue(ResponseFormat, options); |
| 90 | } |
| 91 | if (Optional.IsDefined(NucleusSamplingFactor) && _additionalBinaryDataProperties?.ContainsKey("top_p") != true) |
| 92 | { |
| 93 | writer.WritePropertyName("top_p"u8); |
| 94 | writer.WriteNumberValue(NucleusSamplingFactor.Value); |
| 95 | } |
| 96 | if (Optional.IsDefined(ReasoningEffortLevel) && _additionalBinaryDataProperties?.ContainsKey("reasoning_effort") != true) |
| 97 | { |
| 98 | writer.WritePropertyName("reasoning_effort"u8); |
| 99 | writer.WriteStringValue(ReasoningEffortLevel.Value.ToString()); |
| 100 | } |
| 101 | // Plugin customization: remove options.Format != "W" check |
| 102 | if (_additionalBinaryDataProperties != null) |
| 103 | { |
| 104 | foreach (var item in _additionalBinaryDataProperties) |
| 105 | { |
| 106 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 107 | { |
| 108 | continue; |
| 109 | } |
| 110 | writer.WritePropertyName(item.Key); |
| 111 | #if NET6_0_OR_GREATER |
| 112 | writer.WriteRawValue(item.Value); |
| 113 | #else |
| 114 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 115 | { |
| 116 | JsonSerializer.Serialize(writer, document.RootElement); |
| 117 | } |
| 118 | #endif |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | AssistantModificationOptions IJsonModel<AssistantModificationOptions>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 124 | |
| 125 | protected virtual AssistantModificationOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 126 | { |
| 127 | string format = options.Format == "W" ? ((IPersistableModel<AssistantModificationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 128 | if (format != "J") |
| 129 | { |
| 130 | throw new FormatException($"The model {nameof(AssistantModificationOptions)} does not support reading '{format}' format."); |
| 131 | } |
| 132 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 133 | return DeserializeAssistantModificationOptions(document.RootElement, options); |
| 134 | } |
| 135 | |
| 136 | internal static AssistantModificationOptions DeserializeAssistantModificationOptions(JsonElement element, ModelReaderWriterOptions options) |
| 137 | { |
| 138 | if (element.ValueKind == JsonValueKind.Null) |
| 139 | { |
| 140 | return null; |
| 141 | } |
| 142 | string name = default; |
| 143 | string description = default; |
| 144 | string instructions = default; |
| 145 | IDictionary<string, string> metadata = default; |
| 146 | float? temperature = default; |
| 147 | string model = default; |
| 148 | IList<ToolDefinition> defaultTools = default; |
| 149 | ToolResources toolResources = default; |
| 150 | AssistantResponseFormat responseFormat = default; |
| 151 | float? nucleusSamplingFactor = default; |
| 152 | ChatReasoningEffortLevel? reasoningEffortLevel = default; |
| 153 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 154 | foreach (var prop in element.EnumerateObject()) |
| 155 | { |
| 156 | if (prop.NameEquals("name"u8)) |
| 157 | { |
| 158 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 159 | { |
| 160 | name = null; |
| 161 | continue; |
| 162 | } |
| 163 | name = prop.Value.GetString(); |
| 164 | continue; |
| 165 | } |
| 166 | if (prop.NameEquals("description"u8)) |
| 167 | { |
| 168 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 169 | { |
| 170 | description = null; |
| 171 | continue; |
| 172 | } |
| 173 | description = prop.Value.GetString(); |
| 174 | continue; |
| 175 | } |
| 176 | if (prop.NameEquals("instructions"u8)) |
| 177 | { |
| 178 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 179 | { |
| 180 | instructions = null; |
| 181 | continue; |
| 182 | } |
| 183 | instructions = prop.Value.GetString(); |
| 184 | continue; |
| 185 | } |
| 186 | if (prop.NameEquals("metadata"u8)) |
| 187 | { |
| 188 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 189 | { |
| 190 | continue; |
| 191 | } |
| 192 | Dictionary<string, string> dictionary = new Dictionary<string, string>(); |
| 193 | foreach (var prop0 in prop.Value.EnumerateObject()) |
| 194 | { |
| 195 | if (prop0.Value.ValueKind == JsonValueKind.Null) |
| 196 | { |
| 197 | dictionary.Add(prop0.Name, null); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | dictionary.Add(prop0.Name, prop0.Value.GetString()); |
| 202 | } |
| 203 | } |
| 204 | metadata = dictionary; |
| 205 | continue; |
| 206 | } |
| 207 | if (prop.NameEquals("temperature"u8)) |
| 208 | { |
| 209 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 210 | { |
| 211 | temperature = null; |
| 212 | continue; |
| 213 | } |
| 214 | temperature = prop.Value.GetSingle(); |
| 215 | continue; |
| 216 | } |
| 217 | if (prop.NameEquals("model"u8)) |
| 218 | { |
| 219 | model = prop.Value.GetString(); |
| 220 | continue; |
| 221 | } |
| 222 | if (prop.NameEquals("tools"u8)) |
| 223 | { |
| 224 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 225 | { |
| 226 | continue; |
| 227 | } |
| 228 | List<ToolDefinition> array = new List<ToolDefinition>(); |
| 229 | foreach (var item in prop.Value.EnumerateArray()) |
| 230 | { |
| 231 | array.Add(ToolDefinition.DeserializeToolDefinition(item, options)); |
| 232 | } |
| 233 | defaultTools = array; |
| 234 | continue; |
| 235 | } |
| 236 | if (prop.NameEquals("tool_resources"u8)) |
| 237 | { |
| 238 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 239 | { |
| 240 | toolResources = null; |
| 241 | continue; |
| 242 | } |
| 243 | toolResources = ToolResources.DeserializeToolResources(prop.Value, options); |
| 244 | continue; |
| 245 | } |
| 246 | if (prop.NameEquals("response_format"u8)) |
| 247 | { |
| 248 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 249 | { |
| 250 | responseFormat = null; |
| 251 | continue; |
| 252 | } |
| 253 | responseFormat = AssistantResponseFormat.DeserializeAssistantResponseFormat(prop.Value, options); |
| 254 | continue; |
| 255 | } |
| 256 | if (prop.NameEquals("top_p"u8)) |
| 257 | { |
| 258 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 259 | { |
| 260 | nucleusSamplingFactor = null; |
| 261 | continue; |
| 262 | } |
| 263 | nucleusSamplingFactor = prop.Value.GetSingle(); |
| 264 | continue; |
| 265 | } |
| 266 | if (prop.NameEquals("reasoning_effort"u8)) |
| 267 | { |
| 268 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 269 | { |
| 270 | reasoningEffortLevel = null; |
| 271 | continue; |
| 272 | } |
| 273 | reasoningEffortLevel = new ChatReasoningEffortLevel(prop.Value.GetString()); |
| 274 | continue; |
| 275 | } |
| 276 | // Plugin customization: remove options.Format != "W" check |
| 277 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 278 | } |
| 279 | return new AssistantModificationOptions( |
| 280 | name, |
| 281 | description, |
| 282 | instructions, |
| 283 | metadata ?? new ChangeTrackingDictionary<string, string>(), |
| 284 | temperature, |
| 285 | model, |
| 286 | defaultTools ?? new ChangeTrackingList<ToolDefinition>(), |
| 287 | toolResources, |
| 288 | responseFormat, |
| 289 | nucleusSamplingFactor, |
| 290 | reasoningEffortLevel, |
| 291 | additionalBinaryDataProperties); |
| 292 | } |
| 293 | |
| 294 | BinaryData IPersistableModel<AssistantModificationOptions>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 295 | |
| 296 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 297 | { |
| 298 | string format = options.Format == "W" ? ((IPersistableModel<AssistantModificationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 299 | switch (format) |
| 300 | { |
| 301 | case "J": |
| 302 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 303 | default: |
| 304 | throw new FormatException($"The model {nameof(AssistantModificationOptions)} does not support writing '{options.Format}' format."); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | AssistantModificationOptions IPersistableModel<AssistantModificationOptions>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 309 | |
| 310 | protected virtual AssistantModificationOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 311 | { |
| 312 | string format = options.Format == "W" ? ((IPersistableModel<AssistantModificationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 313 | switch (format) |
| 314 | { |
| 315 | case "J": |
| 316 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 317 | { |
| 318 | return DeserializeAssistantModificationOptions(document.RootElement, options); |
| 319 | } |
| 320 | default: |
| 321 | throw new FormatException($"The model {nameof(AssistantModificationOptions)} does not support reading '{options.Format}' format."); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | string IPersistableModel<AssistantModificationOptions>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 326 | } |
| 327 | } |
| 328 | |