|
- using Services.ReActAgent;
- using System.Text;
- using ToolsServices;
-
- namespace Services
- {
- public class TF_From_DSFService
- {
- private static readonly string NomFichierParametres = FichiersInternesService.ParamsTF_From_DSF;
-
- public static async Task<(bool, string)> Traitement(string inputPath, string outputPath, bool isApiExterne)
- {
- StringBuilder sb = new();
- LoggerService.LogInfo($"TF_From_DSFService.Traitement");
-
- if (!Directory.Exists(inputPath))
- {
- LoggerService.LogWarning($"Le dossier {inputPath} n'existe pas.");
- return (false, $"Le dossier {inputPath} n'existe pas.");
- }
-
- if (!Directory.Exists(outputPath))
- {
- LoggerService.LogWarning($"Le dossier {outputPath} n'existe pas.");
- return (false, $"Le dossier {outputPath} n'existe pas.");
- }
-
- var fichiers = Directory
- .EnumerateFiles(inputPath, "*.*", SearchOption.AllDirectories)
- .Where(f => f.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".docx", StringComparison.OrdinalIgnoreCase))
- .ToArray();
-
- if (fichiers.Length == 0)
- {
- LoggerService.LogWarning($"Aucun fichier trouvé dans le dossier spécifié : {inputPath}");
- return (false, $"Aucun fichier trouvé dans le dossier spécifié : {inputPath}");
- }
-
- var reActRagAgent = new ReActAgent.ReActAgent();
- var model = ReActAgent.ReActAgent.GetModeleIA(ModelsUseCases.TypeUseCase.TF_From_DSF);
-
- foreach (var fichier in fichiers)
- {
- try
- {
- #region Analyse du DSF
- var allCsv = new StringBuilder();
-
- var docDSF = FilesService.ExtractText(fichier);
- var chunks = RAGService.ChunkText(docDSF, 2000);// Chunker.ChunkText(docDSF, 2000); //SplitText(docDSF, 3000);
- var nbChuncks = chunks.Length;
- int numChuck = 0;
-
- foreach (var chunk in chunks)
- {
- numChuck++;
- var prompt = PromptService.GetPrompt(PromptService.ePrompt.TF_From_DSFService_GenereTF, chunk);
-
- LoggerService.LogDebug($"Traitement TF From DSF {numChuck}/{nbChuncks} : {fichier}");
- var (reponse,m) = await reActRagAgent.AppelerLLMAsync(ModelsUseCases.TypeUseCase.TF_From_DSF, true, prompt, "Traitement de TF From DSF", model, isApiExterne);
- allCsv.AppendLine(reponse);
-
- }
-
-
- #region Sauvegarde du résultat
- /*
- string horodate = DateTime.Now.ToString("hhmmss");
- string fileNameCSV = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(fichier) + $"_{model.Replace(":","-")}_{horodate}.csv");
- */
- string fileNameCSV = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(fichier) + $".csv");
- TxtService.CreateTextFile(fileNameCSV, allCsv.ToString());
- #endregion
-
- #endregion
- }
-
- catch (Exception ex)
- {
- var msg = $"Erreur lors de l'extraction du texte de {fichier} : {ex.Message}";
- LoggerService.LogError(msg);
- return (false, msg);
- }
- }
- //}
-
- LoggerService.LogInfo($"Traitement TF From DSF terminée");
-
- return (true, sb.ToString());
- }
-
- private static List<string> SplitText(string text, int chunkSize = 3000)
- {
- var chunks = new List<string>();
- for (int i = 0; i < text.Length; i += chunkSize)
- {
- int length = Math.Min(chunkSize, text.Length - i);
- chunks.Add(text.Substring(i, length));
- }
- return chunks;
- }
-
- public static (string, string) LoadParametres()
- {
- LoggerService.LogInfo("TF_From_DSFService.LoadParametres");
- //ParametresOllamaService SelectedItem = new();
-
- try
- {
- string FicheMission = "";
- string PathCV = "";
- if (File.Exists(NomFichierParametres))
- {
-
- string[] lignes = File.ReadAllLines(NomFichierParametres);
-
- if (lignes.Length > 0)
- FicheMission = lignes[0];
-
- if (lignes.Length > 1)
- PathCV = lignes[1];
-
- }
- return (FicheMission, PathCV);
-
- }
- catch
- {
- LoggerService.LogError($"Erreur lors du chargement des paramètres depuis {NomFichierParametres}");
- return ("", "");
- }
- }
-
- public static bool SaveParametres(string inputPath, string outputPath)
- {
- LoggerService.LogInfo("TF_From_DSFService.SaveParametres");
- try
- {
- StringBuilder sb = new();
- sb.AppendLine(inputPath);
- sb.AppendLine(outputPath);
-
- File.WriteAllText(NomFichierParametres, sb.ToString());
-
- return true;
- }
- catch
- {
- LoggerService.LogError($"Erreur lors de la sauvegarde des paramètres dans {NomFichierParametres}");
- return false;
- }
-
- }
-
-
- }
- }
|