Compare commits

...

29 Commits
0.1.6 ... 0.1.7

Author SHA1 Message Date
zyxucp
3b5997fce6 Merge branch 'main' of https://github.com/xuzeyu91/AntSK 2024-03-09 23:43:30 +08:00
zyxucp
7b0f6c3e75 add 增加消息结束后滚动条滚到底部 2024-03-09 23:40:00 +08:00
zyxucp
099b85619c add 增加消息时间的气泡 2024-03-09 23:12:28 +08:00
zyxucp
0129cd3f39 fix 修改头像样式为固定大小 2024-03-09 23:04:09 +08:00
zyxucp
84f3cbf9a9 add 增加消息复制按钮 2024-03-09 22:55:45 +08:00
zyxucp
a7af462a44 add 增加知识库外部使用流式接口 2024-03-09 22:20:05 +08:00
zyxucp
fdca08eb3d fix 抽象KMS结构 2024-03-09 22:09:31 +08:00
zyxucp
c3eeefe9fe fix 抽象sendkms 2024-03-09 21:40:36 +08:00
zyxucp
2f6990320c fix 封装Chat方法 2024-03-09 21:21:32 +08:00
zyxucp
ef83450425 fix 调整名称 2024-03-09 21:12:22 +08:00
zyxucp
f2f10ec9f4 fix 调整项目结构,抽象发送消息接口 2024-03-09 21:10:50 +08:00
zyxucp
7346ff2e78 add 更新图片 2024-03-09 19:35:38 +08:00
zyxucp
1dc274ce82 fix 执行code clear 2024-03-09 19:19:05 +08:00
zyxucp
2be438f9c3 fix 调整项目结构 2024-03-09 19:12:40 +08:00
zyxucp
1f7f51ff1e fix 优化代码 2024-03-09 18:24:17 +08:00
zyxucp
338a7ae083 Update docker-compose.simple.yml 2024-03-09 17:47:30 +08:00
zyxucp
04d7896a92 Update docker-compose.yml 2024-03-09 17:47:17 +08:00
zyxucp
4b8c8c0f96 fix 修改openai azue 和llama 流式不一致的问题 2024-03-09 17:30:11 +08:00
zyxucp
8296476d94 Update docker-compose.simple.yml 2024-03-09 12:40:04 +08:00
zyxucp
38b3fc26ed Update docker-compose.yml 2024-03-09 12:39:48 +08:00
zyxucp
6852b458fa fix 字典修改 2024-03-09 12:15:32 +08:00
zyxucp
cdb41023d4 Merge branch 'main' of https://github.com/xuzeyu91/AntSK 2024-03-09 12:10:05 +08:00
zyxucp
8e00e681f0 fix 修改StreamingTextContent 2024-03-09 12:09:53 +08:00
zyxucp
577d6dd3a6 add 增加异常日志 2024-03-09 12:02:47 +08:00
zyxucp
eeecd15d9e fix 修改StreamingTextContent 2024-03-09 11:59:36 +08:00
zyxucp
e39b26bcc3 更新 README.md 2024-03-09 00:05:20 +08:00
zyxucp
6b5a77f8c1 Update appsettings.json 2024-03-08 23:38:36 +08:00
zyxucp
0dc9736c35 Update docker-compose.simple.yml 2024-03-08 23:18:15 +08:00
zyxucp
bbe2471815 Update docker-compose.yml 2024-03-08 23:17:49 +08:00
131 changed files with 1043 additions and 1417 deletions

View File

@@ -118,12 +118,6 @@ model/xxx.gguf
"DbType": "Sqlite",
"ConnectionStrings": "Data Source=AntSK.db;"
},
"OpenAIOption": {
"EndPoint": "http://localhost:5000/llama/",
"Key": "NotNull",
"Model": "gpt4-turbo",
"EmbeddingModel": "text-embedding-ada-002"
},
"KernelMemory": {
"VectorDb": "Disk",
"ConnectionString": "Host=;Port=;Database=antsk;Username=;Password=",

View File

@@ -3,7 +3,7 @@ version: '3.8'
services:
antsk:
container_name: antsk
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.5
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.6.2
ports:
- 5000:5000
networks:

View File

@@ -18,7 +18,7 @@ services:
- ./pg/data:/var/lib/postgresql/data
antsk:
container_name: antsk
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.5
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.6.2
ports:
- 5000:5000
networks:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 KiB

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 174 KiB

After

Width:  |  Height:  |  Size: 170 KiB

View File

@@ -8,6 +8,9 @@
<NoWarn>CA1050,CA1707,CA2007,VSTHRD111,CS1591,RCS1110,CA5394,SKEXP0001,SKEXP0002,SKEXP0003,SKEXP0004,SKEXP0010,SKEXP0011,,SKEXP0012,SKEXP0020,SKEXP0021,SKEXP0022,SKEXP0023,SKEXP0024,SKEXP0025,SKEXP0026,SKEXP0027,SKEXP0028,SKEXP0029,SKEXP0030,SKEXP0031,SKEXP0032,SKEXP0040,SKEXP0041,SKEXP0042,SKEXP0050,SKEXP0051,SKEXP0052,SKEXP0053,SKEXP0054,SKEXP0055,SKEXP0060,SKEXP0061,SKEXP0101,SKEXP0102</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AntDesign.Charts" Version="0.5.1" />
<PackageReference Include="AntDesign.ProLayout" Version="0.17.3" />
<PackageReference Include="AutoMapper" Version="8.1.0" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="MarkdownSharp" Version="2.0.5" />

View File

@@ -48,6 +48,20 @@
<param name="value"></param>
<returns></returns>
</member>
<member name="F:AntSK.Domain.Domain.Other.LLamaConfig.dicLLamaWeights">
<summary>
避免模型重复加载,本地缓存
</summary>
</member>
<member name="M:AntSK.Domain.Domain.Service.ChatService.SendChatByAppAsync(AntSK.Domain.Repositories.Apps,System.String,System.String)">
<summary>
发送消息
</summary>
<param name="app"></param>
<param name="questions"></param>
<param name="history"></param>
<returns></returns>
</member>
<member name="M:AntSK.Domain.Domain.Service.KernelService.GetKernelByApp(AntSK.Domain.Repositories.Apps)">
<summary>
获取kernel实例依赖注入不好按每个用户去Import不同的插件所以每次new一个新的kernel
@@ -78,11 +92,6 @@
<param name="history"></param>
<returns></returns>
</member>
<member name="F:AntSK.Domain.Domain.Service.LLamaConfig.dicLLamaWeights">
<summary>
避免模型重复加载,本地缓存
</summary>
</member>
<member name="M:AntSK.Domain.Map.MapperExtend.ToDTOList``1(System.Object)">
<summary>
Entity集合转DTO集合

View File

@@ -1,10 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Common.DependencyInjection
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Common.DependencyInjection
namespace AntSK.Domain.Common.DependencyInjection
{
public class ServiceDescriptionAttribute : Attribute
{

View File

@@ -1,7 +1,4 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
namespace AntSK.Domain.Common.Map
{

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace AntSK.Domain.Common.Map
namespace AntSK.Domain.Common.Map
{
public static class MapperExtend
{

View File

@@ -1,8 +1,5 @@
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
namespace AntSK.Domain.Common.Map
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Dto
namespace AntSK.Domain.Domain.Dto
{
public class KMFile
{

View File

@@ -1,11 +1,4 @@
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Models
namespace AntSK.Domain.Domain.Dto
{
public class OpenAIModel
{

View File

@@ -1,7 +1,6 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace AntSK.Models.OpenAPI
namespace AntSK.Domain.Domain.Dto
{
public class OpenAIResult
{
@@ -21,7 +20,7 @@ namespace AntSK.Models.OpenAPI
public OpenAIMessage message { get; set; }
}
public class OpenAIEmbeddingResult
public class OpenAIEmbeddingResult
{
[JsonProperty("object")]
public string obj { get; set; } = "list";
@@ -29,17 +28,17 @@ namespace AntSK.Models.OpenAPI
public UsageModel usage { get; set; } = new UsageModel();
public List<DataModel> data { get; set; } = new List<DataModel>() { new DataModel() };
public List<DataModel> data { get; set; } = new List<DataModel>() { new DataModel() };
}
public class UsageModel
public class UsageModel
{
public long prompt_tokens { get; set; } = 0;
public long total_tokens { get; set; } = 0;
}
public class DataModel
public class DataModel
{
[JsonProperty("object")]
public string obj { get; set; } = "embedding";

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Dto
{
public class RelevantSource
{
public string SourceName { get; set; }
public string Text { get; set; }
public float Relevance { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Repositories;
using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{
public interface IChatService
{
IAsyncEnumerable<StreamingKernelContent> SendChatByAppAsync(Apps app, string questions, string history);
IAsyncEnumerable<StreamingKernelContent> SendKmsByAppAsync(Apps app, string questions, string history, List<RelevantSource> relevantSources = null);
}
}

View File

@@ -1,9 +1,4 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{

View File

@@ -1,11 +1,5 @@
using AntSK.Domain.Domain.Dto;
using Microsoft.KernelMemory.Configuration;
using Microsoft.KernelMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{

View File

@@ -1,10 +1,5 @@
using AntSK.Domain.Repositories;
using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{

View File

@@ -2,13 +2,8 @@
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Service
namespace AntSK.Domain.Domain.Other
{
public class BackGroundTaskHandler : IBackgroundTaskHandler<ImportKMSTaskReq>
{
@@ -23,7 +18,7 @@ namespace AntSK.Domain.Domain.Service
using (var scope = _scopeFactory.CreateScope())
{
Console.WriteLine("ExecuteAsync.开始执行后台任务");
var importKMSService = scope.ServiceProvider.GetRequiredService<IImportKMSService>();
var importKMSService = scope.ServiceProvider.GetRequiredService<IImportKMSService>();
//不能使用异步
importKMSService.ImportKMSTask(item);
Console.WriteLine("ExecuteAsync.后台任务执行完成");

View File

@@ -1,13 +1,8 @@
using LLama.Common;
using LLama;
using LLama;
using LLama.Common;
using LLamaSharp.KernelMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Service
namespace AntSK.Domain.Domain.Other
{
public static class LLamaConfig
{
@@ -16,7 +11,7 @@ namespace AntSK.Domain.Domain.Service
/// 避免模型重复加载,本地缓存
/// </summary>
static Dictionary<string, (LLamaWeights, ModelParams)> dicLLamaWeights = new Dictionary<string, (LLamaWeights, ModelParams)>();
public static (LLamaWeights, ModelParams) GetLLamaConfig(string modelPath, LLamaSharpConfig config =null)
public static (LLamaWeights, ModelParams) GetLLamaConfig(string modelPath, LLamaSharpConfig config = null)
{
lock (lockobj)
{
@@ -28,7 +23,7 @@ namespace AntSK.Domain.Domain.Service
{
InferenceParams infParams = new() { AntiPrompts = ["\n\n"] };
LLamaSharpConfig lsConfig = new(modelPath) { DefaultInferenceParams = infParams };
if (config!=null)
if (config != null)
{
lsConfig = config;
}
@@ -43,7 +38,7 @@ namespace AntSK.Domain.Domain.Service
dicLLamaWeights.Add(modelPath, (weights, parameters));
return (weights, parameters);
}
}
}
}
}
}

View File

@@ -0,0 +1,105 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Repositories;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Utils;
using Microsoft.KernelMemory;
using AntSK.Domain.Model;
using MarkdownSharp;
using AntSK.Domain.Domain.Dto;
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(IChatService), ServiceLifetime.Scoped)]
public class ChatService(
IKernelService _kernelService,
IKMService _kMService ,
IKmsDetails_Repositories _kmsDetails_Repositories
) : IChatService
{
/// <summary>
/// 发送消息
/// </summary>
/// <param name="app"></param>
/// <param name="questions"></param>
/// <param name="history"></param>
/// <returns></returns>
public async IAsyncEnumerable<StreamingKernelContent> SendChatByAppAsync(Apps app, string questions, string history)
{
if (string.IsNullOrEmpty(app.Prompt) || !app.Prompt.Contains("{{$input}}"))
{
//如果模板为空,给默认提示词
app.Prompt = app.Prompt.ConvertToString() + "{{$input}}";
}
var _kernel = _kernelService.GetKernelByApp(app);
var temperature = app.Temperature / 100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() { Temperature = temperature };
if (!string.IsNullOrEmpty(app.ApiFunctionList))
{
_kernelService.ImportFunctionsByApp(app, _kernel);
settings.ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions;
}
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var chatResult = _kernel.InvokeStreamingAsync(function: func, arguments: new KernelArguments() { ["input"] = $"{history}{Environment.NewLine} user:{questions}" });
await foreach (var content in chatResult)
{
yield return content;
}
}
public async IAsyncEnumerable<StreamingKernelContent> SendKmsByAppAsync(Apps app, string questions, string history, List<RelevantSource> relevantSources=null)
{
var _kernel = _kernelService.GetKernelByApp(app);
//知识库问答
var filters = new List<MemoryFilter>();
var kmsidList = app.KmsIdList.Split(",");
//只取第一个知识库的配置
var _memory = _kMService.GetMemoryByKMS(kmsidList.FirstOrDefault());
foreach (var kmsid in kmsidList)
{
filters.Add(new MemoryFilter().ByTag("kmsid", kmsid));
}
var xlresult = await _memory.SearchAsync(questions, index: "kms", filters: filters);
string dataMsg = "";
if (xlresult != null)
{
foreach (var item in xlresult.Results)
{
foreach (var part in item.Partitions)
{
dataMsg += $"[file:{item.SourceName};Relevance:{(part.Relevance * 100).ToString("F2")}%]:{part.Text}{Environment.NewLine}";
if (relevantSources.IsNotNull())
{
var markdown = new Markdown();
string sourceName = item.SourceName;
var fileDetail = _kmsDetails_Repositories.GetFirst(p => p.FileGuidName == item.SourceName);
if (fileDetail.IsNotNull())
{
sourceName = fileDetail.FileName;
}
relevantSources.Add(new RelevantSource() { SourceName = sourceName, Text = markdown.Transform(part.Text), Relevance = part.Relevance });
}
}
}
KernelFunction jsonFun = _kernel.Plugins.GetFunction("KMSPlugin", "Ask");
var chatResult = _kernel.InvokeStreamingAsync(function: jsonFun,
arguments: new KernelArguments() { ["doc"] = dataMsg, ["history"] = history, ["questions"] = questions });
MessageInfo info = null;
var markdown1 = new Markdown();
await foreach (var content in chatResult)
{
yield return content;
}
}
}
}
}

View File

@@ -1,30 +1,21 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Utils;
using Microsoft.KernelMemory;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(IHttpService), ServiceLifetime.Scoped)]
public class HttpService: IHttpService
public class HttpService : IHttpService
{
public async Task< RestResponse> PostAsync(string url ,Object jsonBody)
public async Task<RestResponse> PostAsync(string url, Object jsonBody)
{
RestClient client = new RestClient();
RestRequest request= new RestRequest(url, Method.Post);
RestRequest request = new RestRequest(url, Method.Post);
string josn = JsonConvert.SerializeObject(jsonBody);
request.AddJsonBody(jsonBody);
var result =await client.ExecuteAsync(request);
return result;
var result = await client.ExecuteAsync(request);
return result;
}
}
}

View File

@@ -3,13 +3,6 @@ using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Configuration;
using Microsoft.SemanticKernel.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Service
{
@@ -34,12 +27,12 @@ namespace AntSK.Domain.Domain.Service
case ImportType.File:
//导入文件
{
var importResult= _memory.ImportDocumentAsync(new Document(fileid)
.AddFile(req.FilePath)
.AddTag("kmsid", req.KmsId)
, index: "kms").Result;
var importResult = _memory.ImportDocumentAsync(new Document(fileid)
.AddFile(req.FilePath)
.AddTag("kmsid", req.KmsId)
, index: "kms").Result;
//查询文档数量
var docTextList = _kMService.GetDocumentByFileID(km.Id,fileid).Result;
var docTextList = _kMService.GetDocumentByFileID(km.Id, fileid).Result;
string fileGuidName = Path.GetFileName(req.FilePath);
req.KmsDetail.FileName = req.FileName;
req.KmsDetail.FileGuidName = fileGuidName;
@@ -53,7 +46,7 @@ namespace AntSK.Domain.Domain.Service
var importResult = _memory.ImportWebPageAsync(req.Url, fileid, new TagCollection() { { "kmsid", req.KmsId } }
, index: "kms").Result;
//查询文档数量
var docTextList = _kMService.GetDocumentByFileID(km.Id,fileid).Result;
var docTextList = _kMService.GetDocumentByFileID(km.Id, fileid).Result;
req.KmsDetail.Url = req.Url;
req.KmsDetail.DataCount = docTextList.Count;
}
@@ -64,7 +57,7 @@ namespace AntSK.Domain.Domain.Service
var importResult = _memory.ImportTextAsync(req.Text, fileid, new TagCollection() { { "kmsid", req.KmsId } }
, index: "kms").Result;
//查询文档数量
var docTextList = _kMService.GetDocumentByFileID(km.Id,fileid).Result;
var docTextList = _kMService.GetDocumentByFileID(km.Id, fileid).Result;
req.KmsDetail.Url = req.Url;
req.KmsDetail.DataCount = docTextList.Count;
@@ -72,14 +65,14 @@ namespace AntSK.Domain.Domain.Service
break;
}
req.KmsDetail.Status = Model.Enum.ImportKmsStatus.Success;
_kmsDetails_Repositories.Update(req.KmsDetail);
//_kmsDetails_Repositories.GetList(p => p.KmsId == req.KmsId);
_kmsDetails_Repositories.Update(req.KmsDetail);
//_kmsDetails_Repositories.GetList(p => p.KmsId == req.KmsId);
Console.WriteLine("后台导入任务成功:" + req.KmsDetail.DataCount);
}
catch (Exception ex)
{
req.KmsDetail.Status = Model.Enum.ImportKmsStatus.Fail;
_kmsDetails_Repositories.Update(req.KmsDetail);
_kmsDetails_Repositories.Update(req.KmsDetail);
Console.WriteLine("后台导入任务异常:" + ex.Message);
}
}

View File

@@ -1,21 +1,17 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using Microsoft.KernelMemory;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Options;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Other;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using LLama;
using LLamaSharp.KernelMemory;
using Microsoft.Extensions.Configuration;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Configuration;
using Microsoft.KernelMemory.ContentStorage.DevTools;
using Microsoft.KernelMemory.FileSystem.DevTools;
using Microsoft.KernelMemory.Postgres;
using System.Net.Http;
using Microsoft.Extensions.Options;
using Microsoft.KernelMemory.Configuration;
using Microsoft.Extensions.Configuration;
using AntSK.Domain.Repositories;
using LLamaSharp.KernelMemory;
using LLama.Common;
using DocumentFormat.OpenXml.Spreadsheet;
using LLama;
namespace AntSK.Domain.Domain.Service
{
@@ -60,7 +56,7 @@ namespace AntSK.Domain.Domain.Service
//加载huihu 模型
WithTextGenerationByAIType(memory, chatModel, chatHttpClient);
//加载向量模型
WithTextEmbeddingGenerationByAIType(memory,embedModel, embeddingHttpClient);
WithTextEmbeddingGenerationByAIType(memory, embedModel, embeddingHttpClient);
//加载向量库
WithMemoryDbByVectorDB(memory, _config);
@@ -69,7 +65,7 @@ namespace AntSK.Domain.Domain.Service
}
private void WithTextEmbeddingGenerationByAIType(IKernelMemoryBuilder memory,AIModels embedModel, HttpClient embeddingHttpClient )
private void WithTextEmbeddingGenerationByAIType(IKernelMemoryBuilder memory, AIModels embedModel, HttpClient embeddingHttpClient)
{
switch (embedModel.AIType)
{
@@ -85,7 +81,9 @@ namespace AntSK.Domain.Domain.Service
{
APIKey = embedModel.ModelKey,
Deployment = embedModel.ModelName.ConvertToString(),
Endpoint = embedModel.EndPoint.ConvertToString()
Endpoint = embedModel.EndPoint.ConvertToString(),
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIType = AzureOpenAIConfig.APITypes.EmbeddingGeneration,
});
break;
case Model.Enum.AIType.LLamaSharp:
@@ -96,7 +94,7 @@ namespace AntSK.Domain.Domain.Service
}
}
private void WithTextGenerationByAIType(IKernelMemoryBuilder memory,AIModels chatModel, HttpClient chatHttpClient )
private void WithTextGenerationByAIType(IKernelMemoryBuilder memory, AIModels chatModel, HttpClient chatHttpClient)
{
switch (chatModel.AIType)
{
@@ -112,7 +110,9 @@ namespace AntSK.Domain.Domain.Service
{
APIKey = chatModel.ModelKey,
Deployment = chatModel.ModelName.ConvertToString(),
Endpoint = chatModel.EndPoint.ConvertToString()
Endpoint = chatModel.EndPoint.ConvertToString(),
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIType = AzureOpenAIConfig.APITypes.TextCompletion,
});
break;
case Model.Enum.AIType.LLamaSharp:
@@ -124,7 +124,7 @@ namespace AntSK.Domain.Domain.Service
}
}
private void WithMemoryDbByVectorDB(IKernelMemoryBuilder memory,IConfiguration _config)
private void WithMemoryDbByVectorDB(IKernelMemoryBuilder memory, IConfiguration _config)
{
string VectorDb = _config["KernelMemory:VectorDb"].ConvertToString();
string ConnectionString = _config["KernelMemory:ConnectionString"].ConvertToString();
@@ -153,7 +153,7 @@ namespace AntSK.Domain.Domain.Service
}
}
public async Task<List<KMFile>> GetDocumentByFileID(string kmsid,string fileid)
public async Task<List<KMFile>> GetDocumentByFileID(string kmsid, string fileid)
{
var _memory = GetMemoryByKMS(kmsid);
var memories = await _memory.ListIndexesAsync();
@@ -183,6 +183,6 @@ namespace AntSK.Domain.Domain.Service
return docTextList;
}
}
}

View File

@@ -1,38 +1,29 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Other;
using AntSK.Domain.Model;
using AntSK.Domain.Options;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using DocumentFormat.OpenXml.EMMA;
using LLama;
using LLamaSharp.KernelMemory;
using LLamaSharp.SemanticKernel.TextCompletion;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Core;
using Microsoft.SemanticKernel.TextGeneration;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using ServiceLifetime = AntSK.Domain.Common.DependencyInjection.ServiceLifetime;
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(IKernelService), ServiceLifetime.Scoped)]
public class KernelService: IKernelService
public class KernelService : IKernelService
{
private readonly IApis_Repositories _apis_Repositories;
private readonly IAIModels_Repositories _aIModels_Repositories;
public KernelService(
IApis_Repositories apis_Repositories,
IAIModels_Repositories aIModels_Repositories
)
)
{
_apis_Repositories = apis_Repositories;
_aIModels_Repositories = aIModels_Repositories;
@@ -46,15 +37,15 @@ namespace AntSK.Domain.Domain.Service
/// <returns></returns>
public Kernel GetKernelByApp(Apps app)
{
var chatModel= _aIModels_Repositories.GetFirst(p => p.Id == app.ChatModelID);
var chatModel = _aIModels_Repositories.GetFirst(p => p.Id == app.ChatModelID);
var chatHttpClient = OpenAIHttpClientHandlerUtil.GetHttpClient(chatModel.EndPoint);
var builder = Kernel.CreateBuilder();
WithTextGenerationByAIType(builder, chatModel, chatHttpClient);
var kernel= builder.Build();
var kernel = builder.Build();
RegisterPluginsWithKernel(kernel);
return kernel;
}
@@ -71,7 +62,7 @@ namespace AntSK.Domain.Domain.Service
break;
case Model.Enum.AIType.AzureOpenAI:
builder.AddAzureOpenAIChatCompletion(
deploymentName:chatModel.ModelName,
deploymentName: chatModel.ModelName,
apiKey: chatModel.ModelKey,
endpoint: chatModel.EndPoint
);
@@ -187,7 +178,7 @@ namespace AntSK.Domain.Domain.Service
/// <param name="questions"></param>
/// <param name="history"></param>
/// <returns></returns>
public async Task<string> HistorySummarize(Kernel _kernel,string questions, string history)
public async Task<string> HistorySummarize(Kernel _kernel, string questions, string history)
{
KernelFunction sunFun = _kernel.Plugins.GetFunction("ConversationSummaryPlugin", "SummarizeConversation");
var summary = await _kernel.InvokeAsync(sunFun, new() { ["input"] = $"内容是:{history.ToString()} {Environment.NewLine} 请注意用中文总结" });

View File

@@ -1,9 +1,4 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Map
{

View File

@@ -1,9 +1,4 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Map
{

View File

@@ -1,10 +1,5 @@
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Map
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model.Enum
namespace AntSK.Domain.Model.Enum
{
/// <summary>
/// AI类型
@@ -13,7 +7,7 @@ namespace AntSK.Domain.Model.Enum
{
OpenAI = 1,
AzureOpenAI = 2,
LLamaSharp=3
LLamaSharp = 3
}
/// <summary>

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
namespace AntSK.Domain.Model
{
public enum HttpMethodType
{

View File

@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model.Enum
namespace AntSK.Domain.Model.Enum
{
public enum ImportKmsStatus
{
Loadding=0,
Success=1,
Fail=2
Loadding = 0,
Success = 1,
Fail = 2
}
}

View File

@@ -1,10 +1,4 @@
using AntSK.Domain.Repositories;
using Microsoft.KernelMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
{
@@ -26,14 +20,15 @@ namespace AntSK.Domain.Model
}
public class ImportKMSTaskReq: ImportKMSTaskDTO
public class ImportKMSTaskReq : ImportKMSTaskDTO
{
public KmsDetails KmsDetail { get; set; } = new KmsDetails();
}
public enum ImportType {
File=1,
Url=2,
Text=3
public enum ImportType
{
File = 1,
Url = 2,
Text = 3
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
namespace AntSK.Domain.Model
{
public class MessageInfo
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
namespace AntSK.Domain.Model
{
public class PageList<T>
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class DBConnectionOption
{

View File

@@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class KernelMemoryOption
{
/// <summary>
/// 向量库
/// </summary>
/// <summary>
/// 向量库
/// </summary>
public static string VectorDb { get; set; }
/// <summary>
/// 连接字符串

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class LLamaSharpOption
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class LoginOption
{

View File

@@ -1,11 +1,6 @@
using AntSK.Domain.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{
@@ -47,7 +42,7 @@ namespace AntSK.Domain.Repositories
/// 温度
/// </summary>
[SugarColumn(DefaultValue = "70")]
public double Temperature { get; set; }=70f;
public double Temperature { get; set; } = 70f;
/// <summary>
/// 提示词

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{
@@ -60,6 +55,6 @@ namespace AntSK.Domain.Repositories
[SugarColumn(DefaultValue = "49")]
public int OverlappingTokens { get; set; } = 49;
}
}

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using AntSK.Domain.Model.Enum;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using AntSK.Domain.Model;
using SqlSugar;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Model;
namespace AntSK.Domain.Repositories.Base
{

View File

@@ -1,13 +1,7 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Map;
using AntSK.Domain.Map;
using AntSK.Domain.Model;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using System.Linq.Expressions;
namespace AntSK.Domain.Repositories.Base
@@ -15,7 +9,7 @@ namespace AntSK.Domain.Repositories.Base
public class Repository<T> : SimpleClient<T> where T : class, new()
{
public Repository( ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)

View File

@@ -1,13 +1,6 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Options;
using AntSK.Domain.Utils;
using AntSK.Domain.Options;
using SqlSugar;
using System.Reflection;
using Microsoft.Extensions.Configuration;
namespace AntSK.Domain.Repositories.Base
{
@@ -17,7 +10,8 @@ namespace AntSK.Domain.Repositories.Base
/// <summary>
/// sqlserver连接
/// </summary>
public static SqlSugarScope SqlScope() {
public static SqlSugarScope SqlScope()
{
string DBType = DBConnectionOption.DbType;
string ConnectionString = DBConnectionOption.ConnectionStrings;
@@ -43,12 +37,12 @@ namespace AntSK.Domain.Repositories.Base
}
};
DbType dbType = (DbType)Enum.Parse(typeof(DbType), DBType);
config.DbType = dbType;
var scope= new SqlSugarScope(config, Db =>
config.DbType = dbType;
var scope = new SqlSugarScope(config, Db =>
{
});
return scope;
}
}
}
}

View File

@@ -1,11 +1,6 @@
using AntSK.Domain.Model.Enum;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{
@@ -25,7 +20,7 @@ namespace AntSK.Domain.Repositories
/// 模型类型
/// </summary>
[Required]
public AIModelType AIModelType { get; set; }= AIModelType.Chat;
public AIModelType AIModelType { get; set; } = AIModelType.Chat;
/// <summary>
/// 模型地址
/// </summary>

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Utils
namespace AntSK.Domain.Utils
{
public static class ConvertUtils
{

View File

@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace AntSK.Domain.Utils
{

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.JSInterop;
namespace AntSK.Domain.Utils
{
public static class JSUtils
{
public static async Task ScrollToBottomAsync(this IJSRuntime _JSRuntime, string elementId)
{
await _JSRuntime.InvokeVoidAsync("scrollToBottom", elementId);
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Buffers.Text;
using System.Buffers.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

View File

@@ -1,11 +1,4 @@
using AntSK.Domain.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace AntSK.Domain.Utils
{
@@ -23,7 +16,7 @@ namespace AntSK.Domain.Utils
Regex regex = new Regex(@"(https?)://([^/:]+)(:\d+)?/(.*)");
Match match = regex.Match(_endPoint);
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" && request.Content != null)
{
{
string requestBody = await request.Content.ReadAsStringAsync();
//便于调试查看请求prompt
Console.WriteLine(requestBody);
@@ -47,14 +40,14 @@ namespace AntSK.Domain.Utils
{
// 这里是你要修改的 URL
Scheme = $"{xieyi}://{hostnew}/",
Host = host,
Host = host,
Path = route + "v1/chat/completions",
};
if (port.ConvertToInt32() != 0)
{
uriBuilder.Port = port.ConvertToInt32();
}
request.RequestUri = uriBuilder.Uri;
break;
@@ -83,9 +76,9 @@ namespace AntSK.Domain.Utils
}
public class OpenAIHttpClientHandlerUtil
public class OpenAIHttpClientHandlerUtil
{
public static HttpClient GetHttpClient( string endPoint)
public static HttpClient GetHttpClient(string endPoint)
{
var handler = new OpenAIHttpClientHandler(endPoint.ConvertToString());
var httpClient = new HttpClient(handler);

View File

@@ -1,6 +1,4 @@
using BCrypt.Net;
namespace AntSK.Domain.Utils
namespace AntSK.Domain.Utils
{
public class PasswordUtil
{

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace AntSK.Domain.Utils
{

View File

@@ -8,8 +8,8 @@
<NoWarn>CA1050,CA1707,CA2007,VSTHRD111,CS1591,RCS1110,CA5394,SKEXP0001,SKEXP0002,SKEXP0003,SKEXP0004,SKEXP0010,SKEXP0011,,SKEXP0012,SKEXP0020,SKEXP0021,SKEXP0022,SKEXP0023,SKEXP0024,SKEXP0025,SKEXP0026,SKEXP0027,SKEXP0028,SKEXP0029,SKEXP0030,SKEXP0031,SKEXP0032,SKEXP0040,SKEXP0041,SKEXP0042,SKEXP0050,SKEXP0051,SKEXP0052,SKEXP0053,SKEXP0054,SKEXP0055,SKEXP0060,SKEXP0061,SKEXP0101,SKEXP0102</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AntDesign.Charts" Version="0.5.1" />
<PackageReference Include="AntDesign.ProLayout" Version="0.17.3" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.2" />

View File

@@ -22,13 +22,13 @@
</summary>
<param name="_taskBroker"></param>
</member>
<member name="M:AntSK.Controllers.LLamaSharpController.chat(AntSK.Models.OpenAIModel)">
<member name="M:AntSK.Controllers.LLamaSharpController.chat(AntSK.Domain.Domain.Dto.OpenAIModel)">
<summary>
本地会话接口
</summary>
<returns></returns>
</member>
<member name="M:AntSK.Controllers.LLamaSharpController.embedding(AntSK.Models.OpenAIEmbeddingModel)">
<member name="M:AntSK.Controllers.LLamaSharpController.embedding(AntSK.Domain.Domain.Dto.OpenAIEmbeddingModel)">
<summary>
本地嵌入接口
</summary>
@@ -45,7 +45,7 @@
对外接口
</summary>
</member>
<member name="M:AntSK.Controllers.OpenController.chat(AntSK.Models.OpenAIModel)">
<member name="M:AntSK.Controllers.OpenController.chat(AntSK.Domain.Domain.Dto.OpenAIModel)">
<summary>
对话接口
</summary>
@@ -65,7 +65,7 @@
发送普通对话
</summary>
<param name="questions"></param>
<param name="msg"></param>
<param name="history"></param>
<param name="app"></param>
<returns></returns>
</member>
@@ -90,7 +90,7 @@
发送普通对话
</summary>
<param name="questions"></param>
<param name="msg"></param>
<param name="history"></param>
<param name="app"></param>
<returns></returns>
</member>
@@ -136,7 +136,7 @@
<param name="app"></param>
<returns></returns>
</member>
<member name="M:AntSK.Services.OpenApi.OpenApiService.HistorySummarize(AntSK.Domain.Repositories.Apps,AntSK.Models.OpenAIModel)">
<member name="M:AntSK.Services.OpenApi.OpenApiService.HistorySummarize(AntSK.Domain.Repositories.Apps,AntSK.Domain.Domain.Dto.OpenAIModel)">
<summary>
历史会话的会话总结
</summary>

View File

@@ -1,15 +1,12 @@
using AntDesign;
using AntDesign.ProLayout;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AntSK.Domain.Options;
using AntSK.Models;
using AntSK.Services;
using AntSK.Services.Auth;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
using AntSK.Services.Auth;
using AntSK.Domain.Options;
namespace AntSK.Components
{
@@ -56,7 +53,7 @@ namespace AntSK.Components
[Inject] public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
[Inject] protected MessageService? Message { get; set; }
private ClaimsPrincipal context => ((AntSKAuthProvider)AuthenticationStateProvider).GetCurrentUser();
private ClaimsPrincipal context => ((AntSKAuthProvider)AuthenticationStateProvider).GetCurrentUser();
protected override async Task OnInitializedAsync()
{
@@ -86,7 +83,7 @@ namespace AntSK.Components
{
NavigationManager.NavigateTo("/setting/user/info/" + context.Identity.Name);
}
else
else
{
_ = Message.Info("管理员无需设置", 2);
}

View File

@@ -1,7 +1,4 @@
using AntDesign;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net.NetworkInformation;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
{
@@ -32,10 +29,10 @@ namespace AntSK.Controllers
}
string extension = Path.GetExtension(file.FileName);
string fileid=Guid.NewGuid().ToString();
string fileid = Guid.NewGuid().ToString();
// 组合目标路径
var uploads = Path.Combine(uploadsFolderPath, fileid+extension);
var uploads = Path.Combine(uploadsFolderPath, fileid + extension);
// 保存文件至目标路径
using var fileStream = System.IO.File.Create(uploads);
using var uploadStream = file.OpenReadStream();

View File

@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Http;
using AntSK.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using AntSK.Domain.Repositories;
namespace AntSK.Controllers
{

View File

@@ -1,14 +1,10 @@
using AntSK.Domain.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static AntSK.Pages.KmsPage.KmsDetail;
using System;
using AntSK.Domain.Repositories;
using AntSK.BackgroundTask;
using AntSK.Domain.Domain.Interface;
using Microsoft.KernelMemory.Configuration;
using AntSK.Domain.Model.Enum;
using AntSK.Domain.Map;
using AntSK.BackgroundTask;
using AntSK.Domain.Model;
using AntSK.Domain.Model.Enum;
using AntSK.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
{
@@ -27,14 +23,14 @@ namespace AntSK.Controllers
IKmsDetails_Repositories kmsDetails_Repositories,
IKMService iKMService,
BackgroundTaskBroker<ImportKMSTaskReq> taskBroker
)
)
{
_kmsDetails_Repositories = kmsDetails_Repositories;
_iKMService = iKMService;
_taskBroker = taskBroker;
}
[HttpPost]
public async Task<IActionResult> ImportKMSTask(ImportKMSTaskDTO model)
public async Task<IActionResult> ImportKMSTask(ImportKMSTaskDTO model)
{
Console.WriteLine("api/kms/ImportKMSTask 开始");
ImportKMSTaskReq req = model.ToDTO<ImportKMSTaskReq>();

View File

@@ -1,8 +1,6 @@
using AntSK.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Dto;
using AntSK.Services.LLamaSharp;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
{
@@ -39,7 +37,7 @@ namespace AntSK.Controllers
public async Task embedding(OpenAIEmbeddingModel model)
{
Console.WriteLine("开始llama/v1/embeddings");
await _lLamaSharpService.Embedding(model,HttpContext);
await _lLamaSharpService.Embedding(model, HttpContext);
Console.WriteLine("结束llama/v1/embeddings");
}

View File

@@ -1,10 +1,6 @@
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Utils;
using AntSK.Models;
using AntSK.Models.OpenAPI;
using AntSK.Services.OpenApi;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
@@ -25,7 +21,7 @@ namespace AntSK.Controllers
public async Task chat(OpenAIModel model)
{
string sk = HttpContext.Request.Headers["Authorization"].ConvertToString();
await _openApiService.Chat(model,sk, HttpContext);
await _openApiService.Chat(model, sk, HttpContext);
}
}
}

View File

@@ -1,6 +1,4 @@
using System;
namespace AntSK
namespace AntSK
{
public static class DateTimeExtension
{

View File

@@ -1,5 +1,3 @@
using System;
namespace AntSK.Models
{
public class ActivitiesType

View File

@@ -1,6 +1,4 @@
using System;
namespace AntSK.Models
namespace AntSK.Models
{
public class StepFormModel
{

View File

@@ -1,6 +1,5 @@
using System;
using System.Text.Json.Serialization;
using AntSK.Domain.Utils;
using System.Text.Json.Serialization;
namespace AntSK.Models
{

View File

@@ -1,8 +1,6 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using System.IO;
using Microsoft.AspNetCore.Components;
using System.Text.RegularExpressions;
namespace AntSK.Pages.ApiPage
@@ -20,7 +18,7 @@ namespace AntSK.Pages.ApiPage
[Inject]
protected MessageService? Message { get; set; }
private Apis _apiModel = new Apis() ;
private Apis _apiModel = new Apis();
protected override async Task OnInitializedAsync()
@@ -38,7 +36,7 @@ namespace AntSK.Pages.ApiPage
{
//新增
_apiModel.Id = Guid.NewGuid().ToString();
if (_apis_Repositories.IsAny(p => p.Name == _apiModel.Name))
{
_ = Message.Error("名称已存在!", 2);
@@ -54,7 +52,8 @@ namespace AntSK.Pages.ApiPage
_apis_Repositories.Insert(_apiModel);
}
else {
else
{
//修改
_apis_Repositories.Update(_apiModel);
@@ -64,7 +63,8 @@ namespace AntSK.Pages.ApiPage
}
private void Back() {
private void Back()
{
NavigationManager.NavigateTo("/plugins/apilist");
}
}

View File

@@ -1,16 +1,14 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using AntSK.Services;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.ApiPage
{
public partial class ApiList
{
private Apis [] _data = { };
private Apis[] _data = { };
[Inject]
[Inject]
protected IApis_Repositories _apis_Repositories { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
@@ -33,7 +31,7 @@ namespace AntSK.Pages.ApiPage
{
data = await _apis_Repositories.GetListAsync(p => p.Name.Contains(searchKey));
}
list.AddRange(data);
_data = list.ToArray();
await InvokeAsync(StateHasChanged);
@@ -62,7 +60,7 @@ namespace AntSK.Pages.ApiPage
if (result == ConfirmResult.Yes)
{
await _apis_Repositories.DeleteAsync(id);
await InitData("");
await InitData("");
}
}
}

View File

@@ -1,9 +1,7 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using System.IO;
using AntSK.Domain.Model.Enum;
using AntSK.Domain.Repositories;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.AppPage
{
@@ -27,9 +25,9 @@ namespace AntSK.Pages.AppPage
[Inject]
protected IAIModels_Repositories _aimodels_Repositories { get; set; }
private Apps _appModel = new Apps() ;
private Apps _appModel = new Apps();
IEnumerable <string> kmsIds;
IEnumerable<string> kmsIds;
private List<Kmss> _kmsList = new List<Kmss>();
@@ -43,16 +41,16 @@ namespace AntSK.Pages.AppPage
{
await base.OnInitializedAsync();
_kmsList = _kmss_Repositories.GetList();
_apiList= _apis_Repositories.GetList();
_apiList = _apis_Repositories.GetList();
_chatList= _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Chat);
_chatList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Chat);
if (!string.IsNullOrEmpty(AppId))
{
//查看
_appModel= _apps_Repositories.GetFirst(p => p.Id == AppId);
_appModel = _apps_Repositories.GetFirst(p => p.Id == AppId);
kmsIds = _appModel.KmsIdList?.Split(",");
apiIds= _appModel.ApiFunctionList?.Split(",");
apiIds = _appModel.ApiFunctionList?.Split(",");
}
@@ -80,27 +78,29 @@ namespace AntSK.Pages.AppPage
//新增
_appModel.Id = Guid.NewGuid().ToString();
//秘钥
_appModel.SecretKey="sk-"+ Guid.NewGuid().ToString();
_appModel.SecretKey = "sk-" + Guid.NewGuid().ToString();
if (_apps_Repositories.IsAny(p => p.Name == _appModel.Name))
{
_ = Message.Error("名称已存在!", 2);
return;
}
_apps_Repositories.Insert(_appModel);
}
else {
else
{
//修改
_apps_Repositories.Update(_appModel);
}
//NavigationManager.NavigateTo($"/app/detail/{_appModel.Id}");
NavigationManager.NavigateTo($"/applist");
}
private void Back() {
private void Back()
{
NavigationManager.NavigateTo("/applist");
}
}

View File

@@ -1,16 +1,14 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using AntSK.Services;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.AppPage
{
public partial class AppList
{
private Apps [] _data = { };
private Apps[] _data = { };
[Inject]
[Inject]
protected IApps_Repositories _apps_Repositories { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
@@ -33,7 +31,7 @@ namespace AntSK.Pages.AppPage
{
data = await _apps_Repositories.GetListAsync(p => p.Name.Contains(searchKey));
}
list.AddRange(data);
_data = list.ToArray();
await InvokeAsync(StateHasChanged);
@@ -68,7 +66,7 @@ namespace AntSK.Pages.AppPage
if (result == ConfirmResult.Yes)
{
await _apps_Repositories.DeleteAsync(id);
await InitData("");
await InitData("");
}
}
}

View File

@@ -1,9 +1,7 @@
using AntDesign;
using AntSK.Domain.Model;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Repositories;
using AntSK.Models;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;
@@ -53,7 +51,7 @@ namespace AntSK.Pages.AppPage
await base.OnInitializedAsync();
_appModel = _apps_Repositories.GetFirst(p => p.Id == AppId);
_openApiUrl = NavigationManager.BaseUri + "api/v1/chat/completions";
_openChatUrl= NavigationManager.BaseUri + "openchat/"+AppId;
_openChatUrl = NavigationManager.BaseUri + "openchat/" + AppId;
GetDesc();
GetScript();
}
@@ -63,7 +61,7 @@ namespace AntSK.Pages.AppPage
_desc = @$"为了方便其他应用对接接口符合openai规范省略了温度TopP等参数。{Environment.NewLine}BaseUrl:{Environment.NewLine}{_openApiUrl} {Environment.NewLine}headers:{Environment.NewLine}Authorization: ""{_appModel.SecretKey}"" {Environment.NewLine}Body: {Environment.NewLine}{JsonConvert.SerializeObject(new OpenAIModel() { messages = new List<OpenAIMessage>() { new OpenAIMessage() { role = "user", content = "" } } }, Formatting.Indented)}";
}
private void GetScript()
private void GetScript()
{
_script = $"<script src=\"{NavigationManager.BaseUri}js/iframe.js\" data-width=\"40rem\" data-height=\"80vh\" id=\"antsk-iframe\" data-src=\"{NavigationManager.BaseUri}openchat/{AppId}\" data-color=\"#4e83fd\" data-message-icon-url=\"{NavigationManager.BaseUri}assets/ai.png\"></script>";
}

View File

@@ -30,12 +30,14 @@
<GridRow Style="width:100%">
<GridCol Span="23">
<div class="chat-bubble sent">
@(item.Context)
@* <span class="timestamp">@item.CreateTime</span> *@
<Popover Title="@item.CreateTime.ToString()">
@(item.Context)
</Popover>
</div>
<Icon Style="float:right;margin-top:10px;" Type="copy" Theme="outline" OnClick="async () =>await OnCopyAsync(item)" />
</GridCol>
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;margin-right:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
</GridCol>
</GridRow>
}
@@ -43,12 +45,11 @@
{
<GridRow Style="width:100%">
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
</GridCol>
<GridCol Span="23">
<div class="chat-bubble received">
@((MarkupString)(item.HtmlAnswers))
@* <span class="timestamp">@item.CreateTime</span> *@
</div>
</GridCol>
</GridRow>
@@ -76,8 +77,8 @@
</Extra>
<Body>
<AntList Bordered DataSource="@RelevantSources" Style="padding:10px;">
<ChildContent Context="item" >
<AntList Bordered DataSource="@_relevantSources" Style="padding:10px;">
<ChildContent Context="item">
<span> <b>@item.SourceName </b> 相似度:<Text Mark> @item.Relevance</Text></span>
<Body>
@((MarkupString)(@item.Text))
@@ -130,22 +131,7 @@
background-color: #daf8cb;
align-self: flex-end;
float: right;
}
.timestamp {
display: block;
font-size: 0.75em;
margin-top: 5px;
}
.received .timestamp {
text-align: right;
margin-right: 10px;
}
.sent .timestamp {
text-align: left;
margin-left: 10px;
position: relative;
}
</style>
@code {

View File

@@ -1,25 +1,18 @@
using AntDesign;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using Azure.AI.OpenAI;
using Azure.Core;
using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Wordprocessing;
using MarkdownSharp;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Microsoft.KernelMemory;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Newtonsoft.Json;
using RestSharp;
using SqlSugar;
using System;
using System.Text;
using AntSK.Domain.Utils;
namespace AntSK.Pages.ChatPage
{
@@ -37,15 +30,16 @@ namespace AntSK.Pages.ChatPage
protected IKmss_Repositories _kmss_Repositories { get; set; }
[Inject]
protected IKmsDetails_Repositories _kmsDetails_Repositories { get; set; }
[Inject] IJSRuntime _JSRuntime { get; set; }
//[Inject]
//protected Kernel _kernel { get; set; }
[Inject]
protected IKernelService _kernelService { get; set; }
[Inject]
protected IKMService _kMService { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
[Inject]
IChatService _chatService { get; set; }
protected bool _loading = false;
protected List<MessageInfo> MessageList = [];
@@ -53,7 +47,7 @@ namespace AntSK.Pages.ChatPage
protected string _json = "";
protected bool Sendding = false;
List<RelevantSource> RelevantSources = new List<RelevantSource>();
List<RelevantSource> _relevantSources = new List<RelevantSource>();
protected List<Apps> _list = new List<Apps>();
protected override async Task OnInitializedAsync()
@@ -94,7 +88,8 @@ namespace AntSK.Pages.ChatPage
catch (System.Exception ex)
{
Sendding = false;
_ = Message.Error("异常:"+ex.Message, 2);
Console.WriteLine("异常:" + ex.Message);
_ = Message.Error("异常:" + ex.Message, 2);
}
}
protected async Task OnCopyAsync(MessageInfo item)
@@ -105,7 +100,8 @@ namespace AntSK.Pages.ChatPage
});
}
protected async Task OnClearAsync() {
protected async Task OnClearAsync()
{
if (MessageList.Count > 0)
{
var content = "是否要清理会话记录";
@@ -129,7 +125,7 @@ namespace AntSK.Pages.ChatPage
Apps app = _apps_Repositories.GetFirst(p => p.Id == AppId);
if (MessageList.Count > 0)
{
msg = await HistorySummarize(app,questions);
msg = await HistorySummarize(app, questions);
}
switch (app.Type)
@@ -156,115 +152,69 @@ namespace AntSK.Pages.ChatPage
/// <returns></returns>
private async Task SendKms(string questions, string msg, Apps app)
{
var _kernel = _kernelService.GetKernelByApp(app);
//知识库问答
var filters = new List<MemoryFilter>();
var kmsidList = app.KmsIdList.Split(",");
//只取第一个知识库的配置
var _memory = _kMService.GetMemoryByKMS(kmsidList.FirstOrDefault());
foreach (var kmsid in kmsidList)
{
filters.Add(new MemoryFilter().ByTag("kmsid", kmsid));
}
var xlresult = await _memory.SearchAsync(questions, index: "kms", filters: filters);
string dataMsg = "";
if (xlresult != null)
{
foreach (var item in xlresult.Results)
{
foreach (var part in item.Partitions)
{
dataMsg += $"[file:{item.SourceName};Relevance:{(part.Relevance*100).ToString("F2")}%]:{part.Text}{Environment.NewLine}";
//输出调试信息
var markdown = new Markdown();
string sourceName = item.SourceName;
var fileDetail = _kmsDetails_Repositories.GetFirst(p => p.FileGuidName == item.SourceName);
if (fileDetail.IsNotNull())
{
sourceName = fileDetail.FileName;
}
RelevantSources.Add(new RelevantSource() { SourceName = sourceName, Text = markdown.Transform(part.Text), Relevance = part.Relevance });
}
}
KernelFunction jsonFun = _kernel.Plugins.GetFunction("KMSPlugin", "Ask");
var chatResult = _kernel.InvokeStreamingAsync<StreamingTextContent>(function: jsonFun,
arguments: new KernelArguments() { ["doc"] = dataMsg, ["history"] = msg, ["questions"]=questions });
MessageInfo info = null;
var markdown1 = new Markdown();
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Text?.ConvertToString();
info.HtmlAnswers = content?.Text?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Text;
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
}
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="msg"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string msg, Apps app)
{
var _kernel = _kernelService.GetKernelByApp(app);
if (string.IsNullOrEmpty(app.Prompt)||!app.Prompt.Contains("{{$input}}"))
{
//如果模板为空,给默认提示词
app.Prompt = app.Prompt.ConvertToString()+"{{$input}}";
}
var temperature = app.Temperature/100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() {Temperature= temperature };
if (!string.IsNullOrEmpty(app.ApiFunctionList))
{
_kernelService.ImportFunctionsByApp(app, _kernel);
settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,Temperature = temperature };
}
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var chatResult = _kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function: func, arguments: new KernelArguments() { ["input"] = msg });
MessageInfo info = null;
var markdown = new Markdown();
var markdown1 = new Markdown();
var chatResult = _chatService.SendKmsByAppAsync(app, questions, msg, _relevantSources);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Content?.ConvertToString();
info.HtmlAnswers = content?.Content?.ConvertToString();
info.Context = content?.ConvertToString();
info.HtmlAnswers = content?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Content;
await Task.Delay(50);
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="history"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string history, Apps app)
{
MessageInfo info =null;
var markdown = new Markdown();
var chatResult = _chatService.SendChatByAppAsync(app, questions, history);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.ConvertToString();
info.HtmlAnswers = content?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
@@ -273,18 +223,18 @@ namespace AntSK.Pages.ChatPage
{
info!.HtmlAnswers = markdown.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
/// 历史会话的会话总结
/// </summary>
/// <param name="questions"></param>
/// <returns></returns>
private async Task<string> HistorySummarize(Apps app,string questions)
private async Task<string> HistorySummarize(Apps app, string questions)
{
var _kernel = _kernelService.GetKernelByApp(app);
if (MessageList.Count > 1)
@@ -307,24 +257,19 @@ namespace AntSK.Pages.ChatPage
var msg = await _kernelService.HistorySummarize(_kernel, questions, history.ToString());
return msg;
}
else
else
{
var msg = $"history{history.ToString()}{Environment.NewLine} user{questions}"; ;
var msg = $"history{history.ToString()}{Environment.NewLine}";
return msg;
}
}
}
else
else
{
return questions;
return "";
}
}
}
public class RelevantSource
{
public string SourceName { get; set; }
public string Text { get; set; }
public float Relevance { get; set; }
}
}

View File

@@ -14,12 +14,14 @@
<GridRow>
<GridCol Span="23">
<div class="chat-bubble sent">
@(item.Context)
@* <span class="timestamp">@item.CreateTime</span> *@
<Popover Title="@item.CreateTime.ToString()">
@(item.Context)
</Popover>
</div>
<Icon Style="float:right;margin-top:10px;" Type="copy" Theme="outline" OnClick="async () =>await OnCopyAsync(item)" />
</GridCol>
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
</GridCol>
</GridRow>
}
@@ -27,13 +29,13 @@
{
<GridRow>
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
</GridCol>
<GridCol Span="23">
<div class="chat-bubble received">
@((MarkupString)(item.HtmlAnswers))
@* <span class="timestamp">@item.CreateTime</span> *@
</div>
</GridCol>
</GridRow>
}
@@ -92,22 +94,6 @@
align-self: flex-end;
float: right;
}
.timestamp {
display: block;
font-size: 0.75em;
margin-top: 5px;
}
.received .timestamp {
text-align: right;
margin-right: 10px;
}
.sent .timestamp {
text-align: left;
margin-left: 10px;
}
</style>
@code {

View File

@@ -1,24 +1,17 @@
using AntDesign;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using Azure.AI.OpenAI;
using Azure.Core;
using DocumentFormat.OpenXml.EMMA;
using MarkdownSharp;
using Microsoft.AspNetCore.Components;
using Microsoft.KernelMemory;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Text;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Service;
using Microsoft.JSInterop;
namespace AntSK.Pages.ChatPage
{
@@ -26,7 +19,7 @@ namespace AntSK.Pages.ChatPage
{
[Parameter]
public string AppId { get; set; }
[Inject]
[Inject]
protected MessageService? Message { get; set; }
[Inject]
protected IApps_Repositories _apps_Repositories { get; set; }
@@ -40,6 +33,10 @@ namespace AntSK.Pages.ChatPage
protected IKMService _kMService { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
[Inject]
IChatService _chatService { get; set; }
[Inject] IJSRuntime _JSRuntime { get; set; }
protected bool _loading = false;
protected List<MessageInfo> MessageList = [];
@@ -52,7 +49,7 @@ namespace AntSK.Pages.ChatPage
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
app = _apps_Repositories.GetFirst(p=>p.Id==AppId);
app = _apps_Repositories.GetFirst(p => p.Id == AppId);
}
protected async Task OnClearAsync()
@@ -100,7 +97,8 @@ namespace AntSK.Pages.ChatPage
catch (System.Exception ex)
{
Sendding = false;
_ = Message.Error("异常:"+ex.Message, 2);
Console.WriteLine("异常:" + ex.Message);
_ = Message.Error("异常:" + ex.Message, 2);
}
}
@@ -124,10 +122,10 @@ namespace AntSK.Pages.ChatPage
{
string msg = "";
//处理多轮会话
Apps app=_apps_Repositories.GetFirst(p => p.Id == AppId);
Apps app = _apps_Repositories.GetFirst(p => p.Id == AppId);
if (MessageList.Count > 0)
{
msg = await HistorySummarize(app,questions);
msg = await HistorySummarize(app, questions);
}
switch (app.Type)
{
@@ -153,114 +151,72 @@ namespace AntSK.Pages.ChatPage
/// <returns></returns>
private async Task SendKms(string questions, string msg, Apps app)
{
var _kernel = _kernelService.GetKernelByApp(app);
var _memory = _kMService.GetMemoryByKMS(app.KmsIdList.Split(",").FirstOrDefault());
//知识库问答
var filters = new List<MemoryFilter>();
var kmsidList = app.KmsIdList.Split(",");
foreach (var kmsid in kmsidList)
{
filters.Add(new MemoryFilter().ByTag("kmsid", kmsid));
}
var xlresult = await _memory.SearchAsync(questions, index: "kms", filters: filters);
string dataMsg = "";
if (xlresult != null)
{
foreach (var item in xlresult.Results)
{
foreach (var part in item.Partitions)
{
dataMsg += $"[file:{item.SourceName};Relevance:{(part.Relevance * 100).ToString("F2")}%]:{part.Text}{Environment.NewLine}";
}
}
KernelFunction jsonFun = _kernel.Plugins.GetFunction("KMSPlugin", "Ask");
var chatResult = _kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function: jsonFun,
arguments: new KernelArguments() { ["doc"] = dataMsg, ["history"] = msg, ["questions"] = questions });
MessageInfo info = null;
var markdown1 = new Markdown();
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Content?.ConvertToString();
info.HtmlAnswers = content?.Content?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Content;
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
}
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="msg"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string msg, Apps app)
{
var _kernel= _kernelService.GetKernelByApp(app);
if (string.IsNullOrEmpty(app.Prompt) || !app.Prompt.Contains("{{$input}}"))
{
//如果模板为空,给默认提示词
app.Prompt = app.Prompt.ConvertToString() + "{{$input}}";
}
//注册插件
var temperature = app.Temperature / 100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() { Temperature = temperature };
if (!string.IsNullOrEmpty(app.ApiFunctionList))
{
_kernelService.ImportFunctionsByApp(app, _kernel);
settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions, Temperature = temperature };
}
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var chatResult = _kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function: func, arguments: new KernelArguments() { ["input"] = msg });
MessageInfo info = null;
var markdown = new Markdown();
var markdown1 = new Markdown();
var chatResult=_chatService.SendKmsByAppAsync(app, questions, msg);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Content?.ConvertToString();
info.HtmlAnswers = content?.Content?.ConvertToString();
info.Context = content.ConvertToString();
info.HtmlAnswers = content.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Content;
await Task.Delay(50);
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="history"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string history, Apps app)
{
MessageInfo info = null;
var markdown = new Markdown();
var chatResult = _chatService.SendChatByAppAsync(app, questions, history);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content.ConvertToString();
info.HtmlAnswers = content.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
info!.HtmlAnswers = markdown.Transform(info.HtmlAnswers);
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
@@ -268,7 +224,7 @@ namespace AntSK.Pages.ChatPage
/// </summary>
/// <param name="questions"></param>
/// <returns></returns>
private async Task<string> HistorySummarize(Apps app,string questions)
private async Task<string> HistorySummarize(Apps app, string questions)
{
var _kernel = _kernelService.GetKernelByApp(app);
if (MessageList.Count > 1)
@@ -297,9 +253,9 @@ namespace AntSK.Pages.ChatPage
return msg;
}
}
else
else
{
return questions;
return "";
}
}
}

View File

@@ -1,10 +1,7 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using System.IO;
using AntSK.Domain.Model.Enum;
using DocumentFormat.OpenXml.Wordprocessing;
using AntSK.Domain.Repositories;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.KmsPage
{
@@ -31,12 +28,12 @@ namespace AntSK.Pages.KmsPage
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_chatList = _aimodels_Repositories.GetList(p=>p.AIModelType==AIModelType.Chat);
_chatList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Chat);
_embeddingList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Embedding);
if (!string.IsNullOrEmpty(KmsId))
{
//查看
_kmsModel =await _kmss_Repositories.GetFirstAsync(p => p.Id == KmsId);
_kmsModel = await _kmss_Repositories.GetFirstAsync(p => p.Id == KmsId);
}
}
private void HandleSubmit()
@@ -60,7 +57,7 @@ namespace AntSK.Pages.KmsPage
{
_ = Message.Error("名称已存在!", 2);
return;
}
}
_kmss_Repositories.Insert(_kmsModel);
}
else
@@ -71,5 +68,5 @@ namespace AntSK.Pages.KmsPage
NavigationManager.NavigateTo("/kmslist");
}
}
}
}

View File

@@ -1,23 +1,13 @@
using AntDesign;
using AntSK.BackgroundTask;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Service;
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using AntSK.Models;
using AntSK.Services;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Vml.Spreadsheet;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.KernelMemory;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Security.Claims;
using System.Security.Policy;
namespace AntSK.Pages.KmsPage
{
@@ -54,7 +44,7 @@ namespace AntSK.Pages.KmsPage
{"success", ProgressStatus.Success}
};
private List<KmsDetails> _data =new List<KmsDetails>();
private List<KmsDetails> _data = new List<KmsDetails>();
[Inject]
protected IConfirmService _confirmService { get; set; }
@@ -62,7 +52,7 @@ namespace AntSK.Pages.KmsPage
protected IKmsDetails_Repositories _kmsDetails_Repositories { get; set; }
[Inject]
protected IKmss_Repositories _kmss_Repositories { get; set; }
protected IKmss_Repositories _kmss_Repositories { get; set; }
private MemoryServerless _memory { get; set; }
[Inject]
@@ -78,14 +68,14 @@ namespace AntSK.Pages.KmsPage
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_data =await _kmsDetails_Repositories.GetListAsync(p => p.KmsId == KmsId);
_data = await _kmsDetails_Repositories.GetListAsync(p => p.KmsId == KmsId);
var km = _kmss_Repositories.GetFirst(p => p.Id == KmsId);
//使用知识库设置的参数,
_memory = iKMService.GetMemoryByKMS(km.Id);
}
//刷新
private async Task Refresh()
private async Task Refresh()
{
_data = await _kmsDetails_Repositories.GetListAsync(p => p.KmsId == KmsId);
}
@@ -105,7 +95,7 @@ namespace AntSK.Pages.KmsPage
{
try
{
var result= await _httpService.PostAsync(NavigationManager.BaseUri + "api/KMS/ImportKMSTask", new ImportKMSTaskDTO()
var result = await _httpService.PostAsync(NavigationManager.BaseUri + "api/KMS/ImportKMSTask", new ImportKMSTaskDTO()
{
ImportType = ImportType.Url,
KmsId = KmsId,
@@ -117,7 +107,7 @@ namespace AntSK.Pages.KmsPage
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message+" ---- "+ex.StackTrace);
Console.WriteLine(ex.Message + " ---- " + ex.StackTrace);
}
}
private void UrlHandleCancel(MouseEventArgs e)
@@ -141,7 +131,7 @@ namespace AntSK.Pages.KmsPage
{
try
{
var result =await _httpService.PostAsync(NavigationManager.BaseUri + "api/KMS/ImportKMSTask", new ImportKMSTaskDTO()
var result = await _httpService.PostAsync(NavigationManager.BaseUri + "api/KMS/ImportKMSTask", new ImportKMSTaskDTO()
{
ImportType = ImportType.Text,
KmsId = KmsId,
@@ -215,17 +205,17 @@ namespace AntSK.Pages.KmsPage
"text/x-markdown",
"text/markdown"
};
var IsType = types.Contains( file.Type );
if (!IsType&& file.Ext != ".md")
var IsType = types.Contains(file.Type);
if (!IsType && file.Ext != ".md")
{
_message.Error("文件格式错误,请重新选择!");
}
var IsLt500K = file.Size < 1024 *1024* 100;
var IsLt500K = file.Size < 1024 * 1024 * 100;
if (!IsLt500K)
{
_message.Error("文件需不大于100MB!");
}
return IsType && IsLt500K;
}
private void OnSingleCompleted(UploadInfo fileinfo)
@@ -233,8 +223,8 @@ namespace AntSK.Pages.KmsPage
if (fileinfo.File.State == UploadState.Success)
{
filePath=fileinfo.File.Url = fileinfo.File.Response;
fileName= fileinfo.File.FileName;
filePath = fileinfo.File.Url = fileinfo.File.Response;
fileName = fileinfo.File.FileName;
}
}
@@ -244,7 +234,7 @@ namespace AntSK.Pages.KmsPage
}
private async Task DeleteFile(string fileid)
{
{
try
{
var content = "是否确认删除此文档?";

View File

@@ -1,6 +1,5 @@
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Domain.Interface;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.KmsPage
@@ -15,7 +14,7 @@ namespace AntSK.Pages.KmsPage
[Inject]
protected IKMService iKMService { get; set; }
private List<KMFile> _data = new List<KMFile>() ;
private List<KMFile> _data = new List<KMFile>();
protected override async Task OnInitializedAsync()
{
@@ -23,8 +22,9 @@ namespace AntSK.Pages.KmsPage
_data = await iKMService.GetDocumentByFileID(KmsId, FileId);
}
private void NavigateBack() {
private void NavigateBack()
{
NavigationManager.NavigateTo($"/kms/detail/{KmsId}");
}
}
}
}

View File

@@ -1,14 +1,7 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using AntSK.Services;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Drawing.Charts;
using System.Collections.Generic;
using System;
using Microsoft.KernelMemory;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Repositories;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages
{
@@ -53,11 +46,11 @@ namespace AntSK.Pages
{
data = await _kmss_Repositories.GetListAsync();
}
else
else
{
data = await _kmss_Repositories.GetListAsync(p => p.Name.Contains(searchKey));
}
list.AddRange(data);
_data = list.ToArray();
await InvokeAsync(StateHasChanged);
@@ -88,7 +81,7 @@ namespace AntSK.Pages
var content = "删除知识库会一起删除导入的知识文档,无法还原。是否确认删除此知识库?";
var title = "删除";
var result= await _confirmService.Show(content, title, ConfirmButtons.YesNo);
var result = await _confirmService.Show(content, title, ConfirmButtons.YesNo);
if (result == ConfirmResult.Yes)
{
var _memory = _kMService.GetMemoryByKMS(id);
@@ -104,7 +97,7 @@ namespace AntSK.Pages
await _kmss_Repositories.DeleteAsync(id);
await InitData("");
}
}
}
}
}

View File

@@ -1,9 +1,7 @@
using AntDesign.ProLayout;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Threading.Tasks;
using AntSK.Models;
using AntSK.Services;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.Profile
{

View File

@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;
using AntSK.Models;
using AntSK.Services;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.Profile
{

Some files were not shown because too many files have changed in this diff Show More