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