104 lines
2.4 KiB
C#
104 lines
2.4 KiB
C#
|
|
using BeWo.Data.Entities;
|
|||
|
|
using BeWo.Service.DCEntityMapper;
|
|||
|
|
using BS.Shared.Core;
|
|||
|
|
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<AiModel> GetAiModelle()
|
|||
|
|
{
|
|||
|
|
var key = getKey();
|
|||
|
|
var sub_url = "api/models";
|
|||
|
|
|
|||
|
|
var dictList = SendGet<Dictionary<string, List<AiModelDC>>>(sub_url, key);
|
|||
|
|
|
|||
|
|
if (dictList.TryGetValue("data", out var models))
|
|||
|
|
return MapperFactory.AiModel.MapToNewEntities(models);
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string SendAiMessageRequest(AiConversationDC conversation)
|
|||
|
|
{
|
|||
|
|
var key = getKey();
|
|||
|
|
var sub_url = "api/chat/completions";
|
|||
|
|
|
|||
|
|
var payload = new
|
|||
|
|
{
|
|||
|
|
model = conversation.Modell.ModelName,
|
|||
|
|
messages = conversation.Messages.Select(m => new { role = m.Role.ToString().ToLower(), content = m.Message })
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
//var requestData = JsonConvert.SerializeObject(payload);
|
|||
|
|
|
|||
|
|
var response = SendPostUploadString<object, string>(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
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var json = JsonConvert.DeserializeAnonymousType(response, response_format);
|
|||
|
|
|
|||
|
|
foreach (var choice in json.choices)
|
|||
|
|
{
|
|||
|
|
if (choice?.message is object && !string.IsNullOrEmpty(choice.message.content))
|
|||
|
|
return choice.message.content;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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 = ConfigurationManager.AppSettings["OpenWebUIKey"];
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(key))
|
|||
|
|
throw new InvalidOperationException("OpenWebUIKey is missing");
|
|||
|
|
|
|||
|
|
return key;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|