openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/AssistantCreationOptions.Serialization.cs
342lines · 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 | using OpenAI.Chat; |
| 12 | |
| 13 | namespace OpenAI.Assistants |
| 14 | { |
| 15 | public partial class AssistantCreationOptions : IJsonModel<AssistantCreationOptions> |
| 16 | { |
| 17 | void IJsonModel<AssistantCreationOptions>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 18 | { |
| 19 | writer.WriteStartObject(); |
| 20 | JsonModelWriteCore(writer, options); |
| 21 | writer.WriteEndObject(); |
| 22 | } |
| 23 | |
| 24 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 25 | { |
| 26 | string format = options.Format == "W" ? ((IPersistableModel<AssistantCreationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 27 | if (format != "J") |
| 28 | { |
| 29 | throw new FormatException($"The model {nameof(AssistantCreationOptions)} does not support writing '{format}' format."); |
| 30 | } |
| 31 | if (Optional.IsDefined(Name) && _additionalBinaryDataProperties?.ContainsKey("name") != true) |
| 32 | { |
| 33 | writer.WritePropertyName("name"u8); |
| 34 | writer.WriteStringValue(Name); |
| 35 | } |
| 36 | if (Optional.IsDefined(Description) && _additionalBinaryDataProperties?.ContainsKey("description") != true) |
| 37 | { |
| 38 | writer.WritePropertyName("description"u8); |
| 39 | writer.WriteStringValue(Description); |
| 40 | } |
| 41 | if (Optional.IsDefined(Instructions) && _additionalBinaryDataProperties?.ContainsKey("instructions") != true) |
| 42 | { |
| 43 | writer.WritePropertyName("instructions"u8); |
| 44 | writer.WriteStringValue(Instructions); |
| 45 | } |
| 46 | if (Optional.IsCollectionDefined(Metadata) && _additionalBinaryDataProperties?.ContainsKey("metadata") != true) |
| 47 | { |
| 48 | writer.WritePropertyName("metadata"u8); |
| 49 | writer.WriteStartObject(); |
| 50 | foreach (var item in Metadata) |
| 51 | { |
| 52 | writer.WritePropertyName(item.Key); |
| 53 | if (item.Value == null) |
| 54 | { |
| 55 | writer.WriteNullValue(); |
| 56 | continue; |
| 57 | } |
| 58 | writer.WriteStringValue(item.Value); |
| 59 | } |
| 60 | writer.WriteEndObject(); |
| 61 | } |
| 62 | if (Optional.IsDefined(Temperature) && _additionalBinaryDataProperties?.ContainsKey("temperature") != true) |
| 63 | { |
| 64 | writer.WritePropertyName("temperature"u8); |
| 65 | writer.WriteNumberValue(Temperature.Value); |
| 66 | } |
| 67 | if (_additionalBinaryDataProperties?.ContainsKey("model") != true) |
| 68 | { |
| 69 | writer.WritePropertyName("model"u8); |
| 70 | writer.WriteStringValue(Model); |
| 71 | } |
| 72 | if (Optional.IsCollectionDefined(Tools) && _additionalBinaryDataProperties?.ContainsKey("tools") != true) |
| 73 | { |
| 74 | writer.WritePropertyName("tools"u8); |
| 75 | writer.WriteStartArray(); |
| 76 | foreach (ToolDefinition item in Tools) |
| 77 | { |
| 78 | writer.WriteObjectValue(item, options); |
| 79 | } |
| 80 | writer.WriteEndArray(); |
| 81 | } |
| 82 | if (Optional.IsDefined(ToolResources) && _additionalBinaryDataProperties?.ContainsKey("tool_resources") != true) |
| 83 | { |
| 84 | writer.WritePropertyName("tool_resources"u8); |
| 85 | writer.WriteObjectValue(ToolResources, options); |
| 86 | } |
| 87 | if (Optional.IsDefined(ResponseFormat) && _additionalBinaryDataProperties?.ContainsKey("response_format") != true) |
| 88 | { |
| 89 | writer.WritePropertyName("response_format"u8); |
| 90 | writer.WriteObjectValue(ResponseFormat, options); |
| 91 | } |
| 92 | if (Optional.IsDefined(NucleusSamplingFactor) && _additionalBinaryDataProperties?.ContainsKey("top_p") != true) |
| 93 | { |
| 94 | writer.WritePropertyName("top_p"u8); |
| 95 | writer.WriteNumberValue(NucleusSamplingFactor.Value); |
| 96 | } |
| 97 | if (Optional.IsDefined(ReasoningEffortLevel) && _additionalBinaryDataProperties?.ContainsKey("reasoning_effort") != true) |
| 98 | { |
| 99 | writer.WritePropertyName("reasoning_effort"u8); |
| 100 | writer.WriteStringValue(ReasoningEffortLevel.Value.ToString()); |
| 101 | } |
| 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 | AssistantCreationOptions IJsonModel<AssistantCreationOptions>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 124 | |
| 125 | protected virtual AssistantCreationOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 126 | { |
| 127 | string format = options.Format == "W" ? ((IPersistableModel<AssistantCreationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 128 | if (format != "J") |
| 129 | { |
| 130 | throw new FormatException($"The model {nameof(AssistantCreationOptions)} does not support reading '{format}' format."); |
| 131 | } |
| 132 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 133 | return DeserializeAssistantCreationOptions(document.RootElement, options); |
| 134 | } |
| 135 | |
| 136 | internal static AssistantCreationOptions DeserializeAssistantCreationOptions(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> tools = 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 | tools = 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 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 277 | } |
| 278 | return new AssistantCreationOptions( |
| 279 | name, |
| 280 | description, |
| 281 | instructions, |
| 282 | metadata ?? new ChangeTrackingDictionary<string, string>(), |
| 283 | temperature, |
| 284 | model, |
| 285 | tools ?? new ChangeTrackingList<ToolDefinition>(), |
| 286 | toolResources, |
| 287 | responseFormat, |
| 288 | nucleusSamplingFactor, |
| 289 | reasoningEffortLevel, |
| 290 | additionalBinaryDataProperties); |
| 291 | } |
| 292 | |
| 293 | BinaryData IPersistableModel<AssistantCreationOptions>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 294 | |
| 295 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 296 | { |
| 297 | string format = options.Format == "W" ? ((IPersistableModel<AssistantCreationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 298 | switch (format) |
| 299 | { |
| 300 | case "J": |
| 301 | return ModelReaderWriter.Write(this, options); |
| 302 | default: |
| 303 | throw new FormatException($"The model {nameof(AssistantCreationOptions)} does not support writing '{options.Format}' format."); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | AssistantCreationOptions IPersistableModel<AssistantCreationOptions>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 308 | |
| 309 | protected virtual AssistantCreationOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 310 | { |
| 311 | string format = options.Format == "W" ? ((IPersistableModel<AssistantCreationOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 312 | switch (format) |
| 313 | { |
| 314 | case "J": |
| 315 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 316 | { |
| 317 | return DeserializeAssistantCreationOptions(document.RootElement, options); |
| 318 | } |
| 319 | default: |
| 320 | throw new FormatException($"The model {nameof(AssistantCreationOptions)} does not support reading '{options.Format}' format."); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | string IPersistableModel<AssistantCreationOptions>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 325 | |
| 326 | public static implicit operator BinaryContent(AssistantCreationOptions assistantCreationOptions) |
| 327 | { |
| 328 | if (assistantCreationOptions == null) |
| 329 | { |
| 330 | return null; |
| 331 | } |
| 332 | return BinaryContent.Create(assistantCreationOptions, ModelSerializationExtensions.WireOptions); |
| 333 | } |
| 334 | |
| 335 | public static explicit operator AssistantCreationOptions(ClientResult result) |
| 336 | { |
| 337 | using PipelineResponse response = result.GetRawResponse(); |
| 338 | using JsonDocument document = JsonDocument.Parse(response.Content); |
| 339 | return DeserializeAssistantCreationOptions(document.RootElement, ModelSerializationExtensions.WireOptions); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |