Skip to content

Commit

Permalink
improved ToString implementations overriden in a previous PR
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina committed Jun 11, 2024
1 parent e6fe89c commit 35d86a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/Custom/Chat/ChatCompletion.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenAI.Chat;

Expand Down Expand Up @@ -65,5 +66,19 @@ public partial class ChatCompletion
/// Returns text representation of the first part of the first choice.
/// </summary>
/// <returns></returns>
public override string ToString() => Content[0].Text;
public override string ToString()
{
IReadOnlyList<ChatMessageContentPart> content = Content;
if (content.Count == 1)
{
return content[0].ToString();
}

StringBuilder sb = new();
foreach (var part in content)
{
sb.AppendLine(part.ToString());
}
return sb.ToString();
}
}
8 changes: 7 additions & 1 deletion src/Custom/Chat/ChatMessageContentPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ public static ChatMessageContentPart CreateImageMessageContentPart(BinaryData im
/// Returns text representation of this part.
/// </summary>
/// <returns></returns>
public override string ToString() => Text;
public override string ToString()
{
if (Kind == ChatMessageContentPartKind.Text) {
return String.IsNullOrWhiteSpace(Text) ? "<empty>" : Text;
}
return $"<{Kind.ToString().ToLowerInvariant()}>";
}

/// <summary>
/// Implicitly creates a new <see cref="ChatMessageContentPart"/> instance from an item of plain text.
Expand Down

0 comments on commit 35d86a5

Please sign in to comment.