Skip to content

Commit 3d0419c

Browse files
authored
Update ToChatResponse{Async} to also factor in AuthorName (#6910)
* Update ToChatResponse{Async} to also factor in AuthorName Originally we were only factoring in Role. Then we also augmented it to factor in MessageId, since updates from different messages are by definition not part of the same message. Now it also makes sense to factor in AuthorName, as different text from different speakers / agents are also by definition not part of the same message. * Fix useAsync inversion in tests
1 parent ffee6c7 commit 3d0419c

File tree

3 files changed

+383
-48
lines changed

3 files changed

+383
-48
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatResponseExtensions.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -306,26 +306,19 @@ private static void FinalizeResponse(ChatResponse response)
306306
private static void ProcessUpdate(ChatResponseUpdate update, ChatResponse response)
307307
{
308308
// If there is no message created yet, or if the last update we saw had a different
309-
// message ID or role than the newest update, create a new message.
310-
ChatMessage message;
311-
var isNewMessage = false;
312-
if (response.Messages.Count == 0)
313-
{
314-
isNewMessage = true;
315-
}
316-
else if (update.MessageId is { Length: > 0 } updateMessageId
317-
&& response.Messages[response.Messages.Count - 1].MessageId is string lastMessageId
318-
&& updateMessageId != lastMessageId)
319-
{
320-
isNewMessage = true;
321-
}
322-
else if (update.Role is { } updateRole
323-
&& response.Messages[response.Messages.Count - 1].Role is { } lastRole
324-
&& updateRole != lastRole)
309+
// identifying parts, create a new message.
310+
bool isNewMessage = true;
311+
if (response.Messages.Count != 0)
325312
{
326-
isNewMessage = true;
313+
var lastMessage = response.Messages[response.Messages.Count - 1];
314+
isNewMessage =
315+
NotEmptyOrEqual(update.AuthorName, lastMessage.AuthorName) ||
316+
NotEmptyOrEqual(update.MessageId, lastMessage.MessageId) ||
317+
NotNullOrEqual(update.Role, lastMessage.Role);
327318
}
328319

320+
// Get the message to target, either a new one or the last ones.
321+
ChatMessage message;
329322
if (isNewMessage)
330323
{
331324
message = new(ChatRole.Assistant, []);
@@ -418,4 +411,12 @@ private static void ProcessUpdate(ChatResponseUpdate update, ChatResponse respon
418411
}
419412
}
420413
}
414+
415+
/// <summary>Gets whether both strings are not null/empty and not the same as each other.</summary>
416+
private static bool NotEmptyOrEqual(string? s1, string? s2) =>
417+
s1 is { Length: > 0 } str1 && s2 is { Length: > 0 } str2 && str1 != str2;
418+
419+
/// <summary>Gets whether two roles are not null and not the same as each other.</summary>
420+
private static bool NotNullOrEqual(ChatRole? r1, ChatRole? r2) =>
421+
r1.HasValue && r2.HasValue && r1.Value != r2.Value;
421422
}

0 commit comments

Comments
 (0)