Files
BeWoPlaner/Service/ServiceImplementations/AiEnhancedServiceImp.cs

220 lines
5.9 KiB
C#

using BeWo.Data;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Data.Security;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceContracts;
using BeWo.Service.ServiceProxy;
using BeWo.Service.ServiceUtils;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Feature.AI;
using BS.Shared.DataContracts.MikePHPContracts;
using DevExpress.Entity.Model.Metadata;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BeWo.Service.ServiceImplementations
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class AiEnhancedServiceImp : IAiEnhancedService
{
public AiConfigDC GetAiConfig()
{
var configs = DAOFactory.GenericDAO.GetAllActive<AiConfig>();
var config = configs.FirstOrDefault();
if(config == null)
{
config = getDefaultAiConfig();
DAOFactory.GenericDAO.Insert(config);
}
var config_dc = MapperFactory.AiConfig.MapToNewDC(config);
return config_dc;
}
public AiConfigDC UpdateAiConfig(AiConfigDC toUpdate)
{
if (toUpdate?.Oid is null)
throw new InvalidOperationException();
var oid = toUpdate.Oid.Value;
var entity = DAOFactory.GenericDAO.LoadByID<AiConfig>(oid);
var update = MapperFactory.AiConfig.MergeWithEntity(toUpdate, entity);
DAOFactory.GenericDAO.Update(update);
var dc = MapperFactory.AiConfig.MapToNewDC(update);
return dc;
}
public IEnumerable<AiModelDC> GetAiModels()
{
var models = getAiModels();
return MapperFactory.AiModel.MapToNewDCs(models);
}
public IEnumerable<AiConversationDC> GetAiConversations(int uicontext)
{
// SearchDAO
var current_user = UserRightHelper.GetLoggedInUserWithOid();
var convs = DAOFactory.GenericDAO.GetAll<AiConversation>();
var filter = convs.Where(x => x.UIContext == uicontext && x.ApplicationUserOid == current_user.Oid);
var dcs = MapperFactory.AiConversation.MapToNewDCs(filter);
return dcs;
}
public AiConversationDC CreateAiConversation(AiConversationDC conversation, long modell_oid)
{
var current_user = UserRightHelper.GetLoggedInUserWithOid();
var system_prompt = AiSystemBuilder.CreateSystemInstructions(conversation.UIContext, conversation.ContextBeWoObjects);
var conv = new AiConversation();
conv.Created = DateTime.Now;
conv.ContextBeWoObjects = MapperFactory.AiConversation.Parse(conversation.ContextBeWoObjects);
conv.Displayname = "Displayname";
conv.Messages = new List<AiConversationMessage>();
conv.Modell = DAOFactory.GenericDAO.LoadByID<AiModel>(modell_oid);
conv.UIContext = conversation.UIContext;
conv.Updated = DateTime.Now;
conv.ApplicationUserOid = current_user.Oid;
var msg = new AiConversationMessage();
msg.Message = system_prompt;
msg.AiConversation = conv;
msg.Role = BS.Shared.AiConversationMessageRole.System;
msg.Created = DateTime.Now;
conv.Messages.Add(msg);
DAOFactory.GenericDAO.Insert(conv);
var dc = MapperFactory.AiConversation.MapToNewDC(conv);
return dc;
}
public AiConversationMessageDC[] SendNewMessage(long conversation, string message)
{
var conv = DAOFactory.GenericDAO.LoadByID<AiConversation>(conversation);
// Updated
conv.Updated = DateTime.Now;
// Füge Message hinzu
var user_msg = new AiConversationMessage();
user_msg.Message = message;
user_msg.AiConversation = conv;
user_msg.Role = BS.Shared.AiConversationMessageRole.User;
user_msg.Created = DateTime.Now;
conv.Messages.Add(user_msg);
// Update DB Entity
DAOFactory.GenericDAO.Update(conv);
var conv_dc = MapperFactory.AiConversation.MapToNewDC(conv);
var user_msg_dc = MapperFactory.AiConversationMessage.MapToNewDC(user_msg);
// Hole Config
var config = GetAiConfig();
// Send Request, wait for Response
var dt_before_request = DateTime.Now;
var result = ServiceFacade.OpenWebUI.SendAiMessageRequest(conv_dc, config, out var model, out var duration);
// Antwort
var assi_msg = new AiConversationMessage();
assi_msg.Message = result;
assi_msg.AiConversation = conv;
assi_msg.Role = BS.Shared.AiConversationMessageRole.Assistent;
assi_msg.Created = dt_before_request;
assi_msg.Duration = duration ?? 0;
assi_msg.ModelName = model;
conv.Messages.Add(assi_msg);
// Update conv.
DAOFactory.GenericDAO.Update(conv);
var assi_msg_dc = MapperFactory.AiConversationMessage.MapToNewDC(assi_msg);
return new AiConversationMessageDC[] { user_msg_dc, assi_msg_dc };
}
private AiConfig getDefaultAiConfig()
{
var config = new AiConfig();
config.Temperature = 1;
config.Top_P = 1;
config.Max_Gen_Len = 1000;
config.SelectedModel = getDefaultAiModel();
return config;
}
private AiModel getDefaultAiModel()
{
var models = getAiModels();
if (models == null || !models.Any())
return null;
return models.First();
}
private IEnumerable<AiModel> getAiModels()
{
var old_models = DAOFactory.GenericDAO.GetAll<AiModel>();
var new_models = ServiceFacade.OpenWebUI.GetAiModelle();
var unknown_models = new_models.ToList();
foreach (var old_model in old_models)
{
bool found = false;
foreach (var new_model in new_models)
{
if (old_model.ModelName == new_model.ModelName)
{
if (old_model.IsActive != BS.Shared.ActivationTypeId.Active)
DAOFactory.GenericDAO.SetActivationType(old_model, BS.Shared.ActivationTypeId.Active);
found = true;
unknown_models.Remove(new_model);
break;
}
}
if (!found)
DAOFactory.GenericDAO.Deactivate(old_model);
}
if (unknown_models.Count != 0)
{
foreach (var unknown_model in unknown_models)
{
DAOFactory.GenericDAO.Insert(unknown_model);
}
}
var models = DAOFactory.GenericDAO.GetAllActive<AiModel>();
return models;
}
}
}