Logger
This commit is contained in:
@@ -7,12 +7,29 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.Json;
|
||||
using BS.Shared.Interface;
|
||||
using BeWo.ServerUtils.ApiFacade;
|
||||
using BS.Shared.Logger;
|
||||
|
||||
namespace AICore.Facade.LLM
|
||||
{
|
||||
public abstract class BaseLLMApiClient
|
||||
{
|
||||
private protected readonly HttpClientFacade _GetClientFacade;
|
||||
private protected readonly JsonHttpClientFacade _PostClientFacade;
|
||||
private protected readonly IApiErrorExtractor _ErrorExtractor;
|
||||
|
||||
protected BaseLLMApiClient(string base_url, string api_key)
|
||||
{
|
||||
_GetClientFacade = new HttpClientFacade(base_url, api_key, false);
|
||||
_PostClientFacade = new JsonHttpClientFacade(base_url, api_key, false)
|
||||
{
|
||||
Timeout = TimeSpan.FromMinutes(10)
|
||||
};
|
||||
_ErrorExtractor = new DefaultErrorExtractor();
|
||||
}
|
||||
|
||||
public abstract ApiResponse<IEnumerable<AiModelDC>> GetAiModelle();
|
||||
public abstract ApiResponse<string> SendAiMessageRequest(dynamic payload, out string message, out string model, out int? duration);
|
||||
public abstract ApiResponse<string> SendAiMessageRequest(dynamic payload, out string message, out string model, out int? duration, FileLogger logger = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,23 +8,16 @@ 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
|
||||
{
|
||||
private protected readonly HttpClientFacade _GetClientFacade;
|
||||
private protected readonly JsonHttpClientFacade _PostClientFacade;
|
||||
private protected readonly IApiErrorExtractor _ErrorExtractor;
|
||||
|
||||
public OllamaApiClient(string base_url, string api_key)
|
||||
public OllamaApiClient(string base_url, string api_key) : base(base_url, api_key)
|
||||
{
|
||||
_GetClientFacade = new HttpClientFacade(base_url, api_key, false);
|
||||
_PostClientFacade = new JsonHttpClientFacade(base_url, api_key, false)
|
||||
{
|
||||
Timeout = TimeSpan.FromMinutes(10)
|
||||
};
|
||||
_ErrorExtractor = new DefaultErrorExtractor();
|
||||
|
||||
}
|
||||
|
||||
public override ApiResponse<IEnumerable<AiModelDC>> GetAiModelle()
|
||||
@@ -60,7 +53,7 @@ namespace AICore.Facade.LLM
|
||||
return ApiResponse<IEnumerable<AiModelDC>>.SuccessResponse(data);
|
||||
}
|
||||
|
||||
public override ApiResponse<string> SendAiMessageRequest(dynamic payload, out string message, out string model, out int? duration)
|
||||
public override ApiResponse<string> SendAiMessageRequest(dynamic payload, out string message, out string model, out int? duration, FileLogger logger = null)
|
||||
{
|
||||
message = null;
|
||||
duration = null;
|
||||
@@ -68,6 +61,12 @@ namespace AICore.Facade.LLM
|
||||
|
||||
_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,
|
||||
@@ -89,6 +88,12 @@ namespace AICore.Facade.LLM
|
||||
|
||||
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>();
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using BeWo.ServerUtils.ApiFacade;
|
||||
using BS.Shared;
|
||||
using BS.Shared.DataContracts;
|
||||
using BS.Shared.DataContracts.Feature.AI;
|
||||
using BS.Shared.Extensions;
|
||||
using BS.Shared.Interface;
|
||||
using BS.Shared.Logger;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@@ -13,18 +15,9 @@ namespace AICore.Facade.LLM
|
||||
{
|
||||
public class OpenWebApiClient : BaseLLMApiClient
|
||||
{
|
||||
private protected readonly HttpClientFacade _GetClientFacade;
|
||||
private protected readonly JsonHttpClientFacade _PostClientFacade;
|
||||
private protected readonly IApiErrorExtractor _ErrorExtractor;
|
||||
|
||||
public OpenWebApiClient(string base_url, string api_key)
|
||||
public OpenWebApiClient(string base_url, string api_key) : base(base_url, api_key)
|
||||
{
|
||||
_GetClientFacade = new HttpClientFacade(base_url, api_key);
|
||||
_PostClientFacade = new JsonHttpClientFacade(base_url, api_key)
|
||||
{
|
||||
Timeout = TimeSpan.FromMinutes(10)
|
||||
};
|
||||
_ErrorExtractor = new DefaultErrorExtractor();
|
||||
|
||||
}
|
||||
|
||||
public override ApiResponse<IEnumerable<AiModelDC>> GetAiModelle()
|
||||
@@ -60,7 +53,7 @@ namespace AICore.Facade.LLM
|
||||
return ApiResponse<IEnumerable<AiModelDC>>.SuccessResponse(data);
|
||||
}
|
||||
|
||||
public override ApiResponse<string> SendAiMessageRequest(dynamic payload, out string message, out string model, out int? duration)
|
||||
public override ApiResponse<string> SendAiMessageRequest(dynamic payload, out string message, out string model, out int? duration, FileLogger logger = null)
|
||||
{
|
||||
message = null;
|
||||
duration = null;
|
||||
@@ -68,6 +61,12 @@ namespace AICore.Facade.LLM
|
||||
|
||||
_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,
|
||||
@@ -93,6 +92,12 @@ namespace AICore.Facade.LLM
|
||||
|
||||
var response = _PostClientFacade.GetAnonymousTypeAsync("api/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>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user