Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

158 linhas
5.6KB

  1. using Services.ReActAgent;
  2. using System.Text;
  3. using ToolsServices;
  4. namespace Services
  5. {
  6. public class TF_From_DSFService
  7. {
  8. private static readonly string NomFichierParametres = FichiersInternesService.ParamsTF_From_DSF;
  9. public static async Task<(bool, string)> Traitement(string inputPath, string outputPath, bool isApiExterne)
  10. {
  11. StringBuilder sb = new();
  12. LoggerService.LogInfo($"TF_From_DSFService.Traitement");
  13. if (!Directory.Exists(inputPath))
  14. {
  15. LoggerService.LogWarning($"Le dossier {inputPath} n'existe pas.");
  16. return (false, $"Le dossier {inputPath} n'existe pas.");
  17. }
  18. if (!Directory.Exists(outputPath))
  19. {
  20. LoggerService.LogWarning($"Le dossier {outputPath} n'existe pas.");
  21. return (false, $"Le dossier {outputPath} n'existe pas.");
  22. }
  23. var fichiers = Directory
  24. .EnumerateFiles(inputPath, "*.*", SearchOption.AllDirectories)
  25. .Where(f => f.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".docx", StringComparison.OrdinalIgnoreCase))
  26. .ToArray();
  27. if (fichiers.Length == 0)
  28. {
  29. LoggerService.LogWarning($"Aucun fichier trouvé dans le dossier spécifié : {inputPath}");
  30. return (false, $"Aucun fichier trouvé dans le dossier spécifié : {inputPath}");
  31. }
  32. var reActRagAgent = new ReActAgent.ReActAgent();
  33. var model = ReActAgent.ReActAgent.GetModeleIA(ModelsUseCases.TypeUseCase.TF_From_DSF);
  34. foreach (var fichier in fichiers)
  35. {
  36. try
  37. {
  38. #region Analyse du DSF
  39. var allCsv = new StringBuilder();
  40. var docDSF = FilesService.ExtractText(fichier);
  41. var chunks = RAGService.ChunkText(docDSF, 2000);// Chunker.ChunkText(docDSF, 2000); //SplitText(docDSF, 3000);
  42. var nbChuncks = chunks.Length;
  43. int numChuck = 0;
  44. foreach (var chunk in chunks)
  45. {
  46. numChuck++;
  47. var prompt = PromptService.GetPrompt(PromptService.ePrompt.TF_From_DSFService_GenereTF, chunk);
  48. LoggerService.LogDebug($"Traitement TF From DSF {numChuck}/{nbChuncks} : {fichier}");
  49. var (reponse,m) = await reActRagAgent.AppelerLLMAsync(ModelsUseCases.TypeUseCase.TF_From_DSF, true, prompt, "Traitement de TF From DSF", model, isApiExterne);
  50. allCsv.AppendLine(reponse);
  51. }
  52. #region Sauvegarde du résultat
  53. /*
  54. string horodate = DateTime.Now.ToString("hhmmss");
  55. string fileNameCSV = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(fichier) + $"_{model.Replace(":","-")}_{horodate}.csv");
  56. */
  57. string fileNameCSV = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(fichier) + $".csv");
  58. TxtService.CreateTextFile(fileNameCSV, allCsv.ToString());
  59. #endregion
  60. #endregion
  61. }
  62. catch (Exception ex)
  63. {
  64. var msg = $"Erreur lors de l'extraction du texte de {fichier} : {ex.Message}";
  65. LoggerService.LogError(msg);
  66. return (false, msg);
  67. }
  68. }
  69. //}
  70. LoggerService.LogInfo($"Traitement TF From DSF terminée");
  71. return (true, sb.ToString());
  72. }
  73. private static List<string> SplitText(string text, int chunkSize = 3000)
  74. {
  75. var chunks = new List<string>();
  76. for (int i = 0; i < text.Length; i += chunkSize)
  77. {
  78. int length = Math.Min(chunkSize, text.Length - i);
  79. chunks.Add(text.Substring(i, length));
  80. }
  81. return chunks;
  82. }
  83. public static (string, string) LoadParametres()
  84. {
  85. LoggerService.LogInfo("TF_From_DSFService.LoadParametres");
  86. //ParametresOllamaService SelectedItem = new();
  87. try
  88. {
  89. string FicheMission = "";
  90. string PathCV = "";
  91. if (File.Exists(NomFichierParametres))
  92. {
  93. string[] lignes = File.ReadAllLines(NomFichierParametres);
  94. if (lignes.Length > 0)
  95. FicheMission = lignes[0];
  96. if (lignes.Length > 1)
  97. PathCV = lignes[1];
  98. }
  99. return (FicheMission, PathCV);
  100. }
  101. catch
  102. {
  103. LoggerService.LogError($"Erreur lors du chargement des paramètres depuis {NomFichierParametres}");
  104. return ("", "");
  105. }
  106. }
  107. public static bool SaveParametres(string inputPath, string outputPath)
  108. {
  109. LoggerService.LogInfo("TF_From_DSFService.SaveParametres");
  110. try
  111. {
  112. StringBuilder sb = new();
  113. sb.AppendLine(inputPath);
  114. sb.AppendLine(outputPath);
  115. File.WriteAllText(NomFichierParametres, sb.ToString());
  116. return true;
  117. }
  118. catch
  119. {
  120. LoggerService.LogError($"Erreur lors de la sauvegarde des paramètres dans {NomFichierParametres}");
  121. return false;
  122. }
  123. }
  124. }
  125. }