Skip to content

Commit

Permalink
add Serilog
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzeyu91 committed May 29, 2024
1 parent 3db0cdc commit 034da30
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 60 deletions.
6 changes: 4 additions & 2 deletions docker-compose.simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ version: '3.8'
services:
antsk:
container_name: antsk
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.3.1
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.3.6
# 如果需要pytorch环境需要使用下面这个镜像,镜像比较大
# image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:p0.3.1
# image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:p0.3.6
ports:
- 5000:5000
networks:
Expand All @@ -15,5 +15,7 @@ services:
- ASPNETCORE_URLS=http://*:5000
volumes:
- ./appsettings.json:/app/appsettings.json # 本地配置文件 需要放在同级目录
- /AntSK/model:/app/model
- /AntSK/model:/root/.cache/modelscope/hub/AI-ModelScope # LLamaFactory模型文件
networks:
antsk:
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ services:
- ./pg/data:/var/lib/postgresql/data
antsk:
container_name: antsk
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.3.1
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.3.6
# 如果需要pytorch环境需要使用下面这个镜像,镜像比较大
# image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:p0.3.1
# image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:p0.3.6
ports:
- 5000:5000
networks:
Expand All @@ -32,5 +32,7 @@ services:
- ASPNETCORE_URLS=http://*:5000
volumes:
- ./appsettings.json:/app/appsettings.json # 本地配置文件 需要放在同级目录
- /AntSK/model:/app/model
- /AntSK/model:/root/.cache/modelscope/hub/AI-ModelScope # LLamaFactory模型文件
networks:
antsk:
9 changes: 6 additions & 3 deletions src/AntSK.Domain/AntSK.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />

<PackageReference Include="pythonnet" Version="3.0.3" />

<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />

<PackageReference Include="AutoMapper" Version="8.1.0" />
Expand All @@ -40,8 +40,11 @@
<PackageReference Include="LLamaSharp.Backend.Cuda12" Version="$(LLamaSharpVersion)" />
<PackageReference Include="LLamaSharp.kernel-memory" Version="$(LLamaSharpVersion)" />
<PackageReference Include="LLamaSharp.semantic-kernel" Version="$(LLamaSharpVersion)" />



<PackageReference Include="Serilog" Version="4.0.0-dev-02167" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.1.0-dev-00943" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00972" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.1-dev-10391" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AntSK.LLamaFactory\AntSK.LLamaFactory.csproj" />
Expand Down
13 changes: 11 additions & 2 deletions src/AntSK.Domain/Common/DependencyInjection/InitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using SqlSugar;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand All @@ -19,6 +20,12 @@ namespace AntSK.Domain.Common.DependencyInjection
{
public static class InitExtensions
{
private static ILogger _logger;

public static void InitLog(ILogger logger)
{
_logger = logger;
}
/// <summary>
/// 使用codefirst创建数据库表
/// </summary>
Expand Down Expand Up @@ -52,6 +59,8 @@ public static WebApplication CodeFirst(this WebApplication app)
}
//安装向量插件
_repository.GetDB().Ado.ExecuteCommandAsync($"CREATE EXTENSION IF NOT EXISTS vector;");

_logger.LogInformation("初始化表结构完成");
}
return app;
}
Expand All @@ -72,7 +81,7 @@ public static WebApplication InitDbData(this WebApplication app)
llamafactoryStart.Value = "false";
_dic_Repository.Insert(llamafactoryStart);
}

_logger.LogInformation("初始化数据库初始数据完成");
}
return app;
}
Expand All @@ -99,7 +108,7 @@ public static WebApplication LoadFun(this WebApplication app)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " ---- " + ex.StackTrace);
_logger.LogError(ex.Message + " ---- " + ex.StackTrace);
}
return app;
}
Expand Down
8 changes: 5 additions & 3 deletions src/AntSK.Domain/Common/LLamaFactory/ProcessWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Amazon.Runtime.Internal.Util;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand All @@ -7,7 +9,7 @@

