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