This commit is contained in:
2025-09-12 14:58:41 +02:00
parent b464a6e56c
commit 09cc0dec59
30 changed files with 314 additions and 39 deletions

View File

@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
namespace AICore.Context.Entry
{
internal class CustomerDetailContextEntry : DetailContextEntry<CustomerDC>
internal class CustomerDetailContextEntry : DetailContextEntry
{
[JsonPropertyName("Klient-ID")]
public long CustomerOid { get; set; }

View File

@@ -1,6 +1,8 @@
namespace AICore.Context.Entry
using BS.Shared.DataContracts;
namespace AICore.Context.Entry
{
internal abstract class DetailContextEntry<T> : BaseContextEntry
internal abstract class DetailContextEntry : BaseContextEntry
{
}

View File

@@ -2,7 +2,7 @@
namespace AICore.Context.Entry
{
internal class EmployeeDetailContextEntry : DetailContextEntry<EmployeeDC>
internal class EmployeeDetailContextEntry : DetailContextEntry
{
}

View File

@@ -2,7 +2,7 @@
namespace AICore.Context.Entry
{
internal class OrganisationDetailContextEntry : DetailContextEntry<OrganisationDC>
internal class OrganisationDetailContextEntry : DetailContextEntry
{
}

View File

@@ -2,7 +2,7 @@
namespace AICore.Context.Entry
{
internal class PersonDetailContextEntry : DetailContextEntry<PersonDC>
internal class PersonDetailContextEntry : DetailContextEntry
{
}

View File

@@ -2,7 +2,7 @@
namespace AICore.Context.Entry
{
internal class SupportConceptDetailContextEntry : DetailContextEntry<SupportConceptDC>
internal class SupportConceptDetailContextEntry : DetailContextEntry
{
}

View File

@@ -5,7 +5,7 @@ using System.Text.Json.Serialization;
namespace AICore.Context.Entry.Navigation
{
internal class CustomerNavigationContextEntry : NavigationContextEntry<CompactCustomerDC>
internal class CustomerNavigationContextEntry : NavigationContextEntry
{
[JsonPropertyName("Adresse")]
public string AddressString { get; set; }

View File

@@ -3,7 +3,7 @@ using System.Text.Json.Serialization;
namespace AICore.Context.Entry.Navigation
{
internal class EmployeeNavigationContextEntry : NavigationContextEntry<CompactEmployeeDC>
internal class EmployeeNavigationContextEntry : NavigationContextEntry
{
[JsonPropertyName("Mitarbeiter-ID")]
public long EmployeeOid { get; set; }

View File

@@ -1,6 +1,8 @@
namespace AICore.Context.Entry.Navigation
using BS.Shared.DataContracts;
namespace AICore.Context.Entry.Navigation
{
internal abstract class NavigationContextEntry<T> : BaseContextEntry
internal abstract class NavigationContextEntry : BaseContextEntry
{
}

View File

@@ -3,7 +3,7 @@ using System.Text.Json.Serialization;
namespace AICore.Context.Entry.Navigation
{
internal class OrganisationNavigationContextEntry : NavigationContextEntry<CompactOrganisationDC>
internal class OrganisationNavigationContextEntry : NavigationContextEntry
{
[JsonPropertyName("Organisation-ID")]
public long OrganisationOid { get; set; }

View File

@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
namespace AICore.Context.Entry.Navigation
{
internal class PersonNavigationContextEntry : NavigationContextEntry<CompactPersonDC>
internal class PersonNavigationContextEntry : NavigationContextEntry
{
[JsonPropertyName("Adresse")]
public string AddressString { get; set; }

View File

@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
namespace AICore.Context.Entry.Navigation
{
internal class ServiceRecordContextEntry : NavigationContextEntry<ServiceRecordDC>
internal class ServiceRecordContextEntry : NavigationContextEntry
{
[JsonPropertyName("Zeiteintrag-ID")]
public long ServiceRecordOid { get; set; }

View File

@@ -5,7 +5,7 @@ using System.Text.Json.Serialization;
namespace AICore.Context.Entry.Navigation
{
internal class SupportConceptNavigationContextEntry : NavigationContextEntry<CompactSupportConceptDC>
internal class SupportConceptNavigationContextEntry : NavigationContextEntry
{
[JsonPropertyName("Hilfeplan-ID")]
public long SupportConceptOid { get; set; }

View File

@@ -1,9 +1,9 @@
using AICore.Context.Entry;
using BS.Shared.DataContracts;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Detail
{
internal class CustomerDetailContextSummary : DetailContextSummary<CustomerDetailContextEntry, CustomerDC>
internal class CustomerDetailContextSummary : DetailContextSummary<CustomerDetailContextEntry>
{
internal CustomerDetailContextSummary(CustomerDetailContextEntry customer) : base(customer) { }
}

View File

@@ -1,8 +1,9 @@
using AICore.Context.Entry;
using BS.Shared.DataContracts;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Detail
{
internal abstract class DetailContextSummary<TEntry, TEntity> : BaseContextSummary where TEntry : DetailContextEntry<TEntity>
internal abstract class DetailContextSummary<TEntry> : BaseContextSummary where TEntry : DetailContextEntry
{
internal TEntry DataContext { get; set; }

View File

@@ -1,9 +1,9 @@
using AICore.Context.Entry;
using BS.Shared.DataContracts;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Detail
{
internal class EmployeeDetailContextSummary : DetailContextSummary<EmployeeDetailContextEntry, EmployeeDC>
internal class EmployeeDetailContextSummary : DetailContextSummary<EmployeeDetailContextEntry>
{
internal EmployeeDetailContextSummary(EmployeeDetailContextEntry employee) : base(employee) { }
}

View File

@@ -1,9 +1,9 @@
using AICore.Context.Entry;
using BS.Shared.DataContracts;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Detail
{
internal class OrganisationDetailContextSummary : DetailContextSummary<OrganisationDetailContextEntry, OrganisationDC>
internal class OrganisationDetailContextSummary : DetailContextSummary<OrganisationDetailContextEntry>
{
internal OrganisationDetailContextSummary(OrganisationDetailContextEntry organisation) : base(organisation) { }
}

View File

@@ -1,9 +1,9 @@
using AICore.Context.Entry;
using BS.Shared.DataContracts;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Detail
{
internal class PersonDetailContextSummary : DetailContextSummary<PersonDetailContextEntry, PersonDC>
internal class PersonDetailContextSummary : DetailContextSummary<PersonDetailContextEntry>
{
internal PersonDetailContextSummary(PersonDetailContextEntry person) : base(person) { }
}

View File

@@ -1,9 +1,9 @@
using AICore.Context.Entry;
using BS.Shared.DataContracts;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Detail
{
internal class SupportConceptDetailContextSummary : DetailContextSummary<SupportConceptDetailContextEntry, SupportConceptDC>
internal class SupportConceptDetailContextSummary : DetailContextSummary<SupportConceptDetailContextEntry>
{
internal SupportConceptDetailContextSummary(SupportConceptDetailContextEntry supportConcept) : base(supportConcept) { }
}

View File

@@ -2,9 +2,9 @@
using AICore.Context.Entry.Navigation;
using BS.Shared.DataContracts.Compact;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Navigation
{
internal class CustomerNavigationContextSummary : NavigationContextSummary<CustomerNavigationContextEntry, CompactCustomerDC>
internal class CustomerNavigationContextSummary : NavigationContextSummary<CustomerNavigationContextEntry>
{
internal CustomerNavigationContextSummary(IEnumerable<CustomerNavigationContextEntry> customers) : base(customers) { }
}

View File

@@ -2,9 +2,9 @@
using AICore.Context.Entry.Navigation;
using BS.Shared.DataContracts.Compact;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Navigation
{
internal class EmployeeNavigationContextSummary : NavigationContextSummary<EmployeeNavigationContextEntry, CompactEmployeeDC>
internal class EmployeeNavigationContextSummary : NavigationContextSummary<EmployeeNavigationContextEntry>
{
internal EmployeeNavigationContextSummary(IEnumerable<EmployeeNavigationContextEntry> employee) : base(employee) { }
}

View File

@@ -0,0 +1,17 @@
using System.Collections.Generic;
using AICore.Context.Entry.Navigation;
using BS.Shared.DataContracts;
namespace AICore.Context.Summary.Navigation
{
internal abstract class NavigationContextSummary<TEntry> : BaseContextSummary where TEntry : NavigationContextEntry
{
internal IEnumerable<TEntry> DataContext { get; set; }
protected NavigationContextSummary(IEnumerable<TEntry> dataContext)
{
DataContext = dataContext;
Context = dataContext;
}
}
}

View File

@@ -2,9 +2,9 @@
using AICore.Context.Entry.Navigation;
using BS.Shared.DataContracts.Compact;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Navigation
{
internal class OrganisationNavigationContextSummary : NavigationContextSummary<OrganisationNavigationContextEntry, CompactOrganisationDC>
internal class OrganisationNavigationContextSummary : NavigationContextSummary<OrganisationNavigationContextEntry>
{
internal OrganisationNavigationContextSummary(IEnumerable<OrganisationNavigationContextEntry> organisation) : base(organisation) { }
}

View File

@@ -2,9 +2,9 @@
using AICore.Context.Entry.Navigation;
using BS.Shared.DataContracts.Compact;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Navigation
{
internal class PersonNavigationContextSummary : NavigationContextSummary<PersonNavigationContextEntry, CompactPersonDC>
internal class PersonNavigationContextSummary : NavigationContextSummary<PersonNavigationContextEntry>
{
internal PersonNavigationContextSummary(IEnumerable<PersonNavigationContextEntry> person) : base(person) { }
}

View File

@@ -2,9 +2,9 @@
using BS.Shared.DataContracts;
using System.Collections.Generic;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Navigation
{
internal class ServiceRecordNavigationContextSummary : NavigationContextSummary<ServiceRecordContextEntry, ServiceRecordDC>
internal class ServiceRecordNavigationContextSummary : NavigationContextSummary<ServiceRecordContextEntry>
{
internal CustomerNavigationContextEntry Customer { get; set; }
internal string CostBearer { get; set; }

View File

@@ -2,9 +2,9 @@
using AICore.Context.Entry.Navigation;
using BS.Shared.DataContracts.Compact;
namespace AICore.Context.Summary
namespace AICore.Context.Summary.Navigation
{
internal class SupportConceptNavigationContextSummary : NavigationContextSummary<SupportConceptNavigationContextEntry, CompactSupportConceptDC>
internal class SupportConceptNavigationContextSummary : NavigationContextSummary<SupportConceptNavigationContextEntry>
{
internal SupportConceptNavigationContextSummary(IEnumerable<SupportConceptNavigationContextEntry> supportConcept) : base(supportConcept) { }
}

View File

@@ -1,5 +1,6 @@
using AICore.Context.Entry;
using AICore.Context.Summary;
using AICore.Context.Summary.Navigation;
using BS.Shared;
using BS.Shared.Exceptions;
using System;

View File

@@ -0,0 +1,98 @@
using AICore.Context.Entry;
using AICore.Context.Entry.Navigation;
using AICore.Context.Summary;
using AICore.Context.SummaryMapper;
using AICore.Context.SummaryParser;
using BeWo.ServiceUtils.Core;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Exceptions;
using BS.Shared.Translation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
namespace AICore.Prompt.Core
{
public class AiPromptBuilder
{
public string CreateSystemInstructions(AiContextType uicontext, BaseContextSummary context, AiDataContextType aiDataContextType)
{
var sb = new StringBuilder();
sb.AppendLine(getCustomSystemPrompt());
sb.AppendLine("### **Allgemein**");
sb.AppendLine($"- Aktuelle Zeit: {DateTime.Now}");
sb.AppendLine();
sb.AppendLine("### **Ansicht**");
sb.AppendLine($"- Ich befinde mich aktuell in der \"{EnumTranslations.Ai2Translation[uicontext]}\" meiner Anwendung.");
sb.AppendLine($"- Es ist kein Filter gesetzt.");
sb.AppendLine();
sb.AppendLine("### **Daten**");
if (context?.Context is null)
{
sb.AppendLine("Dir wurden keine Daten bereitgestellt.");
}
else
{
sb.AppendLine("Du erhälst folgende Daten:");
var parser = GetParser(aiDataContextType);
var parsed = parser.Parse(uicontext, context);
sb.AppendLine(parsed);
}
return sb.ToString();
}
private BaseContextSummaryParser GetParser(AiDataContextType aiDataContextType)
{
switch (aiDataContextType)
{
case AiDataContextType.json:
return new JsonContextSummaryParser();
case AiDataContextType.csv:
return new CsvContextSummaryParser();
case AiDataContextType.tsv:
return new CsvContextSummaryParser("\t");
case AiDataContextType.csv2:
return new CsvContextSummaryParser(";", true);
case AiDataContextType.tsv2:
return new CsvContextSummaryParser("\t", true);
default:
break;
}
throw BeWoNotImplementedException.CreateFromEnum(aiDataContextType);
}
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);
}
}
}

View File

@@ -562,6 +562,7 @@
<Compile Include="ServiceUtils\DistanceCalculator\GoogleDistanceMatrixAPI.cs" />
<Compile Include="ServiceUtils\DistanceCalculator\IDistanceAPI.cs" />
<Compile Include="ServiceUtils\DistanceCalculator\OpenrouteServiceAPI.cs" />
<Compile Include="ServiceUtils\AiContextLoader.cs" />
<Compile Include="ServiceUtils\ServiceTranslator.cs" />
<Compile Include="Core\ServiceHelper.cs" />
<Compile Include="Core\ServiceValidator.cs" />

View File

@@ -0,0 +1,153 @@
using AICore.Context.Summary;
using AICore.Context.SummaryMapper;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceImplementations;
using BS.Shared.DataContracts.Compact;
using BS.Shared.DataContracts;
using BS.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeWo.Service.ServiceUtils
{
public static class AiContextLoader
{
public static BaseContextSummary LoadContext(AiContextType uicontext, Dictionary<TableID, long[]> context_reference)
{
switch (uicontext)
{
case AiContextType.SupportConceptNavigation:
case AiContextType.CustomerNavigation:
case AiContextType.PersonNavigation:
case AiContextType.EmployeeNavigation:
case AiContextType.OrganisationNavigation:
return loadNavigationContext(uicontext, context_reference);
case AiContextType.CustomerDetail:
case AiContextType.PersonDetail:
case AiContextType.EmployeeDetail:
case AiContextType.OrganisationDetail:
case AiContextType.SupportConceptDetail:
return loadDetailContext(uicontext, context_reference);
case AiContextType.ServiceRecord:
return loadServiceRecordContext(context_reference);
default:
break;
}
throw new NotImplementedException();
}
private static BaseContextSummary loadNavigationContext(AiContextType uicontext, Dictionary<TableID, long[]> context_reference)
{
switch (uicontext)
{
case AiContextType.SupportConceptNavigation:
var list = new CustomerServiceImp().GetAllAuthorizedCompactSupportConcepts();
var filter = list.Where(x => oidsContainsOid(context_reference[TableID.SupportConcept], x.SupportConceptOid));
return AiContextSummaryFactory.CreateSupportConceptNavigationContextSummary(filter);
case AiContextType.CustomerNavigation:
var list2 = new CustomerServiceImp().GetAllAuthorizedCompactCustomers();
var filter2 = list2.Where(x => oidsContainsOid(context_reference[TableID.Customer], x.CustomerOid));
return AiContextSummaryFactory.CreateCustomerNavigationContextSummary(filter2);
case AiContextType.PersonNavigation:
var list3 = new CustomerServiceImp().GetAllAuthorizedCompactPersons();
var filter3 = list3.Where(x => oidsContainsOid(context_reference[TableID.Person], x.PersonOid));
return AiContextSummaryFactory.CreatePersonNavigationContextSummary(filter3);
case AiContextType.EmployeeNavigation:
var list4 = new EmployeeServiceImp().GetAllCompactEmployees();
var filter4 = list4.Where(x => oidsContainsOid(context_reference[TableID.Employee], x.EmployeeOid));
return AiContextSummaryFactory.CreateEmployeeNavigationContextSummary(filter4);
case AiContextType.OrganisationNavigation:
var list5 = new CustomerServiceImp().GetAllOrganisationsCompact();
var filter5 = list5.Where(x => oidsContainsOid(context_reference[TableID.Organisation], x.OrganisationOid));
return AiContextSummaryFactory.CreateOrganisationNavigationContextSummary(filter5);
}
throw new NotImplementedException();
}
private static BaseContextSummary loadDetailContext(AiContextType uicontext, Dictionary<TableID, long[]> context_reference)
{
switch (uicontext)
{
case AiContextType.CustomerDetail:
var oids = context_reference[TableID.Customer];
var dc = new CustomerServiceImp().LoadCustomer(oids[0]);
return AiContextSummaryFactory.CreateCustomerDetailContextSummary(dc);
case AiContextType.PersonDetail:
var oids2 = context_reference[TableID.Person];
var dc2 = new CustomerServiceImp().LoadPerson(oids2[0]);
return AiContextSummaryFactory.CreatePersonDetailContextSummary(dc2);
case AiContextType.EmployeeDetail:
var oids3 = context_reference[TableID.Employee];
var dc3 = new EmployeeServiceImp().LoadEmployee(oids3[0]);
return AiContextSummaryFactory.CreateEmployeeDetailContextSummary(dc3);
case AiContextType.OrganisationDetail:
var oids4 = context_reference[TableID.Organisation];
var dc4 = new CustomerServiceImp().LoadOrganisation(oids4[0]);
return AiContextSummaryFactory.CreateOrganisationDetailContextSummary(dc4);
case AiContextType.SupportConceptDetail:
var oids5 = context_reference[TableID.SupportConcept];
var dc5 = new CustomerServiceImp().LoadSupportConcept(oids5[0]);
return AiContextSummaryFactory.CreateSupportConceptDetailContextSummary(dc5);
}
throw new NotImplementedException();
}
private static BaseContextSummary loadServiceRecordContext(Dictionary<TableID, long[]> context_reference)
{
var customer = loadCompactCustomerDC(context_reference);
var serviceRecords = loadServiceRecordDCs(context_reference);
var supportConcept = loadSupportConceptDC(context_reference);
return AiContextSummaryFactory.CreateServiceRecordNavigationContextSummary(serviceRecords, customer, supportConcept);
}
private static bool oidsContainsOid(long[] oids, long oid)
{
if (oids == null || oids.Length == 0)
return false;
return oids.Contains(oid);
}
private static CompactCustomerDC loadCompactCustomerDC(Dictionary<TableID, long[]> context_reference)
=> LoadDataContact<CompactCustomerDC_Customer, Customer, CompactCustomerDC>(MapperFactory.CompactCustomerDC_Customer, context_reference);
private static CompactSupportConceptDC loadSupportConceptDC(Dictionary<TableID, long[]> context_reference)
=> LoadDataContact<CompactSupportConceptDC_SupportConcept, SupportConcept, CompactSupportConceptDC>(MapperFactory.CompactSupportConceptDC_SupportConcept, context_reference);
private static IEnumerable<ServiceRecordDC> loadServiceRecordDCs(Dictionary<TableID, long[]> context_reference)
=> LoadDataContacts<ServiceRecordDC_ServiceRecord, ServiceRecord, ServiceRecordDC>(MapperFactory.ServiceRecordDC_ServiceRecord, context_reference);
private static TDC LoadDataContact<TMapper, TEntity, TDC>(TMapper mapper, Dictionary<TableID, long[]> context_reference)
where TMapper : AbstractIDCEntityMapper<TEntity, TDC>
where TEntity : BeWoEntityBase, new()
where TDC : class, new()
{
var example = new TEntity();
var tid = example.Tid;
var oid = context_reference[tid][0];
var entity = DAOFactory.GenericDAO.LoadByID<TEntity>(oid);
var dc = mapper.MapToNewDC(entity);
return dc;
}
private static IEnumerable<TDC> LoadDataContacts<TMapper, TEntity, TDC>(TMapper mapper, Dictionary<TableID, long[]> context_reference)
where TMapper : AbstractIDCEntityMapper<TEntity, TDC>
where TEntity : BeWoEntityBase, new()
where TDC : class, new()
{
var example = new TEntity();
var tid = example.Tid;
var oids = context_reference[tid];
var entities = DAOFactory.GenericDAO.LoadByIDs<TEntity>(oids);
var dcs = mapper.MapToNewDCs(entities);
return dcs;
}
}
}