namespace AntSK.Domain.Common.LLamaFactory
{
public class ProcessWrapper
public class ProcessWrapper(ILogger<ProcessWrapper> _logger)
{
private Process process;

Expand Down Expand Up @@ -41,7 +43,7 @@ public void StartProcess(string arguments, string workingDirectory)
isProcessComplete = true;
}
}
Console.WriteLine(result);
_logger.LogInformation(result);
}
start.WaitForExit();
}
Expand Down
12 changes: 8 additions & 4 deletions src/AntSK.Domain/Domain/Other/BackGroundTaskHandler.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
using AntSK.BackgroundTask;
using Amazon.Runtime.Internal.Util;
using AntSK.BackgroundTask;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace AntSK.Domain.Domain.Other
{
public class BackGroundTaskHandler : IBackgroundTaskHandler<ImportKMSTaskReq>
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ILogger<BackGroundTaskHandler> _logger;

public BackGroundTaskHandler(IServiceScopeFactory scopeFactory)
public BackGroundTaskHandler(IServiceScopeFactory scopeFactory, ILogger<BackGroundTaskHandler> logger)
{
_scopeFactory = scopeFactory;
_logger = logger;
}
public async Task ExecuteAsync(ImportKMSTaskReq item)
{
using (var scope = _scopeFactory.CreateScope())
{
Console.WriteLine("ExecuteAsync.开始执行后台任务");
_logger.LogInformation("ExecuteAsync.开始执行后台任务");
var importKMSService = scope.ServiceProvider.GetRequiredService<IImportKMSService>();
//不能使用异步
importKMSService.ImportKMSTask(item);
Console.WriteLine("ExecuteAsync.后台任务执行完成");
_logger.LogInformation("ExecuteAsync.后台任务执行完成");
}

}
Expand Down
3 changes: 2 additions & 1 deletion src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.KernelMemory.AI.OpenAI;
using Microsoft.KernelMemory.AI.OpenAI.GPT3;
using Python.Runtime;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -92,7 +93,7 @@ public static int TokenCount(string queryStr)

public static void Dispose()
{
Console.WriteLine("python dispose");
Log.Information("python dispose");
}
}
}
3 changes: 2 additions & 1 deletion src/AntSK.Domain/Domain/Service/FunctionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.SemanticKernel;
using HtmlAgilityPack;
using System.Collections.Generic;
using Serilog;

