|
- import 'dart:convert';
- import 'package:http/http.dart' as http;
-
- /// Définit un contrat pour tout service capable d'améliorer un texte.
- abstract class TextImprovementService {
- Future<String> improveText({
- required String originalText,
- required String userInstruction,
- });
- }
-
- /// L'implémentation Ollama de ce service.
- class OllamaTextImprovementService implements TextImprovementService {
- final String _apiUrl = 'http://192.168.20.200:11434/api/generate';
- final String _textModel = 'gpt-oss:20b';
-
- @override
- Future<String> improveText({
- required String originalText,
- required String userInstruction,
- }) async {
- print('[OllamaTextImprovementService] 🚀 Appel pour améliorer le texte...');
- final requestPrompt = """
- You are a social media writing assistant.
- A user wants to improve the following text:
- --- TEXT TO IMPROVE ---
- $originalText
- -----------------------
-
- The user's instruction is: "$userInstruction".
-
- Rewrite the text based on the instruction.
- Your output MUST be ONLY the improved text, without any extra commentary or explanations.
- """;
-
- final requestBody = {
- 'model': _textModel,
- 'prompt': requestPrompt,
- 'stream': false,
- };
-
- try {
- final response = await http.post(
- Uri.parse(_apiUrl),
- headers: {'Content-Type': 'application/json'},
- body: jsonEncode(requestBody),
- ).timeout(const Duration(minutes: 2));
-
- if (response.statusCode == 200) {
- final responseData = jsonDecode(response.body);
- return (responseData['response'] as String? ?? '').trim();
- } else {
- throw Exception('Erreur Ollama (improveText) ${response.statusCode}: ${response.body}');
- }
- } catch (e) {
- print('[OllamaTextImprovementService] ❌ Exception : ${e.toString()}');
- rethrow;
- }
- }
- }
|