Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AddMessages Extension Method for IList<ChatMessage> #6114

Open
jeffhandley opened this issue Mar 14, 2025 · 0 comments
Open

Add AddMessages Extension Method for IList<ChatMessage> #6114

jeffhandley opened this issue Mar 14, 2025 · 0 comments
Labels
area-ai Microsoft.Extensions.AI libraries untriaged

Comments

@jeffhandley
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-ai Microsoft.Extensions.AI libraries untriaged
Projects
None yet
Development

No branches or pull requests

1 participant