307 lines
9.9 KiB
C#
307 lines
9.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using AICore.Context.Summary;
|
|
using AICore.Context.SummaryMapper;
|
|
using BeWo.Data.Access;
|
|
using BeWo.Data.Entities;
|
|
using BeWo.ServiceUtils.Core;
|
|
using BS.Shared;
|
|
using BS.Shared.Exceptions;
|
|
using BS.Shared.Translation;
|
|
|
|
namespace AICore.Prompt.Factories
|
|
{
|
|
public abstract class BaseAiPromptFactory
|
|
{
|
|
public string CreateSystemInstructions(AiContextType uicontext, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine(getCustomSystemPrompt());
|
|
|
|
sb.AppendLine($"Ich befinde mich aktuell in der \"{Translator.Translate(uicontext)}\" meiner Anwendung.");
|
|
sb.AppendLine("Es ist kein Filter gesetzt.");
|
|
|
|
if (bewoobjects is null || bewoobjects.Count == 0)
|
|
{
|
|
sb.AppendLine("Dir wurden keine Daten bereitgestellt.");
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine("Du erhälst folgendes:");
|
|
var parsed = LoadContext(uicontext, bewoobjects);
|
|
sb.AppendLine(parsed);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private string LoadContext(AiContextType uicontext, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
var context = GetContext(uicontext, bewoobjects);
|
|
|
|
var parsed = Parse(uicontext, null);
|
|
|
|
return parsed;
|
|
}
|
|
|
|
protected virtual string Parse(AiContextType uicontext, Dictionary<TableID, object> data)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
protected BaseContextSummary GetContext(AiContextType uicontext, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
switch (uicontext)
|
|
{
|
|
case AiContextType.SupportConcept:
|
|
case AiContextType.Customer:
|
|
case AiContextType.Person:
|
|
case AiContextType.Employee:
|
|
case AiContextType.Organisation:
|
|
return GetNavigationContext(uicontext, bewoobjects);
|
|
case AiContextType.ServiceRecord:
|
|
return GetServiceRecordsContext(bewoobjects);
|
|
case AiContextType.CustomerSingle:
|
|
case AiContextType.PersonSingle:
|
|
case AiContextType.EmployeeSingle:
|
|
return GetSingleContext(uicontext, bewoobjects);
|
|
default:
|
|
break;
|
|
}
|
|
|
|
throw BeWoNotImplementedException.CreateFromEnum(uicontext);
|
|
}
|
|
|
|
private BaseContextSummary GetNavigationContext(AiContextType uicontext, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
switch (uicontext)
|
|
{
|
|
case AiContextType.SupportConcept:
|
|
|
|
return new SupportConceptNavigationContextSummary(LoadEntities<SupportConcept>(bewoobjects, TableID.SupportConcept));
|
|
case AiContextType.Customer:
|
|
return new CustomerNavigationContextSummary(LoadEntities<Customer>(bewoobjects, TableID.Customer));
|
|
case AiContextType.Person:
|
|
return new PersonNavigationContextSummary(LoadEntities<Person>(bewoobjects, TableID.Person));
|
|
case AiContextType.Employee:
|
|
return new EmployeeNavigationContextSummary(LoadEntities<Employee>(bewoobjects, TableID.Employee));
|
|
case AiContextType.Organisation:
|
|
return new OrganisationNavigationContextSummary(LoadEntities<Organisation>(bewoobjects, TableID.Organisation));
|
|
}
|
|
|
|
throw BeWoNotImplementedException.CreateFromEnum(uicontext);
|
|
}
|
|
|
|
private BaseContextSummary GetServiceRecordsContext(Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
|
|
|
|
var context = new ServiceRecordNavigationContext()
|
|
}
|
|
|
|
private BaseContextSummary GetSingleContext(AiContextType uicontext, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
switch (uicontext)
|
|
{
|
|
case AiContextType.CustomerSingle:
|
|
return new CustomerDetailContextSummary(LoadEntity<Customer>(bewoobjects, TableID.Customer));
|
|
case AiContextType.PersonSingle:
|
|
return new PersonDetailContextSummary(LoadEntity<Person>(bewoobjects, TableID.Person));
|
|
case AiContextType.EmployeeSingle:
|
|
return new EmployeeDetailContextSummary(LoadEntity<Employee>(bewoobjects, TableID.Employee));
|
|
default:
|
|
break;
|
|
}
|
|
|
|
throw BeWoNotImplementedException.CreateFromEnum(uicontext);
|
|
}
|
|
|
|
public Dictionary<TableID, object> LoadContext(int uicontext, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
var result = new Dictionary<TableID, object>();
|
|
|
|
switch (uicontext)
|
|
{
|
|
case 0: return LoadNavigationContext(TableID.SupportConcept, bewoobjects);
|
|
case 1: return LoadNavigationContext(TableID.Customer, bewoobjects);
|
|
case 2: return LoadNavigationContext(TableID.Person, bewoobjects);
|
|
case 3: return LoadNavigationContext(TableID.Employee, bewoobjects);
|
|
case 4: return LoadNavigationContext(TableID.Organisation, bewoobjects);
|
|
//case 5: return "Team Navigation";
|
|
//case 6: return "Dokumente Navigation";
|
|
//case 7: return "Benutzer Navigation";
|
|
//case 8: return "Benutzergruppe Navigation";
|
|
//case 9: return "Bericht Navigation";
|
|
//case 10: return "Planer Navigation";
|
|
//case 11: return "Wohnheim Navigation";
|
|
//case 12: return "Vertretungen Navigation";
|
|
//case 13: return "Kunden-Team Navigation";
|
|
//case 14: return "Terminplanung Navigation";
|
|
//case 15: return "Finanz Navigation";
|
|
//case 16: return "Verwaltung Navigation";
|
|
//case 17: return "KI-Gesprächs Navigation";
|
|
//case 18: return "KI-Gesprächs Popup Navigation";
|
|
case 19: return LoadServiceRecordsContext(bewoobjects);
|
|
case 20: return LoadSingleContext(TableID.Customer, bewoobjects);
|
|
//case 21: return "Person Einzelansicht Navigation";
|
|
//case 22: return "Mitarbeiter Einzelansicht Navigation";
|
|
default:
|
|
throw new NotImplementedException($"UI Kontext {uicontext} ist nicht implementiert.");
|
|
//Alternativ könnte man auch einen Standard-String zurückgeben,
|
|
//z.B. return "Unbekannter UI-Kontext";
|
|
}
|
|
}
|
|
|
|
private Dictionary<TableID, object> LoadNavigationContext(TableID tableid, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
IList<Dictionary<string, object>> entities;
|
|
|
|
switch (tableid)
|
|
{
|
|
case TableID.Person:
|
|
entities = LoadEntities(bewoobjects, tableid, MapperFactory.CompactPersonDC_Person);
|
|
break;
|
|
case TableID.Customer:
|
|
entities = LoadEntities(bewoobjects, tableid, MapperFactory.CompactCustomerDC_Customer);
|
|
break;
|
|
case TableID.Employee:
|
|
entities = LoadEntities(bewoobjects, tableid, MapperFactory.CompactEmployeeDC_Employee);
|
|
break;
|
|
case TableID.SupportConcept:
|
|
entities = LoadEntities(bewoobjects, tableid, MapperFactory.CompactSupportConceptDC_SupportConcept);
|
|
break;
|
|
case TableID.Organisation:
|
|
entities = LoadEntities(bewoobjects, tableid, MapperFactory.CompactOrganisationDC_Organisation, (entity, dc) =>
|
|
{
|
|
if (entity.Address != null)
|
|
{
|
|
dc.AddressLine1 = entity.Address.AddressLine1;
|
|
dc.Street = entity.Address.Street;
|
|
dc.PostalCode = entity.Address.PostalCode;
|
|
dc.Town = entity.Address.Town;
|
|
}
|
|
});
|
|
break;
|
|
default:
|
|
throw new NotImplementedException($"TableID {tableid} not implemented as navigation context");
|
|
}
|
|
|
|
var result = new Dictionary<TableID, List<Dictionary<string, object>>>
|
|
{
|
|
{ tableid, entities.ToList() }
|
|
};
|
|
|
|
return result;
|
|
}
|
|
|
|
private Dictionary<TableID, object> LoadSingleContext(TableID tableid, Dictionary<TableID, long[]> bewoobjects)
|
|
{
|
|
var result = new Dictionary<TableID, Dictionary<string, object>>();
|
|
|
|
Dictionary<string, object> entity;
|
|
|
|
switch (tableid)
|
|
{
|
|
case TableID.Person:
|
|
entity = LoadEntity(bewoobjects, tableid, MapperFactory.PersonDC_Person);
|
|
break;
|
|
case TableID.Customer:
|
|
entity = LoadEntity(bewoobjects, tableid, MapperFactory.CustomerDC_Customer);
|
|
break;
|
|
case TableID.Employee:
|
|
entity = LoadEntity(bewoobjects, tableid, MapperFactory.EmployeeDC_Employee);
|
|
break;
|
|
case TableID.SupportConcept:
|
|
entity = LoadEntity(bewoobjects, tableid, MapperFactory.SupportConceptDC_SupportConcept);
|
|
break;
|
|
//case TableID.Organisation:
|
|
// entity = LoadEntity(bewoobjects, tableid, MapperFactory.OrganisationDC_OrganisationMapper, (entity, dc) =>
|
|
// {
|
|
// if (entity.Address != null)
|
|
// {
|
|
// dc.AddressLine1 = entity.Address.AddressLine1;
|
|
// dc.Street = entity.Address.Street;
|
|
// dc.PostalCode = entity.Address.PostalCode;
|
|
// dc.Town = entity.Address.Town;
|
|
// }
|
|
// });
|
|
// break;
|
|
default:
|
|
throw new NotImplementedException($"TableID {tableid} not implemented as navigation context");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private TRet LoadProperty<TEntity, TRet>
|
|
(Dictionary<TableID, long[]> bewoobjects, TableID tid, Func<TEntity, TRet> getPropertyFunc)
|
|
where TEntity : BeWoEntityBase, new()
|
|
{
|
|
if (!bewoobjects.TryGetValue(tid, out long[] oids) || (oids?.Length ?? 0) == 0)
|
|
return default;
|
|
|
|
var entity = DAOFactory.GenericDAO.GetByID<TEntity>(oids[0]);
|
|
var property = getPropertyFunc(entity);
|
|
|
|
return property;
|
|
}
|
|
|
|
private TEntity LoadEntity<TEntity>
|
|
(Dictionary<TableID, long[]> bewoobjects, TableID tid)
|
|
where TEntity : BeWoEntityBase, new()
|
|
{
|
|
if (!bewoobjects.TryGetValue(tid, out long[] oids) || (oids?.Length ?? 0) == 0)
|
|
return null;
|
|
|
|
var entity = DAOFactory.GenericDAO.GetByID<TEntity>(oids[0]);
|
|
|
|
return entity;
|
|
}
|
|
|
|
private List<TEntity> LoadEntities<TEntity>
|
|
(Dictionary<TableID, long[]> bewoobjects, TableID tid)
|
|
where TEntity : BeWoEntityBase, new()
|
|
{
|
|
if (!bewoobjects.TryGetValue(tid, out long[] oids) || (oids?.Length ?? 0) == 0)
|
|
return null;
|
|
|
|
var entities = DAOFactory.GenericDAO.GetByIDs<TEntity>(oids);
|
|
|
|
//if(afterLoad != null)
|
|
//{
|
|
// foreach( var entity in entities)
|
|
// {
|
|
// afterLoad(entity);
|
|
// }
|
|
//}
|
|
|
|
return entities;
|
|
}
|
|
|
|
private string getSystemPrompt(int i)
|
|
{
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
var resourceName = $"AICore.Prompt.SystemPrompts.system_prompt{i}.txt";
|
|
|
|
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
|
|
using (StreamReader reader = new StreamReader(stream))
|
|
{
|
|
string result = reader.ReadToEnd();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
private string getCustomSystemPrompt()
|
|
{
|
|
var path = MergedConfig.GetSetting("AiSystemPromptPath");
|
|
|
|
return File.ReadAllText(path);
|
|
}
|
|
}
|
|
}
|