Add file analysis and answer feedback
This commit is contained in:
@@ -3,6 +3,7 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../../features/chat/presentation/cubit/chat_state.dart';
|
||||
@@ -110,6 +111,10 @@ class ApiRepository {
|
||||
? const []
|
||||
: (versions.isEmpty ? [content] : versions),
|
||||
selectedVersion: message['selected_version'] as int? ?? 0,
|
||||
feedbackVersions:
|
||||
(message['feedback_versions'] as List<dynamic>? ?? const [])
|
||||
.map((value) => value as int?)
|
||||
.toList(),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
@@ -244,6 +249,61 @@ class ApiRepository {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> analyzeFiles(
|
||||
List<XFile> files,
|
||||
String question, {
|
||||
String? model,
|
||||
}) async {
|
||||
final client = http.Client();
|
||||
_activeRequestClient = client;
|
||||
try {
|
||||
final request = http.MultipartRequest(
|
||||
'POST',
|
||||
Uri.parse('$_baseUrl/v1/agent/files/analyze'),
|
||||
)..fields['question'] = question;
|
||||
if (model != null) request.fields['model'] = model;
|
||||
for (final file in files) {
|
||||
request.files.add(
|
||||
http.MultipartFile.fromBytes(
|
||||
'files',
|
||||
await file.readAsBytes(),
|
||||
filename: file.name,
|
||||
),
|
||||
);
|
||||
}
|
||||
final streamed = await client
|
||||
.send(request)
|
||||
.timeout(const Duration(minutes: 10));
|
||||
final response = await http.Response.fromStream(streamed);
|
||||
_checkStatus(response);
|
||||
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final result = data['result'] as String? ?? '';
|
||||
final names = (data['files'] as List<dynamic>? ?? const []).cast<String>();
|
||||
return names.isEmpty
|
||||
? result
|
||||
: '$result\n\nالملفات التي حللتها: ${names.join('، ')}';
|
||||
} finally {
|
||||
if (identical(_activeRequestClient, client)) _activeRequestClient = null;
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> rateAnswer(
|
||||
String conversationId,
|
||||
int messageIndex,
|
||||
int versionIndex,
|
||||
int rating,
|
||||
) async {
|
||||
final response = await http.put(
|
||||
Uri.parse(
|
||||
'$_baseUrl/v1/conversations/$conversationId/messages/$messageIndex/feedback',
|
||||
),
|
||||
headers: await _userHeaders(),
|
||||
body: jsonEncode({'version_index': versionIndex, 'rating': rating}),
|
||||
);
|
||||
_checkStatus(response);
|
||||
}
|
||||
|
||||
Stream<String> completeStream(
|
||||
List<ChatMessage> messages, {
|
||||
String? model,
|
||||
|
||||
Reference in New Issue
Block a user