Files
BeWoPlaner/Server/Components/AICore/Facade/LLM/OllamaApiClient.cs
2025-12-02 15:40:12 +01:00

121 lines
2.8 KiB
C#

using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Feature.AI;
using BS.Shared.Extensions;
using BS.Shared.Interface;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using BeWo.ServerUtils.ApiFacade;
using BS.Shared;
using BS.Shared.Logger;
namespace AICore.Facade.LLM
{
public class OllamaApiClient : BaseLLMApiClient
{
public OllamaApiClient(string base_url, string api_key) : base(base_url, api_key)
{
}
public override ApiResponse<IEnumerable<AiModelDC>> GetAiModelle()
{
var models = new List<AiModelDC>();
var response_format = new
{
models = new[]
{
new
{
name = "",
model = "",
details = new
{
max_context_size = 0
}
}
}
};
var response = _GetClientFacade.GetAnonymousTypeAsync("api/ollama/models", response_format, _ErrorExtractor).GetAwaiter().GetResult();
if (!response.Success)
{
throw new NotImplementedException();
}
var data = response.Data.models.Select(x => new AiModelDC(BS.Shared.AiModelSource.ollama, x.name, x.details.max_context_size));
return ApiResponse<IEnumerable<AiModelDC>>.SuccessResponse(data);
}
public override ApiResponse<string> SendAiMessageRequest(dynamic payload, out string message, out string model, out int? duration, FileLogger logger = null)
{
message = null;
duration = null;
model = null;
_PostClientFacade.JsonObject = payload;
if(logger is object)
{
logger.Log(LogLevel.Info, "Request:");
logger.LogJson(LogLevel.Info, _PostClientFacade.JsonObject);
}
var response_format = new
{
id = string.Empty,
model = string.Empty,
message = new
{
content = string.Empty,
role = string.Empty
},
done_reason = "stop",
done = true,
total_duration = 1316754573L,
load_duration = 31739136L,
prompt_eval_count = 22L,
prompt_eval_duration = 2132887L,
eval_count = 66L,
eval_duration = 1282438235L
};
var response = _PostClientFacade.GetAnonymousTypeAsync("api/ollama/chat/completions", response_format, _ErrorExtractor).GetAwaiter().GetResult();
if(logger is object)
{
logger.Log(LogLevel.Info, "Response:");
logger.LogJson(LogLevel.Info, response);
}
if (!response.Success)
return response.Copy<string>();
var json = response.GetResponseData();
if (json.message is object && !string.IsNullOrEmpty(json.message.content))
{
var total_ns = json.total_duration;
var total_ms = total_ns / 1000000;
duration = (int)total_ms;
model = json.model;
var assi_msg = BS.Shared.Core.Utils.ConvertToISO88591(json.message.content);
message = assi_msg;
return ApiResponse<string>.SuccessResponse(assi_msg);
}
return null;
}
}
}