Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #207 - Error Deserializing ChatMessage #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions OpenAI_API/Chat/ChatMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ public string Text

set
{
text = value;
image = null;
Type = "text";
if (value != null)
{
text = value;
image = null;
Type = "text";
}
}
}

Expand All @@ -181,9 +184,12 @@ public ImageInput Image

set
{
image = value;
text = null;
Type = "image_url";
if (value != null)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After spending a lot of time, I found that this was the root cause. Values were being overwritten, hence deserialization was breaking. I tried to think of all cases/paths, and it looks fine to me to add this check. @OkGoDoIt , if you can think of anything where this doesn't fit it, let me know.

{
image = value;
text = null;
Type = "image_url";
}
}
}

Expand Down Expand Up @@ -244,6 +250,11 @@ public ImageInput(string url, string detail = "auto")
this.Detail = detail;
}

/// <summary>
/// Creates an empty <see cref="ImageInput"/>
/// </summary>
public ImageInput() { }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to support deserialization


/// <summary>
/// Instantiates a new ImageInput object with the given image data bytes
/// </summary>
Expand Down Expand Up @@ -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<IList<ContentItem>>();
}
Expand Down
58 changes: 56 additions & 2 deletions OpenAI_Tests/ChatEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ChatMessage>(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<ChatMessage>(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<ChatMessage>();
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<List<ChatMessage>>(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));
}
}
}