From 509173079648f080cf6dfa92f368831fb41d5b42 Mon Sep 17 00:00:00 2001 From: Zeeshan Mustafa Date: Thu, 9 May 2024 15:45:09 +0500 Subject: [PATCH] Fixed issue #207 - Error Deserializing ChatMessage --- OpenAI_API/Chat/ChatMessage.cs | 25 +++++++++---- OpenAI_Tests/ChatEndpointTests.cs | 58 +++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/OpenAI_API/Chat/ChatMessage.cs b/OpenAI_API/Chat/ChatMessage.cs index 7d77142..41d8fc9 100644 --- a/OpenAI_API/Chat/ChatMessage.cs +++ b/OpenAI_API/Chat/ChatMessage.cs @@ -159,9 +159,12 @@ public string Text set { - text = value; - image = null; - Type = "text"; + if (value != null) + { + text = value; + image = null; + Type = "text"; + } } } @@ -181,9 +184,12 @@ public ImageInput Image set { - image = value; - text = null; - Type = "image_url"; + if (value != null) + { + image = value; + text = null; + Type = "image_url"; + } } } @@ -244,6 +250,11 @@ public ImageInput(string url, string detail = "auto") this.Detail = detail; } + /// + /// Creates an empty + /// + public ImageInput() { } + /// /// Instantiates a new ImageInput object with the given image data bytes /// @@ -312,7 +323,7 @@ public override bool CanConvert(Type objectType) public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); - if (token.Type == JTokenType.Object) + if (token.Type == JTokenType.Object || token.Type == JTokenType.Array) { return token.ToObject>(); } diff --git a/OpenAI_Tests/ChatEndpointTests.cs b/OpenAI_Tests/ChatEndpointTests.cs index f05b4e3..5167485 100644 --- a/OpenAI_Tests/ChatEndpointTests.cs +++ b/OpenAI_Tests/ChatEndpointTests.cs @@ -28,7 +28,7 @@ public void BasicCompletion() Assert.IsNotNull(api.Chat); - var results = api.Chat.CreateChatCompletionAsync(new ChatRequest() + var results = api.Chat.CreateChatCompletionAsync(new ChatRequest() { Model = Model.ChatGPTTurbo, Temperature = 0.1, @@ -566,5 +566,59 @@ public async Task DifferentSeedShouldBeDifferentOutput() Assert.AreNotEqual(jokeA, jokeB); } } - } + + [Test] + public void ChatMessageSerializationShouldDeserialzeAsExpected() + { + var msg = new ChatMessage() + { + Role = ChatMessageRole.User, + TextContent = "This is a test" + }; + var jmsg = JsonConvert.SerializeObject(msg); + var deserializedMsg = JsonConvert.DeserializeObject(jmsg); + + Assert.IsNotNull(deserializedMsg); + Assert.IsTrue(msg.TextContent.Equals(deserializedMsg.TextContent)); + Assert.IsTrue(deserializedMsg.Images.Count == msg.Images.Count); + } + + [Test] + public void ChatMessagesContentSerializationShouldDeserialzeAsExpected() + { + + var msg = new ChatMessage(ChatMessageRole.User, "What is this image about?", new ChatMessage.ImageInput[] { new ChatMessage.ImageInput("https://xyz.com/image.png") }); + + var jmsg = JsonConvert.SerializeObject(msg); + var deserializedMsg = JsonConvert.DeserializeObject(jmsg); + + Assert.IsNotNull(deserializedMsg); + Assert.IsTrue(msg.TextContent.Equals(deserializedMsg.TextContent)); + Assert.IsTrue(deserializedMsg.Images.Count == msg.Images.Count); + Assert.IsTrue(deserializedMsg.Images[0].Url.Equals(msg.Images[0].Url)); + } + + [Test] + public void ChatMessagesListSerializationShouldDeserialzeAsExpected() + { + var messages = new List(); + var msg1 = new ChatMessage(ChatMessageRole.User, "What is this image about?", new ChatMessage.ImageInput[] { new ChatMessage.ImageInput("https://xyz.com/image.png") }); + messages.Add(msg1); + var msg2 = new ChatMessage(ChatMessageRole.User, "And what is this image about?", new ChatMessage.ImageInput[] { new ChatMessage.ImageInput("https://xyz.com/image2.png") }); + messages.Add(msg2); + + var jmsgs = JsonConvert.SerializeObject(messages); + var deserializedMessagesList = JsonConvert.DeserializeObject>(jmsgs); + + Assert.IsNotNull(deserializedMessagesList); + Assert.IsTrue(deserializedMessagesList.Count == messages.Count); + Assert.IsTrue(deserializedMessagesList[0].TextContent.Equals(messages[0].TextContent)); + Assert.IsTrue(deserializedMessagesList[0].Images.Count == messages[0].Images.Count); + Assert.IsTrue(deserializedMessagesList[0].Images[0].Url.Equals(messages[0].Images[0].Url)); + + Assert.IsTrue(deserializedMessagesList[1].TextContent.Equals(messages[1].TextContent)); + Assert.IsTrue(deserializedMessagesList[1].Images.Count == messages[1].Images.Count); + Assert.IsTrue(deserializedMessagesList[1].Images[0].Url.Equals(messages[1].Images[0].Url)); + } + } }