Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.Shared.Diagnostics;

Expand Down Expand Up @@ -85,8 +86,26 @@ public ChatMessage Message
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }

/// <inheritdoc />
public override string ToString() =>
Choices is { Count: > 0 } choices ? string.Join(Environment.NewLine, choices) : string.Empty;
public override string ToString()
{
if (Choices.Count == 1)
{
return Choices[0].ToString();
}

StringBuilder sb = new();
for (int i = 0; i < Choices.Count; i++)
{
if (i > 0)
{
_ = sb.AppendLine().AppendLine();
}

_ = sb.Append("Choice ").Append(i).AppendLine(":").Append(Choices[i]);
}

return sb.ToString();
}

/// <summary>Creates an array of <see cref="StreamingChatCompletionUpdate" /> instances that represent this <see cref="ChatCompletion" />.</summary>
/// <returns>An array of <see cref="StreamingChatCompletionUpdate" /> instances that may be used to represent this <see cref="ChatCompletion" />.</returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ public IList<AIContent> Contents
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }

/// <inheritdoc/>
public override string ToString() => Text ?? string.Empty;
public override string ToString() =>
string.Concat(Contents.OfType<TextContent>());
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ public IList<AIContent> Contents
public string? ModelId { get; set; }

/// <inheritdoc/>
public override string ToString() => Text ?? string.Empty;
public override string ToString() =>
string.Concat(Contents.OfType<TextContent>());
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,40 @@ public void JsonSerialization_Roundtrips()
Assert.Equal("value", ((JsonElement)value!).GetString());
}

[Fact]
public void ToString_OneChoice_OutputsChatMessageToString()
{
ChatCompletion completion = new(
[
new ChatMessage(ChatRole.Assistant, "This is a test." + Environment.NewLine + "It's multiple lines.")
]);

Assert.Equal(completion.Choices[0].Text, completion.ToString());
}

[Fact]
public void ToString_MultipleChoices_OutputsAllChoicesWithPrefix()
{
ChatCompletion completion = new(
[
new ChatMessage(ChatRole.Assistant, "This is a test." + Environment.NewLine + "It's multiple lines."),
new ChatMessage(ChatRole.Assistant, "So is" + Environment.NewLine + " this."),
new ChatMessage(ChatRole.Assistant, "And this."),
]);

Assert.Equal(
"Choice 0:" + Environment.NewLine +
completion.Choices[0] + Environment.NewLine + Environment.NewLine +

"Choice 1:" + Environment.NewLine +
completion.Choices[1] + Environment.NewLine + Environment.NewLine +

"Choice 2:" + Environment.NewLine +
completion.Choices[2],

completion.ToString());
}

[Fact]
public void ToStreamingChatCompletionUpdates_SingleChoice()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Xunit;

Expand Down Expand Up @@ -91,7 +92,7 @@ public void Constructor_RoleList_PropsRoundtrip(int messageCount)
}

Assert.Equal("text-0", message.Text);
Assert.Equal("text-0", message.ToString());
Assert.Equal(string.Concat(Enumerable.Range(0, messageCount).Select(i => $"text-{i}")), message.ToString());
}

Assert.Null(message.AuthorName);
Expand Down Expand Up @@ -134,13 +135,13 @@ public void Text_GetSet_UsesFirstTextContent()
TextContent textContent = Assert.IsType<TextContent>(message.Contents[3]);
Assert.Equal("text-1", textContent.Text);
Assert.Equal("text-1", message.Text);
Assert.Equal("text-1", message.ToString());
Assert.Equal("text-1text-2", message.ToString());

message.Text = "text-3";
Assert.Equal("text-3", message.Text);
Assert.Equal("text-3", message.Text);
Assert.Same(textContent, message.Contents[3]);
Assert.Equal("text-3", message.ToString());
Assert.Equal("text-3text-2", message.ToString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ public void Text_GetSet_UsesFirstTextContent()
TextContent textContent = Assert.IsType<TextContent>(update.Contents[3]);
Assert.Equal("text-1", textContent.Text);
Assert.Equal("text-1", update.Text);
Assert.Equal("text-1", update.ToString());
Assert.Equal("text-1text-2", update.ToString());

update.Text = "text-3";
Assert.Equal("text-3", update.Text);
Assert.Equal("text-3", update.Text);
Assert.Same(textContent, update.Contents[3]);
Assert.Equal("text-3", update.ToString());
Assert.Equal("text-3text-2", update.ToString());
}

[Fact]
Expand Down
Loading