Table of Contents

Class MessageBuilder

Namespace
OllamaSharp.Models.Chat
Assembly
OllamaSharp.dll

A builder class for constructing a Message by appending multiple message chunks.

public class MessageBuilder
Inheritance
MessageBuilder
Inherited Members

Properties

HasValue

Indicates whether the current MessageBuilder instance contains any content, images, or tool calls.

public bool HasValue { get; }

Property Value

bool

Examples

Example usage:

var messageBuilder = new MessageBuilder();
Console.WriteLine(messageBuilder.HasValue); // Output: False
// Append a new chunk of content
messageBuilder.Append(new ChatResponseStream
{
  Message = new Message
  {
    Content = "Hello, how can I assist you?",
    Role = ChatRole.Assistant
  }
});
Console.WriteLine(messageBuilder.HasValue); // Output: True

Remarks

The HasValue property evaluates to true if:

  • The internal content builder has accumulated text.
  • There are any tool calls added to the MessageBuilder.
  • There are images included in the MessageBuilder.
  • Otherwise, it returns false.

    This property is useful for verifying whether the builder contains meaningful data before processing further, such as converting it to a Message or appending additional elements to it.

    Images

    Represents the collection of image references included in a message.

    public IReadOnlyCollection<string>? Images { get; }

    Property Value

    IReadOnlyCollection<string>

    Examples

    Example usage:

    var builder = new MessageBuilder();
    var chunk = new ChatResponseStream
    {
      Message = new Message
      {
        Content = "Here is an example image:",
        Role = ChatRole.Assistant,
        Images = new[] { "example_image.png" } // Note: Images are base64 encoded, this is just an example
      }
    };
    builder.Append(chunk);
    var resultMessage = builder.ToMessage();
    Console.WriteLine(string.Join(", ", resultMessage.Images)); // Output: example_image.png

    Remarks

    This property contains a read-only collection of image file paths or URIs associated with the message content. The Images property is often utilized in scenarios where messages include supplementary visual content, such as in chat interfaces, AI-generated responses with images, or tools requiring multimedia integration.

    Role

    Represents the role associated with a chat message. Roles are used to determine the purpose or origin of a message, such as system, user, assistant, or tool.

    public ChatRole? Role { get; }

    Property Value

    ChatRole?

    Examples

    Example usage:

    var messageBuilder = new MessageBuilder();
    var chunk = new ChatResponseStream
    {
      Message = new Message
      {
        Content = "What can I help you with?",
        Role = ChatRole.Assistant
      }
    };
    messageBuilder.Append(chunk);
    Console.WriteLine(messageBuilder.Role); // Output: Assistant

    Remarks

    The Role property is typically used to indicate the sender's context or role within a conversation.

  • System: Represents system-generated messages.
  • User: Represents messages sent by the user.
  • Assistant: Represents messages generated by an assistant or AI model.
  • Tool: Represents messages or actions triggered by external tools.
  • ToolCalls

    Represents the collection of tool calls associated with a chat message.

    public IReadOnlyCollection<Message.ToolCall>? ToolCalls { get; }

    Property Value

    IReadOnlyCollection<Message.ToolCall>

    Examples

    Example usage:

    var messageBuilder = new MessageBuilder();
    var toolCall = new Message.ToolCall
    {
      Function = new Message.Function()
    };
    var chunk = new ChatResponseStream
    {
      Message = new Message
      {
        Content = "Triggered a tool call",
        ToolCalls = new[] { toolCall }
      }
    };
    messageBuilder.Append(chunk);
    Console.WriteLine(messageBuilder.ToolCalls.Count); // Output: 1

    Remarks

    The ToolCalls property is used to store references to external tools or functions invoked during a chat conversation.

    Tool calls can include various functions or actions that were triggered by the message. For instance:
  • Fetching data asynchronously from APIs.
  • Executing background processes.
  • Triggering integrations with third-party tools.
  • This property aggregates and holds all tool calls appended via the builder during message construction.

    Methods

    Append(ChatResponseStream?)

    Appends a chat response stream chunk to the message under construction.

    public void Append(ChatResponseStream? chunk)

    Parameters

    chunk ChatResponseStream

    The ChatResponseStream instance containing a message and additional data to append. If the message is nulln, no operation is performed.

    Examples

    Example usage:

    var builder = new MessageBuilder();
    var chunk = new ChatResponseStream
    {
    	Message = new Message
    	{
    		Content = "Hello, World!",
    		Role = ChatRole.User,
    		Images = new[] { "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAAGUlEQVR4nGJpfnqXARtgwio6aCUAAQAA///KcwJYgRBQbAAAAABJRU5ErkJggg==" },
    		ToolCalls = new List<Message.ToolCall>()
    	}
    };
    builder.Append(chunk);
    var resultMessage = builder.ToMessage();
    Console.WriteLine(resultMessage.Content); // Output: Hello, World!

    Remarks

    This method processes the provided chunk by appending its message content to the underlying content builder, updates the Role based on the chunk's message role, and adds any related images or tool calls if present.

    ToMessage()

    Converts the current state of the message builder into a Message object.

    public Message ToMessage()

    Returns

    Message

    A Message instance containing the following elements:

    • Content: The combined content built using appended chunks.
    • Role: The role assigned to the message.
    • Images: An array of image strings associated with the message.
    • ToolCalls: A collection of tool call objects associated with the message.

    Examples

    Example usage:

    var builder = new MessageBuilder();
    builder.Role = ChatRole.Assistant;
    // Append content (this would typically be done with Append method)
    builder.Append(new ChatResponseStream
    {
      Message = new Message
      {
        Content = "Generated content from assistant."
      }
    });
    // Convert to a Message object
    var finalMessage = builder.ToMessage();
    Console.WriteLine(finalMessage.Content); // Output: Generated content from assistant.
    Console.WriteLine(finalMessage.Role);    // Output: Assistant