54 lines
1.7 KiB
Dart
54 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Models/AssistantResponse.dart';
|
|
|
|
class AiChatMessage {
|
|
final String role;
|
|
final String content;
|
|
AiChatMessage({required this.role, required this.content});
|
|
Map<String, dynamic> toJson() => {'role': role, 'content': content};
|
|
}
|
|
|
|
class AssistantService {
|
|
final VisitAppContext visitAppContext;
|
|
final List<AiChatMessage> history = [];
|
|
|
|
AssistantService({required this.visitAppContext});
|
|
|
|
Future<AssistantResponse> chat({
|
|
required String message,
|
|
String? configurationId,
|
|
}) async {
|
|
final baseUrl = visitAppContext.clientAPI.apiApi?.basePath ?? 'https://api.mymuseum.be';
|
|
|
|
final body = {
|
|
'message': message,
|
|
'instanceId': visitAppContext.instanceId,
|
|
'appType': 'Mobile',
|
|
'configurationId': configurationId,
|
|
'language': visitAppContext.language?.toUpperCase() ?? 'FR',
|
|
'history': history.map((m) => m.toJson()).toList(),
|
|
};
|
|
|
|
final response = await http.post(
|
|
Uri.parse('$baseUrl/api/ai/chat'),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode(body),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
|
final result = AssistantResponse.fromJson(data);
|
|
history.add(AiChatMessage(role: 'user', content: message));
|
|
history.add(AiChatMessage(role: 'assistant', content: result.reply));
|
|
return result;
|
|
} else {
|
|
throw Exception('Erreur assistant: ${response.statusCode}');
|
|
}
|
|
}
|
|
|
|
void clearHistory() => history.clear();
|
|
}
|