Message.fromMap constructor
Implementation
factory Message.fromMap(Map<String, dynamic> map) {
String roleStr = map['role'];
DateTime getDate() {
/// Open ai sends "created_at" as a integer as Epoch milliseconds,
/// but some other systems could send the value as a ISO string or
/// in the "created" map key.
var createdAt = map['date'] ??
map['created_at'] ??
map['createdAt'] ??
map['created'];
if (createdAt is num) {
return DateTime.fromMillisecondsSinceEpoch(createdAt.toInt());
}
if (createdAt is String) {
return DateTime.parse(createdAt);
}
return DateTime.now();
}
String getText() {
var content = map['content'];
if (content is String) {
return content;
}
/// Open AI message structure
if (content is List) {
for (Map item in content) {
String type = item['type'];
if (type == 'text') {
Map text = item['text'];
//List annotations = item['annotations'];
return text['value'];
}
}
}
return 'NOT IMPLEMENTED';
}
var role = roleStr == 'user' ? Role.user : Role.assistant;
Map<String, dynamic>? usage = map['usage'];
return Message(
date: getDate(),
text: getText(),
role: role,
usage: usage == null ? null : Usage.fromMap(usage),
audio: null,
// TODO
// map['audio'] != null && map['audio'].isNotEmpty
// ? base64Decode(map['audio'])
// : null,
);
}