Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

168 lines
5.7KB

  1. using System.Text.Json;
  2. using ToolsServices;
  3. namespace Services.Ollama
  4. {
  5. #region Classes annexes
  6. public class ModelConfig
  7. {
  8. public string Primary { get; set; } = "";
  9. public string Secondary { get; set; } = "";
  10. public string Fallback { get; set; } = "";
  11. }
  12. public class ModelsConfiguration
  13. {
  14. public Dictionary<string, ModelConfig> Config { get; set; } = new();
  15. }
  16. #endregion
  17. #region Classe principale ModelSelector
  18. public class ModelSelector
  19. {
  20. #region Variables
  21. private readonly ModelsConfiguration _Models = new();
  22. private string ConfigPath = FichiersInternesService.ParamsModeles;
  23. #endregion
  24. #region Constructeur
  25. public ModelSelector()
  26. {
  27. if (!File.Exists(ConfigPath))
  28. {
  29. CreateConfig();
  30. }
  31. var json = File.ReadAllText(ConfigPath);
  32. _Models = JsonSerializer.Deserialize<ModelsConfiguration>(
  33. json,
  34. new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
  35. ) ?? new ModelsConfiguration();
  36. }
  37. #endregion
  38. #region Méthodes publiques
  39. public bool SaveConfig(string useCase, ModelConfig models)
  40. {
  41. LoggerService.LogInfo($"ModelSelector.SaveConfig pour le cas d'usage '{useCase}'");
  42. try
  43. {
  44. if (_Models.Config.ContainsKey(useCase))
  45. {
  46. _Models.Config[useCase].Primary = models.Primary;
  47. _Models.Config[useCase].Secondary = models.Secondary;
  48. _Models.Config[useCase].Fallback = models.Fallback;
  49. }
  50. else
  51. {
  52. _Models.Config[useCase] = new ModelConfig
  53. {
  54. Primary = models.Primary,
  55. Secondary = models.Secondary,
  56. Fallback = models.Fallback
  57. };
  58. }
  59. ModelsConfiguration config = new ModelsConfiguration
  60. {
  61. Config = _Models.Config
  62. };
  63. var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
  64. File.WriteAllText(ConfigPath, json);
  65. return true;
  66. }
  67. catch (Exception ex)
  68. {
  69. LoggerService.LogError($"Échec de la sauvegarde de la configuration des modèles du cas d'usage {useCase} : {ex.Message}");
  70. return false;
  71. }
  72. }
  73. public IEnumerable<string> GetModelsForUseCase(string useCase)
  74. {
  75. LoggerService.LogInfo($"ModelSelector.GetModelsForUseCase pour le cas d'usage : '{useCase}'");
  76. try
  77. {
  78. if (!_Models.Config.TryGetValue(useCase, out var config))
  79. throw new Exception($"Cas d'usage '{useCase}' introuvable dans la config.");
  80. var allModels = new List<string>();
  81. if (config.Primary != null && config.Primary != "")
  82. allModels.Add(config.Primary);
  83. if (config.Secondary != null && config.Secondary != "")
  84. allModels.Add(config.Secondary);
  85. if (config.Fallback != null && config.Fallback != "")
  86. allModels.Add(config.Fallback);
  87. return allModels;
  88. }
  89. catch (Exception ex)
  90. {
  91. LoggerService.LogError($"GetModelsForUseCase: {ex.Message}");
  92. return new List<string>();
  93. }
  94. }
  95. #endregion
  96. #region Méthodes privées
  97. private void CreateConfig()
  98. {
  99. var txt = """
  100. {
  101. "Config": {
  102. "LLM": {
  103. "Primary": "thewindmom/hermes-3-llama-3.1-8b:latest",
  104. "Secondary": "gemma3:12b",
  105. "Fallback": "deepseek-coder-v2:16b"
  106. },
  107. "RAG": {
  108. "Primary": "gemma3:12b-it-qat",
  109. "Secondary": "llama3-chatqa:8b",
  110. "Fallback": "phi4-mini:latest"
  111. },
  112. "Resume_Documents": {
  113. "Primary": "gemma3:12b-it-qat",
  114. "Secondary": "llama3-chatqa:8b",
  115. "Fallback": "phi4:latest"
  116. },
  117. "Interpretation_Images": {
  118. "Primary": "llava-llama3:latest",
  119. "Secondary": "llava:7b",
  120. "Fallback": ""
  121. },
  122. "Prompt_Generation_Fooocus": {
  123. "Primary": "openchat:latest",
  124. "Secondary": "gemma3:12b",
  125. "Fallback": "phi4-mini:latest"
  126. },
  127. "Analyse_Mails": {
  128. "Primary": "thewindmom/hermes-3-llama-3.1-8b:latest",
  129. "Secondary": "gemma3:12b-it-qat",
  130. "Fallback": "phi4:latest"
  131. },
  132. "Analyse_CV_Mission": {
  133. "Primary": "phi4:latest",
  134. "Secondary": "gemma3:27b",
  135. "Fallback": "llama3-chatqa:8b"
  136. },
  137. "Pdf_To_Xml": {
  138. "Primary": "gemma3:12b-it-qat",
  139. "Secondary": "phi4-mini:latest",
  140. "Fallback": ""
  141. }
  142. }
  143. }
  144. """;
  145. TxtService.CreateTextFile(ConfigPath, txt);
  146. }
  147. #endregion
  148. }
  149. #endregion
  150. }