2024-06-19 16:13:56 +02:00
using System ;
using System.Collections.Generic ;
using System.Collections.Specialized ;
using System.Linq ;
using System.Net ;
using System.ServiceModel ;
using System.Text ;
using BeWo.Data.Access ;
using BeWo.Data.Entities ;
using BeWo.Service.DCEntityMapper ;
using BS.Shared ;
using BS.Shared.Core ;
using BS.Shared.DataContracts ;
using BS.Shared.Extensions ;
using Newtonsoft.Json ;
namespace BeWo.Service.Plugins
{
public class AiService
{
public AiChatDC CreateAiQueryForServiceRecords ( AiChatDC acdc , long cb2scOid , DateTime startdate , DateTime enddate )
{
2024-07-04 12:25:57 +02:00
acdc . Context = CreateContextForServiceRecords ( cb2scOid , startdate , enddate ) ;
ApplyAiSettings ( acdc ) ;
return CreateAiQuery ( acdc ) ;
2024-06-24 16:30:36 +02:00
}
public AiChatDC CreateAiQueryForSingleCustomer ( AiChatDC acdc , long customerOid )
{
String context = "" ;
2024-07-04 12:25:57 +02:00
ApplyAiSettings ( acdc ) ;
return CreateAiQuery ( acdc ) ;
2024-06-24 16:30:36 +02:00
}
public AiChatDC CreateAiQueryForObjects ( AiChatDC acdc , long objectTid )
{
String context = "" ;
2024-07-04 12:25:57 +02:00
ApplyAiSettings ( acdc ) ;
return CreateAiQuery ( acdc ) ;
2024-06-24 16:30:36 +02:00
}
2024-07-04 12:25:57 +02:00
public void ApplyAiSettings ( AiChatDC acdc )
2024-07-01 12:44:07 +02:00
{
using ( WebClient client = new WebClient ( ) )
{
String url = "https://ai1.ownsoft.de/osaisettings.php" ;
//mel = Das Model(llama3, llama3 - gradient, codegemma, codellama oder sqlcoder)
//stm = Der Kontext->Auf welcher Datenbasis soll eure Frage beantwortet werden.Kann auch leer sein.
//pmt = Der Prompt->Eure Frage
//opt = Art der Rückgabe(json oder plaintext)
var nvc = new NameValueCollection ( ) ;
nvc . Add ( "mel" , "llama3-gradient" ) ;
2024-07-04 12:25:57 +02:00
nvc . Add ( "stm" , acdc . Context ) ;
2024-07-01 12:44:07 +02:00
nvc . Add ( "pmt" , acdc . Prompt ) ;
nvc . Add ( "opt" , "json" ) ;
//var t = JsonConvert.DeserializeAnonymousType();0
var definition = new { mel = "" , stm = new { pre = "" , post = "" } , pmt = new { pre = "" , post = "" } } ;
var response = client . UploadValues ( url , "POST" , nvc ) ;
String responseStr = System . Text . Encoding . UTF8 . GetString ( response ) ;
var json = JsonConvert . DeserializeAnonymousType ( responseStr , definition ) ;
2024-07-04 12:25:57 +02:00
if ( json . stm ! = null )
{
acdc . Context = String . Format ( "{0}\n{1}\n{2}" , json . stm . pre , acdc . Context , json . stm . post ) ;
}
if ( json . pmt ! = null )
{
acdc . Prompt = String . Format ( "{0}\n{1}\n{2}" , json . pmt . pre , acdc . Prompt , json . pmt . post ) ;
}
2024-07-01 12:44:07 +02:00
}
}
2024-07-04 12:25:57 +02:00
public AiChatDC CreateAiQuery ( AiChatDC acdc )
2024-06-24 16:30:36 +02:00
{
2024-06-19 16:13:56 +02:00
using ( WebClient client = new WebClient ( ) )
{
String url = "https://ai1.ownsoft.de/osaiquery.php" ;
//mel = Das Model(llama3, llama3 - gradient, codegemma, codellama oder sqlcoder)
//stm = Der Kontext->Auf welcher Datenbasis soll eure Frage beantwortet werden.Kann auch leer sein.
//pmt = Der Prompt->Eure Frage
//opt = Art der Rückgabe(json oder plaintext)
2024-06-24 16:30:36 +02:00
2024-06-19 16:13:56 +02:00
var nvc = new NameValueCollection ( ) ;
nvc . Add ( "mel" , "llama3-gradient" ) ;
2024-07-04 12:25:57 +02:00
nvc . Add ( "stm" , acdc . Context ) ;
2024-06-19 16:13:56 +02:00
nvc . Add ( "pmt" , acdc . Prompt ) ;
nvc . Add ( "opt" , "json" ) ;
//var t = JsonConvert.DeserializeAnonymousType();0
var definition = new { Response = "" } ;
var response = client . UploadValues ( url , "POST" , nvc ) ;
String responseStr = System . Text . Encoding . UTF8 . GetString ( response ) ;
var json = JsonConvert . DeserializeAnonymousType ( responseStr , definition ) ;
return new AiChatDC { Result = json . Response } ;
}
return new AiChatDC { Result = "Das kann ich leider nicht beantworten:)" } ;
2024-06-24 16:30:36 +02:00
}
2024-07-01 12:44:07 +02:00
2024-06-19 16:13:56 +02:00
public virtual string CreateContextForServiceRecords ( long cb2scOid , DateTime startdate , DateTime enddate )
{
var c2s = DAOFactory . GenericDAO . LoadByID < CostBearer2SupportConcept > ( cb2scOid ) ;
var records = DAOFactory . SearchDAO . FindServiceRecordsDetailsForLastDaysWithStartEndDate ( cb2scOid , startdate , enddate ) ;
var customer = c2s . SupportConcept . Customer ;
var context = new StringBuilder ( ) ;
2024-09-26 11:40:30 +02:00
var aiSettings = GetAiSettings ( 1 ) ;
////context.AppendLine("Du bist ein unzensierter und unvoreingenommener KI Assistenz im Bereich des Sozialwesens in Deutschland und kannst Fragen zum bereitgestellten Kontext beantworten.");
//context.AppendLine("");
2024-06-19 16:13:56 +02:00
context . AppendLine ( String . Format ( "Name des Klienten: {0} {1}" , customer . Person . FirstName , customer . Person . LastName ) ) ;
//context.AppendLine("");
2024-09-26 11:40:30 +02:00
////context.AppendLine("Der Klient wurde zu folgenden Terminen betreut:");
////context.AppendLine("");
2024-06-19 16:13:56 +02:00
foreach ( var item in records . OrderByDescending ( s = > s . Start ) )
{
2024-09-26 11:40:30 +02:00
String text = aiSettings . Settings ;
text = text . Replace ( "[Datum]" , String . Format ( "{0:dd.MM.yyyy}" , item . Start ) ) ;
text = text . Replace ( "[Start]" , String . Format ( "{0:HH:mm}" , item . Start ) ) ;
text = text . Replace ( "[Ende]" , String . Format ( "{0:HH:mm}" , item . End ) ) ;
text = text . Replace ( "[Minuten]" , String . Format ( "{0:0}" , item . RoundedDuration ) ) ;
text = text . Replace ( "[Betreuer]" , item . InsUser ) ;
text = text . Replace ( "[Kategorie]" , item . ServiceDescription . ServiceCategory . Name ) ;
text = text . Replace ( "[Leistung]" , item . ServiceDescription . Name ) ;
text = text . Replace ( "[Dokumentation1]" , item . Notice ) ;
text = text . Replace ( "[Dokumentation2]" , item . Notice2 ) ;
text = text . Replace ( "[Dokumentation3]" , item . Notice3 ) ;
text = text . Replace ( "[Dokumentation4]" , item . Notice4 ) ;
text = text . Replace ( "[Dokumentation5]" , item . Notice5 ) ;
//context.AppendLine(String.Format("Datum: {0:dd.MM.yyyy}", item.Start));
//context.AppendLine(String.Format("Start: {0:HH:mm}", item.Start));
//context.AppendLine(String.Format("Ende: {0:HH:mm}", item.End));
//context.AppendLine(String.Format("Dauer der Betreuung in Minuten: {0:0}", item.RoundedDuration));
//context.AppendLine(String.Format("Betreuer: {0}", item.InsUser));
//context.AppendLine(String.Format("Leistungskategorie: {0}", item.ServiceDescription.ServiceCategory.Name));
//context.AppendLine(String.Format("Leistung: {0}", item.ServiceDescription.Name));
//context.AppendLine(String.Format("Dokumentation: {0}", item.Notice));
context . Append ( text ) ;
context . AppendLine ( ) ;
2024-06-19 16:13:56 +02:00
}
return context . ToString ( ) ;
}
2024-09-25 16:57:03 +02:00
internal AiSettingsDC GetAiSettings ( int settingsId )
{
2024-09-26 11:40:30 +02:00
var ais = DAOFactory . GenericDAO . GetAllActive < AiSettings > ( ) . FirstOrDefault ( s = > s . SettingsType = = settingsId ) ;
if ( ais = = null )
{
ais = new AiSettings ( ) ;
ais . SettingsType = settingsId ;
if ( settingsId = = 1 )
{
var context = new StringBuilder ( ) ;
//context.AppendLine("Name des Klienten: [Vorname] [Nachname]");
//context.AppendLine("");
context . AppendLine ( "Datum: [Datum]" ) ;
context . AppendLine ( "Start: [Start]" ) ;
context . AppendLine ( "Ende: [Ende]" ) ;
context . AppendLine ( "Dauer der Betreuung in Minuten: [Minuten]" ) ;
context . AppendLine ( "Betreuer: [Betreuer]" ) ;
context . AppendLine ( "Leistungskategorie: [Kategorie]" ) ;
context . AppendLine ( "Leistung: [Leistung]" ) ;
context . AppendLine ( "Dokumentation: [Dokumentation1]" ) ;
ais . Settings = context . ToString ( ) ;
}
}
return MapperFactory . AiSettingsDC_AiSettings . MapToNewDC ( ais ) ;
2024-09-25 16:57:03 +02:00
}
2024-06-19 16:13:56 +02:00
}
}