42 lines
1012 B
Dart
42 lines
1012 B
Dart
enum ChatRole { user, assistant, system }
|
|
|
|
extension ChatRoleExt on ChatRole {
|
|
String enumToStr() => toString().split(".").last;
|
|
bool equalsTo(ChatRole role) => toString() == role.toString();
|
|
bool equalsStringTo(String role) => enumToStr() == role;
|
|
}
|
|
|
|
class OpenAIChatMessage {
|
|
late ChatRole role; // JSON of ChatMessage
|
|
late String content;
|
|
String? name;
|
|
|
|
OpenAIChatMessage(this.role, this.content, {String? name});
|
|
|
|
OpenAIChatMessage.fromJson(Map json) {
|
|
role = toChatRole(json['role']);
|
|
content = json['content'];
|
|
name = json['name'] ?? "";
|
|
}
|
|
|
|
ChatRole toChatRole(String strRole) {
|
|
ChatRole role = ChatRole.user;
|
|
for (ChatRole chatRole in ChatRole.values) {
|
|
if (chatRole.equalsStringTo(strRole)) {
|
|
role = chatRole;
|
|
break;
|
|
}
|
|
}
|
|
return role;
|
|
}
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
"role": role.enumToStr(),
|
|
"content": content,
|
|
"name": name,
|
|
};
|
|
|
|
@override
|
|
String toString() => toJson().toString();
|
|
}
|