您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

61 行
1.8KB

  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3. /// Définit un contrat pour tout service capable d'améliorer un texte.
  4. abstract class TextImprovementService {
  5. Future<String> improveText({
  6. required String originalText,
  7. required String userInstruction,
  8. });
  9. }
  10. /// L'implémentation Ollama de ce service.
  11. class OllamaTextImprovementService implements TextImprovementService {
  12. final String _apiUrl = 'http://192.168.20.200:11434/api/generate';
  13. final String _textModel = 'gpt-oss:20b';
  14. @override
  15. Future<String> improveText({
  16. required String originalText,
  17. required String userInstruction,
  18. }) async {
  19. print('[OllamaTextImprovementService] 🚀 Appel pour améliorer le texte...');
  20. final requestPrompt = """
  21. You are a social media writing assistant.
  22. A user wants to improve the following text:
  23. --- TEXT TO IMPROVE ---
  24. $originalText
  25. -----------------------
  26. The user's instruction is: "$userInstruction".
  27. Rewrite the text based on the instruction.
  28. Your output MUST be ONLY the improved text, without any extra commentary or explanations.
  29. """;
  30. final requestBody = {
  31. 'model': _textModel,
  32. 'prompt': requestPrompt,
  33. 'stream': false,
  34. };
  35. try {
  36. final response = await http.post(
  37. Uri.parse(_apiUrl),
  38. headers: {'Content-Type': 'application/json'},
  39. body: jsonEncode(requestBody),
  40. ).timeout(const Duration(minutes: 2));
  41. if (response.statusCode == 200) {
  42. final responseData = jsonDecode(response.body);
  43. return (responseData['response'] as String? ?? '').trim();
  44. } else {
  45. throw Exception('Erreur Ollama (improveText) ${response.statusCode}: ${response.body}');
  46. }
  47. } catch (e) {
  48. print('[OllamaTextImprovementService] ❌ Exception : ${e.toString()}');
  49. rethrow;
  50. }
  51. }
  52. }