Files
BeWoPlaner/Service/Plugins/AiFunctionService.cs

165 lines
4.3 KiB
C#
Raw Normal View History

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;
2025-11-24 15:47:45 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2025-12-04 15:06:39 +01:00
using AICore.Prompt.SystemPrompts;
using BeWo.Service.Core;
2025-12-12 14:57:26 +01:00
using BS.Shared.Extensions.CustomInterfaces;
2026-01-16 13:33:59 +01:00
using DevExpress.XtraCharts.Native;
using System.IO;
using BS.Shared.Interface;
using System.Text.Json;
2026-01-30 12:20:25 +01:00
using BS.Shared.Core;
2026-02-19 14:32:37 +01:00
using BeWo.Data.Security;
2025-11-24 15:47:45 +01:00
namespace BeWo.Service.Plugins
{
public class AiFunctionService
{
public AiFunctionDocResponse ExecuteAiFunctionServiceRecordDocumentation(AiFunctionDocRequest request)
{
// Request valideren
ValidateDocRequest(request);
2025-11-24 15:47:45 +01:00
// Request auswerten
// DB Daten holen
// DB Daten prüfen
// Anfrage zusammensetzen
var context = AiFunctionContextLoader.LoadServiceRecordDocumentationContext(request);
2026-01-16 13:33:59 +01:00
var systemPromptFolderPath = BeWoServerPathHelper.GetAiSystemPromptPath();
2025-12-04 15:06:39 +01:00
var factory = new AiFunctionSystemPromptBuilder(systemPromptFolderPath);
var system_prompt = factory.CreateServiceRecordDocumentationSystemPrompt(context);
2026-01-13 12:46:43 +01:00
var prompt_baustein_ref = request.AiPromptbausteinPrompt;
2025-12-12 14:57:26 +01:00
var prompt_baustein_ref_oid = prompt_baustein_ref.Oid;
var prompt_baustein_ref_ver = prompt_baustein_ref.Version;
2026-01-16 13:33:59 +01:00
IAiPromptbausteinPrompt prompt_baustein = null;
if(prompt_baustein_ref_oid > 0)
{
prompt_baustein = DAOFactory.GenericDAO.LoadByID<AiPromptbausteinPrompt>(prompt_baustein_ref_oid);
ServiceLogic.ConcurrencyCheck(prompt_baustein_ref_ver, (AiPromptbausteinPrompt)prompt_baustein);
}
else
{
prompt_baustein = findOwnsoftPrompt(AiActionType.ServiceRecordDocumentation, prompt_baustein_ref.Oid);
}
2025-11-24 15:47:45 +01:00
// Anfrage abschicken
2026-01-13 12:46:43 +01:00
var result = getResult(system_prompt, prompt_baustein.Prompt);
2025-11-24 15:47:45 +01:00
// Response auswerten
// Eigene Response erstellen
var response = new AiFunctionDocResponse();
response.Result = result;
return response;
}
2026-01-16 13:33:59 +01:00
private string getResult(string system, string user_prompt)
{
2026-01-16 13:33:59 +01:00
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<string>()
{
system, user_prompt
};
if(model.Value > -1)
{
AiUtils.CheckTokenSize(messages, model.ModelName, model.Value);
}
var payload = new
{
2025-12-17 11:43:16 +01:00
model = "gemma3_12b_max_context:latest",
messages = new[]
{
new
{
role = "System",
content = system
},
new
{
role = "User",
content = user_prompt
},
2026-02-19 14:32:37 +01:00
},
customerid = UserRightHelper.GetTenant()
};
2025-12-02 15:40:12 +01:00
var logger = ServiceLogger.GetRequestLogger();
2026-01-16 13:33:59 +01:00
var response = ServiceFacade.Ollama2ApiClient.SendAiMessageRequest(payload, out string message, out string model2, out int? duration, logger);
2025-11-24 15:47:45 +01:00
return message;
}
private void ValidateDocRequest(AiFunctionDocRequest request)
{
//throw new NotImplementedException();
2025-11-24 15:47:45 +01:00
}
2026-01-16 13:33:59 +01:00
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<AiPromptbausteinFolderDC>(json);
return ownsoft_folder;
}
2025-11-24 15:47:45 +01:00
}
}