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
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:
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
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
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.
ToolCalls
Represents the collection of tool calls associated with a chat message.
public IReadOnlyCollection<Message.ToolCall>? ToolCalls { get; }
Property Value
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:Methods
Append(ChatResponseStream?)
Appends a chat response stream chunk to the message under construction.
public void Append(ChatResponseStream? chunk)
Parameters
chunkChatResponseStreamThe 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
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