Skip to content

Add AddMessages Extension Method for IList<ChatMessage> #6114

@jeffhandley

Description

@jeffhandley

Our existing AddMessages/AddMessagesAsync extension methods don't help because they only work on they complete set of output, whereas for an interactive UI app it's necessary to process the updates as they arrive incrementally. This results in undesired boilerplate code in chat applications.

We could improve MEAI to make this nicer, especially given how it would need to be more complex still if they wanted to receive other content types or to retain other information from the updates, such as AuthorName, AdditionalProperties, RawRepresentation).

One option would be adding to MEAI's ChatResponseExtensions another extension method like this:

public static void AddMessages(this IList<ChatMessage> messages, ChatResponseUpdate update, Func<AIContent, bool>? filter = null)
{
    var contentsList = filter is null ? update.Contents : update.Contents.Where(filter).ToList();
    if (contentsList.Any())
    {
        messages.Add(new ChatMessage(update.Role ?? ChatRole.Assistant, contentsList)
        {
            AuthorName = update.AuthorName,
            RawRepresentation = update.RawRepresentation,
            AdditionalProperties = update.AdditionalProperties,
        });
    }
}

This is more general since it works with any content type and preserves the other metadata properties.

Then in application code, the call to AddNonTextContentToConversation would be replaced by a one-liner inside the await foreach above:

messages.AddMessages(update, filter: u => u is not TextContent);
  • @MackinnonBuck If you think this is better, would you be interested in adding this extension method to MEAI as part of this change? It's quite nice that the template code can now directly use MEAI updates in the same PR!
  • @stephentoub Do you have any opinions on this proposed extension method? If you have concerns like "it's too specific to this one scenario" please say so. In my view it is pretty specific to this one scenario, but also, UI applications are a critical scenario we need to have clean patterns for.

Originally posted by @SteveSandersonMS in #6096 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aiMicrosoft.Extensions.AI libraries

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions