Skip to content

Commit

Permalink
Fixed issue OkGoDoIt#207 - Error Deserializing ChatMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
zeecorleone committed May 9, 2024
1 parent 5f7c23a commit 5091730
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
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)
{
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() { }

/// <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));
}
}
}

0 comments on commit 5091730

Please sign in to comment.