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