LlamaMessage.withRole constructor

LlamaMessage.withRole({
  1. required String role,
  2. required String content,
})

Factory constructor to create a LlamaMessage instance based on the provided role.

The role parameter determines the type of LlamaMessage to create:

  • 'user': Creates a UserLlamaMessage.
  • 'assistant': Creates an AssistantLlamaMessage.
  • 'system': Creates a SystemLlamaMessage.

Throws an ArgumentError if the provided role is not one of the expected values.

Parameters:

  • role: The role of the chat message (e.g., 'user', 'assistant', 'system').
  • content: The content of the chat message.

Implementation

factory LlamaMessage.withRole({
  required String role,
  required String content,
}) {
  switch (role) {
    case 'user':
      return UserLlamaMessage(content);
    case 'assistant':
      return AssistantLlamaMessage(content);
    case 'system':
      return SystemLlamaMessage(content);
    default:
      throw ArgumentError('Invalid role: $role');
  }
}