From 35d86a5737de4555b4fc18642cbad2c1dcb967ed Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Tue, 11 Jun 2024 14:20:05 -0700 Subject: [PATCH 1/2] improved ToString implementations overriden in a previous PR --- src/Custom/Chat/ChatCompletion.cs | 17 ++++++++++++++++- src/Custom/Chat/ChatMessageContentPart.cs | 8 +++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Custom/Chat/ChatCompletion.cs b/src/Custom/Chat/ChatCompletion.cs index 0efd2e53..e2166541 100644 --- a/src/Custom/Chat/ChatCompletion.cs +++ b/src/Custom/Chat/ChatCompletion.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; namespace OpenAI.Chat; @@ -65,5 +66,19 @@ public partial class ChatCompletion /// Returns text representation of the first part of the first choice. /// /// - public override string ToString() => Content[0].Text; + public override string ToString() + { + IReadOnlyList 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(); + } } diff --git a/src/Custom/Chat/ChatMessageContentPart.cs b/src/Custom/Chat/ChatMessageContentPart.cs index f76b7842..fd850583 100644 --- a/src/Custom/Chat/ChatMessageContentPart.cs +++ b/src/Custom/Chat/ChatMessageContentPart.cs @@ -129,7 +129,13 @@ public static ChatMessageContentPart CreateImageMessageContentPart(BinaryData im /// Returns text representation of this part. /// /// - public override string ToString() => Text; + public override string ToString() + { + if (Kind == ChatMessageContentPartKind.Text) { + return String.IsNullOrWhiteSpace(Text) ? "" : Text; + } + return $"<{Kind.ToString().ToLowerInvariant()}>"; + } /// /// Implicitly creates a new instance from an item of plain text. From 9f620b36197e14e3115bf14e3f6b1a487ce8135b Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Tue, 11 Jun 2024 15:23:46 -0700 Subject: [PATCH 2/2] Update src/Custom/Chat/ChatCompletion.cs Co-authored-by: Stephen Toub --- src/Custom/Chat/ChatCompletion.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Custom/Chat/ChatCompletion.cs b/src/Custom/Chat/ChatCompletion.cs index e2166541..b53f8d5a 100644 --- a/src/Custom/Chat/ChatCompletion.cs +++ b/src/Custom/Chat/ChatCompletion.cs @@ -74,11 +74,6 @@ public override string ToString() return content[0].ToString(); } - StringBuilder sb = new(); - foreach (var part in content) - { - sb.AppendLine(part.ToString()); - } - return sb.ToString(); + return string.Join(Environment.NewLine, content); } }