From f803b9538bc40fe7613249a3e34309239c924bc1 Mon Sep 17 00:00:00 2001 From: zeyu xu <286513187@qq.com> Date: Sat, 20 Apr 2024 21:17:27 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E8=B0=83=E6=95=B4=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AntSK.Domain/AntSK.Domain.xml | 4 +-- .../HuggingfaceTextEmbeddingGenerator.cs | 2 +- .../Domain/Other/{ => Bge}/BegRerankConfig.cs | 2 +- .../Other/{ => Bge}/BgeEmbeddingConfig.cs | 14 ++++----- .../Domain/Service/ChatService.cs | 7 +++++ src/AntSK.Domain/Domain/Service/KMService.cs | 30 ++++++++++++++----- .../Pages/Setting/AIModel/AddModel.razor.cs | 2 +- 7 files changed, 42 insertions(+), 19 deletions(-) rename src/AntSK.Domain/Domain/Other/{ => Bge}/BegRerankConfig.cs (97%) rename src/AntSK.Domain/Domain/Other/{ => Bge}/BgeEmbeddingConfig.cs (90%) diff --git a/src/AntSK.Domain/AntSK.Domain.xml b/src/AntSK.Domain/AntSK.Domain.xml index a1b376e..228bdba 100644 --- a/src/AntSK.Domain/AntSK.Domain.xml +++ b/src/AntSK.Domain/AntSK.Domain.xml @@ -177,12 +177,12 @@ 总数 - + 模型写死 - + 模型写死 diff --git a/src/AntSK.Domain/Common/Embedding/HuggingfaceTextEmbeddingGenerator.cs b/src/AntSK.Domain/Common/Embedding/HuggingfaceTextEmbeddingGenerator.cs index 8f4948a..a7396b3 100644 --- a/src/AntSK.Domain/Common/Embedding/HuggingfaceTextEmbeddingGenerator.cs +++ b/src/AntSK.Domain/Common/Embedding/HuggingfaceTextEmbeddingGenerator.cs @@ -7,7 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AntSK.Domain.Domain.Other; +using AntSK.Domain.Domain.Other.Bge; namespace AntSK.Domain.Common.Embedding { diff --git a/src/AntSK.Domain/Domain/Other/BegRerankConfig.cs b/src/AntSK.Domain/Domain/Other/Bge/BegRerankConfig.cs similarity index 97% rename from src/AntSK.Domain/Domain/Other/BegRerankConfig.cs rename to src/AntSK.Domain/Domain/Other/Bge/BegRerankConfig.cs index 4deb6ad..f1b9e5e 100644 --- a/src/AntSK.Domain/Domain/Other/BegRerankConfig.cs +++ b/src/AntSK.Domain/Domain/Other/Bge/BegRerankConfig.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AntSK.Domain.Domain.Other +namespace AntSK.Domain.Domain.Other.Bge { public static class BegRerankConfig { diff --git a/src/AntSK.Domain/Domain/Other/BgeEmbeddingConfig.cs b/src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs similarity index 90% rename from src/AntSK.Domain/Domain/Other/BgeEmbeddingConfig.cs rename to src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs index a9ae786..543bd68 100644 --- a/src/AntSK.Domain/Domain/Other/BgeEmbeddingConfig.cs +++ b/src/AntSK.Domain/Domain/Other/Bge/BgeEmbeddingConfig.cs @@ -7,7 +7,7 @@ using System.Text; using System.Threading.Tasks; using static Python.Runtime.Py; -namespace AntSK.Domain.Domain.Other +namespace AntSK.Domain.Domain.Other.Bge { public static class BgeEmbeddingConfig { @@ -30,15 +30,15 @@ namespace AntSK.Domain.Domain.Other Runtime.PythonDLL = pythondllPath; PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); - + try { - using (Py.GIL())// 初始化Python环境的Global Interpreter Lock) + using (GIL())// 初始化Python环境的Global Interpreter Lock) { - dynamic modelscope = Py.Import("modelscope"); + dynamic modelscope = Import("modelscope"); //dynamic model_dir = modelscope.snapshot_download("AI-ModelScope/bge-large-zh-v1.5", revision: "master"); dynamic model_dir = modelscope.snapshot_download(modelName, revision: "master"); - dynamic HuggingFaceBgeEmbeddingstemp = Py.Import("langchain_community.embeddings.huggingface"); + dynamic HuggingFaceBgeEmbeddingstemp = Import("langchain_community.embeddings.huggingface"); dynamic HuggingFaceBgeEmbeddings = HuggingFaceBgeEmbeddingstemp.HuggingFaceBgeEmbeddings; string model_name = model_dir; dynamic model_kwargs = new PyDict(); @@ -51,7 +51,7 @@ namespace AntSK.Domain.Domain.Other return hugginmodel; } } - catch(Exception ex) + catch (Exception ex) { throw ex; } @@ -63,7 +63,7 @@ namespace AntSK.Domain.Domain.Other public static Task GetEmbedding(string queryStr) { - using (Py.GIL()) + using (GIL()) { PyObject queryResult = model.embed_query(queryStr); var floatList = queryResult.As(); diff --git a/src/AntSK.Domain/Domain/Service/ChatService.cs b/src/AntSK.Domain/Domain/Service/ChatService.cs index 3a894c8..623b92f 100644 --- a/src/AntSK.Domain/Domain/Service/ChatService.cs +++ b/src/AntSK.Domain/Domain/Service/ChatService.cs @@ -100,9 +100,16 @@ namespace AntSK.Domain.Domain.Service }))); } + var dataMsg = new StringBuilder(); if (relevantSourceList.Any()) { + if (!string.IsNullOrEmpty(app.RerankModelID)) + { + //进行rerank + + } + bool isSearch = false; foreach (var item in relevantSourceList) { diff --git a/src/AntSK.Domain/Domain/Service/KMService.cs b/src/AntSK.Domain/Domain/Service/KMService.cs index 9d4d465..7555e05 100644 --- a/src/AntSK.Domain/Domain/Service/KMService.cs +++ b/src/AntSK.Domain/Domain/Service/KMService.cs @@ -45,14 +45,30 @@ namespace AntSK.Domain.Domain.Service var embedModel = _aIModels_Repositories.GetFirst(p => p.Id == app.EmbeddingModelID); var chatHttpClient = OpenAIHttpClientHandlerUtil.GetHttpClient(chatModel.EndPoint); var embeddingHttpClient = OpenAIHttpClientHandlerUtil.GetHttpClient(embedModel.EndPoint); - - var searchClientConfig = new SearchClientConfig + SearchClientConfig searchClientConfig; + if (string.IsNullOrEmpty(app.RerankModelID)) { - MaxAskPromptSize = app.MaxAskPromptSize, - MaxMatchesCount = app.MaxMatchesCount, - AnswerTokens = app.AnswerTokens, - EmptyAnswer = KmsConstantcs.KmsSearchNull - }; + //不重排直接取查询数 + searchClientConfig = new SearchClientConfig + { + MaxAskPromptSize = app.MaxAskPromptSize, + MaxMatchesCount = app.MaxMatchesCount, + AnswerTokens = app.AnswerTokens, + EmptyAnswer = KmsConstantcs.KmsSearchNull + }; + } + else + { + //重排取rerank数 + searchClientConfig = new SearchClientConfig + { + MaxAskPromptSize = app.MaxAskPromptSize, + MaxMatchesCount = app.RerankCount, + AnswerTokens = app.AnswerTokens, + EmptyAnswer = KmsConstantcs.KmsSearchNull + }; + } + var memoryBuild = new KernelMemoryBuilder() .WithSearchClientConfig(searchClientConfig) diff --git a/src/AntSK/Pages/Setting/AIModel/AddModel.razor.cs b/src/AntSK/Pages/Setting/AIModel/AddModel.razor.cs index b3264f2..e368683 100644 --- a/src/AntSK/Pages/Setting/AIModel/AddModel.razor.cs +++ b/src/AntSK/Pages/Setting/AIModel/AddModel.razor.cs @@ -3,7 +3,7 @@ using AntDesign.ProLayout; using AntSK.Domain.Domain.Interface; using AntSK.Domain.Domain.Model.Constant; using AntSK.Domain.Domain.Model.Enum; -using AntSK.Domain.Domain.Other; +using AntSK.Domain.Domain.Other.Bge; using AntSK.Domain.Domain.Service; using AntSK.Domain.Options; using AntSK.Domain.Repositories;