using BeWo.Data.Entities; using BeWo.Service.DCEntityMapper; using BS.Shared.Core; using BS.Shared.Core.Facade; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Feature.AI; using BS.Shared.DataContracts.MikePHPContracts; using DevExpress.DataAccess.Native.EntityFramework; using Newtonsoft.Json; using NHibernate.SqlTypes; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Security.Policy; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace BeWo.Service.ServiceProxy { public class OpenWebUIFacade : WebClientFacade { public IEnumerable GetAiModelle() { var key = getKey(); var sub_url = "api/models"; var dictList = SendGet>>(sub_url, key); if (dictList.TryGetValue("data", out var models)) return MapperFactory.AiModel.MapToNewEntities(models); return null; } public string SendAiMessageRequest(AiConversationDC conversation, AiConfigDC config, out string model, out int? duration) { duration = null; model = null; var key = getKey(); var sub_url = "api/chat/completions"; var payload = new { model = config.SelectedModel.ModelName, messages = conversation.Messages.Select(m => new { role = m.Role.ToString().ToLower(), content = m.Message }) }; //var requestData = JsonConvert.SerializeObject(payload); var response = SendPostUploadString(sub_url, payload, key); var response_format = new { id = string.Empty, model = string.Empty, choices = new[] { new { index = 0, message = new { content = string.Empty, role = string.Empty } } }, usage = new { total_duration = 0L, total_tokens = 68L } }; var json = JsonConvert.DeserializeAnonymousType(response, response_format); foreach (var choice in json.choices) { if (choice?.message is object && !string.IsNullOrEmpty(choice.message.content)) { var total_ns = json.usage.total_duration; var total_ms = total_ns / 1000000; duration = (int)total_ms; model = json.model; var result = Utils.ConvertToISO88591(choice.message.content); return result; } } return "Keine Antwort"; } protected override string getUrl() { var url = ConfigurationManager.AppSettings["OpenWebUIUrl"]; if (string.IsNullOrEmpty(url)) throw new InvalidOperationException("OpenWebUIUrl is missing"); return url; } protected override string getKey() { var key = Core.MergedConfig.GetSecretSetting(WebSecretConfigSetting.OpenWebUIKey); if (string.IsNullOrEmpty(key)) throw new InvalidOperationException("OpenWebUIKey is missing"); return key; } } }