using BeWo.Data.Access; using BeWo.Data.Entities; using BeWo.Service.ServiceProxy; using BeWo.Service.ServiceUtils; using BS.Shared; using BS.Shared.DataContracts.Feature.AI; using BS.Shared.DataContracts.Feature.AI.Functions.ServiceRecords; using DevExpress.DataAccess.Native.Web; using RestSharp.Validation; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AICore.Prompt.SystemPrompts; using BeWo.Service.Core; using BS.Shared.Extensions.CustomInterfaces; using DevExpress.XtraCharts.Native; using System.IO; using BS.Shared.Interface; using System.Text.Json; using BS.Shared.Core; namespace BeWo.Service.Plugins { public class AiFunctionService { public AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(AiFunctionDocRequest request) { // Request valideren ValidateDocRequest(request); // Request auswerten // DB Daten holen // DB Daten prüfen // Anfrage zusammensetzen var context = AiFunctionContextLoader.LoadServiceRecordDocumentationContext(request); var systemPromptFolderPath = BeWoServerPathHelper.GetAiSystemPromptPath(); var factory = new AiFunctionSystemPromptBuilder(systemPromptFolderPath); var system_prompt = factory.CreateServiceRecordDocumentationSystemPrompt(context); var prompt_baustein_ref = request.AiPromptbausteinPrompt; var prompt_baustein_ref_oid = prompt_baustein_ref.Oid; var prompt_baustein_ref_ver = prompt_baustein_ref.Version; IAiPromptbausteinPrompt prompt_baustein = null; if(prompt_baustein_ref_oid > 0) { prompt_baustein = DAOFactory.GenericDAO.LoadByID(prompt_baustein_ref_oid); ServiceLogic.ConcurrencyCheck(prompt_baustein_ref_ver, (AiPromptbausteinPrompt)prompt_baustein); } else { prompt_baustein = findOwnsoftPrompt(AiActionType.ServiceRecordDocumentation, prompt_baustein_ref.Oid); } // Anfrage abschicken var result = getResult(system_prompt, prompt_baustein.Prompt); // Response auswerten // Eigene Response erstellen var response = new AiFunctionDocResponse(); response.Result = result; return response; } private string getResult(string system, string user_prompt) { var models = ServiceFacade.Ollama2ApiClient.GetAiModelle(); var model = models.Data.FirstOrDefault(x => x.ModelName == "gemma3_12b_max_context:latest"); if (model == null) throw new NotImplementedException(); var messages = new List() { system, user_prompt }; if(model.Value > -1) { AiUtils.CheckTokenSize(messages, model.ModelName, model.Value); } var payload = new { model = "gemma3_12b_max_context:latest", messages = new[] { new { role = "System", content = system }, new { role = "User", content = user_prompt }, } }; var logger = ServiceLogger.GetRequestLogger(); var response = ServiceFacade.Ollama2ApiClient.SendAiMessageRequest(payload, out string message, out string model2, out int? duration, logger); return message; } private void ValidateDocRequest(AiFunctionDocRequest request) { //throw new NotImplementedException(); } private AiPromptbausteinPromptDC findOwnsoftPrompt(AiActionType aiAction, long prompt_oid) { var root = getOwnsoftFolder(aiAction); return findOwnsoftPrompt_rec(root, prompt_oid); } private AiPromptbausteinPromptDC findOwnsoftPrompt_rec(AiPromptbausteinFolderDC dc, long prompt_oid) { foreach (var subprompts in dc.SubPrompts) { if (subprompts.Oid == prompt_oid) return subprompts; } foreach (var subfolder in dc.SubFolders) { var search = findOwnsoftPrompt_rec(subfolder, prompt_oid); if (search != null) return search; } return null; } private AiPromptbausteinFolderDC getOwnsoftFolder(AiActionType aiActionType) { var folder_path = BeWoServerPathHelper.GetAiOwnsoftPromptPath(); var file_name = $"{aiActionType.ToString().ToLower()}.json"; var file_path = Path.Combine(folder_path, file_name); if (!File.Exists(file_path)) return null; var json = File.ReadAllText(file_path, Encoding.UTF8); var ownsoft_folder = JsonSerializer.Deserialize(json); return ownsoft_folder; } } }