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