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) { acdc.Context = CreateContextForServiceRecords(cb2scOid, startdate, enddate); ApplyAiSettings(acdc); return CreateAiQuery(acdc); } public AiChatDC CreateAiQueryForSingleCustomer(AiChatDC acdc, long customerOid) { String context = ""; ApplyAiSettings(acdc); return CreateAiQuery(acdc); } public AiChatDC CreateAiQueryForObjects(AiChatDC acdc, long objectTid) { String context = ""; ApplyAiSettings(acdc); return CreateAiQuery(acdc); } public void ApplyAiSettings(AiChatDC acdc) { 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"); nvc.Add("stm", acdc.Context); 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); 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); } } } public AiChatDC CreateAiQuery(AiChatDC acdc) { 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) var nvc = new NameValueCollection(); nvc.Add("mel", "llama3-gradient"); nvc.Add("stm", acdc.Context); 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:)" }; } public virtual string CreateContextForServiceRecords(long cb2scOid, DateTime startdate, DateTime enddate) { var c2s = DAOFactory.GenericDAO.LoadByID(cb2scOid); var records = DAOFactory.SearchDAO.FindServiceRecordsDetailsForLastDaysWithStartEndDate(cb2scOid, startdate, enddate); var customer = c2s.SupportConcept.Customer; var context = new StringBuilder(); 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(""); context.AppendLine(String.Format("Name des Klienten: {0} {1}", customer.Person.FirstName, customer.Person.LastName)); //context.AppendLine(""); ////context.AppendLine("Der Klient wurde zu folgenden Terminen betreut:"); ////context.AppendLine(""); foreach (var item in records.OrderByDescending(s => s.Start)) { 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(); } return context.ToString(); } internal AiSettingsDC GetAiSettings(int settingsId) { var ais = DAOFactory.GenericDAO.GetAllActive().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); } } }