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