Table of Contents

An overview of built-in IMessage types

Start from 0.0.9, AutoGen introduces the IMessage and IMessage<T> types to provide a unified message interface for different agents. The IMessage is a non-generic interface that represents a message. The IMessage<T> is a generic interface that represents a message with a specific T where T can be any type.

Besides, AutoGen also provides a set of built-in message types that implement the IMessage and IMessage<T> interfaces. These built-in message types are designed to cover different types of messages as much as possilbe. The built-in message types include:

Note

The minimal requirement for an agent to be used as admin in GroupChat is to support TextMessage.

Note

Message will be deprecated in 0.0.14. Please replace it with a more specific message type like TextMessage, ImageMessage, etc.

Streaming message support

AutoGen also introduces IStreamingMessage and IStreamingMessage<T> which are used in streaming call api. The following built-in message types implement the IStreamingMessage and IStreamingMessage<T> interfaces:

Note

All IMessage is also a IStreamingMessage. That means you can return an IMessage from a streaming call method. It's also recommended to return the final updated result instead of the last update as the last message in the streaming call method to indicate the end of the stream, which saves caller's effort of assembling the final result from multiple updates.

Usage

The below code snippet shows how to print a streaming update to console and update the final result on the caller side.

var helloTextMessage = new TextMessage(Role.User, "Hello");
var reply = agent.GenerateStreamingReplyAsync([helloTextMessage]);
var finalTextMessage = new TextMessage(Role.Assistant, string.Empty, from: agent.Name);
await foreach (var message in reply)
{
    if (message is TextMessageUpdate textMessage)
    {
        Console.Write(textMessage.Content);
        finalTextMessage.Update(textMessage);
    }
}

If the agent returns a final result instead of the last update as the last message in the streaming call method, the caller can directly use the final result without assembling the final result from multiple updates.

reply = agent.GenerateStreamingReplyAsync([helloTextMessage]);
TextMessage finalMessage = null;
await foreach (var message in reply)
{
    if (message is TextMessageUpdate textMessage)
    {
        Console.Write(textMessage.Content);
    }
    else if (message is TextMessage txtMessage)
    {
        finalMessage = txtMessage;
    }
}