ChatMessage.fromMap constructor

ChatMessage.fromMap(
  1. Map<String, dynamic> map
)

Creates a ChatMessage instance from a map.

The map should contain a 'role' key with one of the following values:

The map should also contain a 'content' key with the message content.

Throws an Exception if the 'role' value is invalid.

Implementation

factory ChatMessage.fromMap(Map<String, dynamic> map) {
  switch (map['role']) {
    case 'user':
      return UserChatMessage(map['content']);
    case 'assistant':
      return AssistantChatMessage(map['content']);
    case 'system':
      return SystemChatMessage(map['content']);
    default:
      throw Exception('Invalid role');
  }
}