Add AI API request, AI API response and AI message model

This commit is contained in:
Théo Poizat
2025-12-04 16:52:00 +01:00
committed by Dat H. Pham
parent b3e111b9bf
commit eaa30918cc
4 changed files with 80 additions and 0 deletions
@@ -0,0 +1,16 @@
import 'ai_message.dart';
class AIAPIRequest {
final List<AIMessage> messages;
const AIAPIRequest({
required this.messages,
});
Map<String, dynamic> toJson() {
return {
'model': 'gpt-oss-120b',
'messages': messages.map((m) => m.toJson()).toList(),
};
}
}
@@ -0,0 +1,31 @@
class AIApiResponse {
final String content;
const AIApiResponse({
required this.content
});
static AIApiResponse? parse(Map<String, dynamic> json) {
final choices = json['choices'] as List<dynamic>?;
if (choices == null || choices.isEmpty) {
return null;
}
final firstChoice = choices[0] as Map<String, dynamic>?;
if (firstChoice == null) {
return null;
}
final message = firstChoice['message'] as Map<String, dynamic>?;
if (message == null) {
return null;
}
final content = message['content'] as String?;
if (content == null || content.isEmpty) {
return null;
}
return AIApiResponse(content: content);
}
}
@@ -0,0 +1,16 @@
class AIMessage {
final String role;
final String content;
const AIMessage({
required this.role,
required this.content,
});
Map<String, dynamic> toJson() {
return {
'role': role,
'content': content,
};
}
}
@@ -0,0 +1,17 @@
class AIResponse {
final String result;
const AIResponse({required this.result});
factory AIResponse.fromJson(Map<String, dynamic> json) {
return AIResponse(
result: json['result'] as String? ?? json['text'] as String? ?? '',
);
}
Map<String, dynamic> toJson() {
return {
'result': result,
};
}
}