openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/AssistantChatMessage.Serialization.cs
50lines · modecode
| 1 | using System.ClientModel.Primitives; |
| 2 | using System.Text.Json; |
| 3 | |
| 4 | namespace OpenAI.Chat; |
| 5 | |
| 6 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Chat.AssistantChatMessage>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 7 | public partial class AssistantChatMessage : IJsonModel<AssistantChatMessage> |
| 8 | { |
| 9 | void IJsonModel<AssistantChatMessage>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 10 | => CustomSerializationHelpers.SerializeInstance(this, SerializeAssistantChatMessage, writer, options); |
| 11 | |
| 12 | internal static void SerializeAssistantChatMessage(AssistantChatMessage instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 13 | => instance.WriteCore(writer, options); |
| 14 | |
| 15 | internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 16 | { |
| 17 | writer.WriteStartObject(); |
| 18 | writer.WritePropertyName("role"u8); |
| 19 | writer.WriteStringValue(Role.ToSerialString()); |
| 20 | |
| 21 | // Content is optional, can be a single string or a collection of ChatMessageContentPart. |
| 22 | if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined()) |
| 23 | { |
| 24 | if (Content.Count > 0) |
| 25 | { |
| 26 | writer.WritePropertyName("content"u8); |
| 27 | if (Content.Count == 1 && Content[0].Text != null) |
| 28 | { |
| 29 | writer.WriteStringValue(Content[0].Text); |
| 30 | } |
| 31 | else |
| 32 | { |
| 33 | writer.WriteStartArray(); |
| 34 | foreach (ChatMessageContentPart part in Content) |
| 35 | { |
| 36 | writer.WriteObjectValue(part, options); |
| 37 | } |
| 38 | writer.WriteEndArray(); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | writer.WriteOptionalProperty("refusal"u8, Refusal, options); |
| 44 | writer.WriteOptionalProperty("name"u8, ParticipantName, options); |
| 45 | writer.WriteOptionalCollection("tool_calls"u8, ToolCalls, options); |
| 46 | writer.WriteOptionalProperty("function_call"u8, FunctionCall, options); |
| 47 | writer.WriteSerializedAdditionalRawData(SerializedAdditionalRawData, options); |
| 48 | writer.WriteEndObject(); |
| 49 | } |
| 50 | } |
| 51 | |