namespace AntSK.Domain.Domain.Service
{
Expand Down Expand Up @@ -115,7 +116,7 @@ public void FuncLoad(string pluginPath)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + " ---- " + ex.StackTrace);
Log.Error(ex.Message + " ---- " + ex.StackTrace);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/AntSK.Domain/Domain/Service/ImportKMSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AntSK.Domain.Domain.Model.Excel;
using AntSK.Domain.Domain.Other;
using AntSK.Domain.Repositories;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Handlers;
using System.Text;
Expand All @@ -15,7 +16,8 @@ namespace AntSK.Domain.Domain.Service
public class ImportKMSService(
IKMService _kMService,
IKmsDetails_Repositories _kmsDetails_Repositories,
IKmss_Repositories _kmss_Repositories
IKmss_Repositories _kmss_Repositories,
ILogger<ImportKMSService> _logger
) : IImportKMSService
{

Expand Down Expand Up @@ -140,13 +142,13 @@ public void ImportKMSTask(ImportKMSTaskReq req)
req.KmsDetail.Status = Model.Enum.ImportKmsStatus.Success;
_kmsDetails_Repositories.Update(req.KmsDetail);
//_kmsDetails_Repositories.GetList(p => p.KmsId == req.KmsId);
Console.WriteLine("后台导入任务成功:" + req.KmsDetail.DataCount);
_logger.LogInformation("后台导入任务成功:" + req.KmsDetail.DataCount);
}
catch (Exception ex)
{
req.KmsDetail.Status = Model.Enum.ImportKmsStatus.Fail;
_kmsDetails_Repositories.Update(req.KmsDetail);
Console.WriteLine("后台导入任务异常:" + ex.Message);
_logger.LogError("后台导入任务异常:" + ex.Message);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/AntSK.Domain/Domain/Service/KernelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using OpenCvSharp.ML;
using LLamaSharp.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.ChatCompletion;
using Amazon.Runtime.Internal.Util;
using Microsoft.Extensions.Logging;

namespace AntSK.Domain.Domain.Service
{
Expand All @@ -33,17 +35,20 @@ public class KernelService : IKernelService
private readonly FunctionService _functionService;
private readonly IServiceProvider _serviceProvider;
private Kernel _kernel;
private readonly ILogger<KernelService> _logger;

public KernelService(
IApis_Repositories apis_Repositories,
IAIModels_Repositories aIModels_Repositories,
FunctionService functionService,
IServiceProvider serviceProvider)
IServiceProvider serviceProvider,
ILogger<KernelService> logger)
{
_apis_Repositories = apis_Repositories;
_aIModels_Repositories = aIModels_Repositories;
_functionService = functionService;
_serviceProvider = serviceProvider;
_logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -224,7 +229,7 @@ private void ImportApiFunction(Apps app, List<KernelFunction> functions)
{
try
{
Console.WriteLine(jsonBody);
_logger.LogInformation(jsonBody);
RestClient client = new RestClient();
RestRequest request = new RestRequest(api.Url, Method.Post);
foreach (var header in api.Header.ConvertToString().Split("\n"))
Expand Down
18 changes: 10 additions & 8 deletions src/AntSK.Domain/Domain/Service/LLamaFactoryService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using AntSK.Domain.Common.DependencyInjection;
using Amazon.Runtime.Internal.Util;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Model.Dto;
using AntSK.Domain.Options;
using AntSK.LLamaFactory.Model;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
Expand All @@ -17,7 +19,7 @@
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(ILLamaFactoryService), ServiceLifetime.Singleton)]
public class LLamaFactoryService : ILLamaFactoryService
public class LLamaFactoryService(ILogger<LLamaFactoryService> _logger) : ILLamaFactoryService
{
private Process process;

Expand All @@ -26,7 +28,7 @@ public class LLamaFactoryService : ILLamaFactoryService
private readonly object _syncLock = new object();
private List<LLamaModel> modelList = new List<LLamaModel>();

public LLamaFactoryService() { }

public delegate Task LogMessageHandler(string message);
public event LogMessageHandler LogMessageReceived;
protected virtual async Task OnLogMessageReceived(string message)
Expand Down Expand Up @@ -56,12 +58,12 @@ public async Task PipInstall()
};
process.OutputDataReceived += (sender, eventArgs) =>
{
Console.WriteLine($"{eventArgs.Data}");
_logger.LogInformation($"{eventArgs.Data}");
OnLogMessageReceived(eventArgs.Data);
};
process.ErrorDataReceived += (sender, eventArgs) =>
{
Console.WriteLine($"{eventArgs.Data}");
_logger.LogInformation($"{eventArgs.Data}");
OnLogMessageReceived(eventArgs.Data);
};
process.Start();
Expand Down Expand Up @@ -97,12 +99,12 @@ public async Task StartLLamaFactory(string modelName, string templateName)
process.StartInfo.EnvironmentVariables["USE_MODELSCOPE_HUB"] = Environment.GetEnvironmentVariable("USE_MODELSCOPE_HUB") ?? "1";
process.OutputDataReceived += (sender, eventArgs) =>
{
Console.WriteLine($"{eventArgs.Data}");
_logger.LogInformation($"{eventArgs.Data}");
OnLogMessageReceived(eventArgs.Data);
};
process.ErrorDataReceived += (sender, eventArgs) =>
{
Console.WriteLine($"{eventArgs.Data}");
_logger.LogInformation($"{eventArgs.Data}");
OnLogMessageReceived(eventArgs.Data);
};
process.Start();
Expand Down Expand Up @@ -137,7 +139,7 @@ public void KillProcess()
if (process1.ProcessName.ToLower() == "python")
{
process1.Kill();
System.Console.WriteLine("kill python");
_logger.LogInformation("kill python");
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/AntSK.Domain/Options/FileDirOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntSK.Domain.Options
{
public class FileDirOption
{
public static string DirectoryPath { get; set; } = Directory.GetCurrentDirectory();
}
}
1 change: 0 additions & 1 deletion src/AntSK.Domain/Options/LLamaSharpOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
public class LLamaSharpOption
{
public static string RunType { get; set; }
public static string FileDirectory { get; set; } = Directory.GetCurrentDirectory();
}
}
6 changes: 4 additions & 2 deletions src/AntSK.Domain/Utils/OpenAIHttpClientHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.RegularExpressions;

using Serilog;
using System.Text.RegularExpressions;

namespace AntSK.Domain.Utils
{
Expand All @@ -19,7 +21,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
string requestBody = await request.Content.ReadAsStringAsync();
//便于调试查看请求prompt
Console.WriteLine(requestBody);
Log.Information(requestBody);
}
if (match.Success)
{
Expand Down
2 changes: 1 addition & 1 deletion src/AntSK/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task<IActionResult> UploadFile(IFormFile file)
}

// 创建文件存储的路径
var uploadsFolderPath = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), LLamaSharpOption.FileDirectory), "files");// 给定的文件夹名称
var uploadsFolderPath = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), FileDirOption.DirectoryPath), "files");// 给定的文件夹名称

// 如果路径不存在,则创建一个新的目录
if (!Directory.Exists(uploadsFolderPath))
Expand Down
Loading

0 comments on commit 034da30

Please sign in to comment.