add bgererank 模型下载

This commit is contained in:
zeyu xu
2024-04-20 20:56:00 +08:00
parent f40dd8b013
commit 93288f9b5c
6 changed files with 160 additions and 22 deletions

View File

@@ -199,6 +199,11 @@
<member name="M:AntSK.Domain.Domain.Other.QAHandler.InvokeAsync(Microsoft.KernelMemory.Pipeline.DataPipeline,System.Threading.CancellationToken)">
<inheritdoc />
</member>
<member name="M:AntSK.Domain.Domain.Other.RerankConfig.LoadModel(System.String,System.String)">
<summary>
模型写死
</summary>
</member>
<member name="M:AntSK.Domain.Domain.Service.ChatService.SendChatByAppAsync(AntSK.Domain.Repositories.Apps,System.String,Microsoft.SemanticKernel.ChatCompletion.ChatHistory)">
<summary>
发送消息

View File

@@ -26,8 +26,10 @@ namespace AntSK.Domain.Domain.Model.Enum
LLamaFactory = 6,
[Display(Name = "Bge Embedding")]
BgeEmbedding = 7,
[Display(Name = "Bge Rerank")]
BgeRerank = 8,
[Display(Name = "StableDiffusion")]
StableDiffusion = 8,
StableDiffusion = 9,
[Display(Name = "模拟输出")]
Mock = 100,
@@ -41,5 +43,6 @@ namespace AntSK.Domain.Domain.Model.Enum
Chat = 1,
Embedding = 2,
Image=3,
Rerank=4
}
}

View File

@@ -0,0 +1,55 @@
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Other
{
public static class RerankConfig
{
public static dynamic model { get; set; }
static object lockobj = new object();
/// <summary>
/// 模型写死
/// </summary>
public static dynamic LoadModel(string pythondllPath, string modelName)
{
lock (lockobj)
{
if (model == null)
{
Runtime.PythonDLL = pythondllPath;
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();
try
{
using (Py.GIL())// 初始化Python环境的Global Interpreter Lock)
{
dynamic modelscope = Py.Import("modelscope");
dynamic flagEmbedding = Py.Import("FlagEmbedding");
dynamic model_dir = modelscope.snapshot_download(modelName, revision: "master");
dynamic flagReranker = flagEmbedding.FlagReranker(model_dir, use_fp16: true);
model = flagReranker;
return model;
}
}
catch (Exception ex)
{
throw ex;
}
}
else
{
return model;
}
}
}
}
}

View File

@@ -17,4 +17,5 @@ matplotlib
fire
modelscope
langchain-community
sentence_transformers
sentence_transformers
FlagEmbedding

View File

@@ -27,21 +27,25 @@
<EnumRadioGroup @bind-Value="context.AIType" ButtonStyle="RadioButtonStyle.Solid" OnChange="AITypeChange" TEnum="AIType"> </EnumRadioGroup>
</FormItem>
<FormItem Label="模型类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<RadioGroup @bind-Value="context.AIModelType">
<RadioGroup @bind-Value="context.AIModelType">
@if (context.AIType == AIType.StableDiffusion)
{
<Radio RadioButton Value="@(AIModelType.Image)">图片模型</Radio>
}
else
else
{
@if (context.AIType != AIType.BgeEmbedding)
@if (context.AIType != AIType.BgeEmbedding && context.AIType != AIType.BgeRerank)
{
<Radio RadioButton Value="@(AIModelType.Chat)">会话模型</Radio>
}
@if (context.AIType != AIType.LLamaFactory && context.AIType != AIType.Mock && context.AIType != AIType.SparkDesk)
@if (context.AIType != AIType.LLamaFactory && context.AIType != AIType.Mock && context.AIType != AIType.SparkDesk && context.AIType != AIType.BgeRerank)
{
<Radio RadioButton Value="@(AIModelType.Embedding)">向量模型</Radio>
}
@if (context.AIType == AIType.BgeRerank)
{
<Radio RadioButton Value="@(AIModelType.Rerank)">Rerank重排模型</Radio>
}
}
</RadioGroup>
</FormItem>
@@ -140,7 +144,7 @@
<Button OnClick="StopLFService" Disabled="@(!llamaFactoryIsStart)">停止</Button>
}
</InputGroup>
</FormItem>
</FormItem>
}
@if (context.AIType == AIType.BgeEmbedding)
@@ -152,19 +156,45 @@
LabelProperty="c=>c">
</Select>
</FormItem>
<FormItem Label="PythonDll路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Input Placeholder="D:\Programs\Python\Python311\python311.dll" @bind-Value="@context.EndPoint" />
<FormItem Label="Python路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Popover ContentTemplate="@_content" Title="提示" Style="width:100%">
<Input Placeholder="D:\Programs\Python\Python311\python311.dll" @bind-Value="@context.EndPoint" />
</Popover>
</FormItem>
<FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Button Type="primary" OnClick="PipInstall">环境安装</Button>
</FormItem>
<FormItem Label="下载并初始化" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Spin Tip="请等待..." Spinning="@(BgeEmbeddingIsStart)">
<Button Type="primary" Disabled="@(BgeEmbeddingIsStart)" OnClick="BgeEmbedding">@BgeEmbeddingBtnText</Button>
</Spin>
</FormItem>
}
@if (context.AIType == AIType.BgeRerank)
{
<FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Select DataSource="@bgeRerankList"
@bind-Value="@context.ModelName"
ValueProperty="c=>c"
LabelProperty="c=>c">
</Select>
</FormItem>
<FormItem Label="Python路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Popover ContentTemplate="@_content" Title="提示" Style="width:100%">
<Input Placeholder="D:\Programs\Python\Python311\python311.dll" @bind-Value="@context.EndPoint" />
</Popover>
</FormItem>
<FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Button Type="primary" OnClick="PipInstall">环境安装</Button>
</FormItem>
<FormItem Label="下载并初始化" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Spin Tip="请等待..." Spinning="@(BgeIsStart)" >
<Button Type="primary" Disabled="@(BgeIsStart)" OnClick="BgeDownload">@BgeBtnText</Button>
<Spin Tip="请等待..." Spinning="@(BgeRerankIsStart)">
<Button Type="primary" Disabled="@(BgeRerankIsStart)" OnClick="BgeRerank">@BgeRerankBtnText</Button>
</Spin>
</FormItem>
}
@if (context.AIType == AIType.Mock)
{
}
@@ -206,5 +236,8 @@
@code
{
RenderFragment _content =
@<div>
<p>在windows上使用 python311.dll文件在linux上使用 libpython3.10.so</p>
</div>;
}

View File

@@ -61,8 +61,13 @@ namespace AntSK.Pages.Setting.AIModel
private bool _logModalVisible;
private List<string> bgeEmbeddingList = new List<string>() { "AI-ModelScope/bge-small-zh-v1.5", "AI-ModelScope/bge-base-zh-v1.5", "AI-ModelScope/bge-large-zh-v1.5" };
private bool BgeIsStart = false;
private string BgeBtnText = "初始化";
private List<string> bgeRerankList = new List<string>() { "Xorbits/bge-reranker-base", "Xorbits/bge-reranker-large", "AI-ModelScope/bge-reranker-v2-m3", "AI-ModelScope/bge-reranker-v2-gemma"};
private bool BgeEmbeddingIsStart = false;
private string BgeEmbeddingBtnText = "初始化";
private bool BgeRerankIsStart = false;
private string BgeRerankBtnText = "初始化";
protected override async Task OnInitializedAsync()
{
@@ -268,7 +273,7 @@ namespace AntSK.Pages.Setting.AIModel
}
}
private async Task BgeDownload()
private async Task BgeEmbedding()
{
if (string.IsNullOrEmpty(_aiModel.ModelName))
{
@@ -277,27 +282,60 @@ namespace AntSK.Pages.Setting.AIModel
}
if (string.IsNullOrEmpty(_aiModel.EndPoint))
{
_ = Message.Error("请输入正确的Python dll路径", 2);
_ = Message.Error("请输入正确的Python dll或python so路径!", 2);
return;
}
BgeIsStart = true;
BgeBtnText = "正在初始化...";
BgeEmbeddingIsStart = true;
BgeEmbeddingBtnText = "正在初始化...";
await Task.Run(() =>
{
try
{
EmbeddingConfig.LoadModel(_aiModel.EndPoint, _aiModel.ModelName);
BgeBtnText = "初始化完成";
BgeIsStart = false;
BgeEmbeddingBtnText = "初始化完成";
BgeEmbeddingIsStart = false;
}
catch (System.Exception ex)
{
_ = Message.Error(ex.Message, 2);
BgeIsStart = false;
BgeEmbeddingIsStart = false;
}
});
}
private async Task BgeRerank()
{
if (string.IsNullOrEmpty(_aiModel.ModelName))
{
_ = Message.Error("请输入模型名称!", 2);
return;
}
if (string.IsNullOrEmpty(_aiModel.EndPoint))
{
_ = Message.Error("请输入正确的Python dll或python so路径", 2);
return;
}
BgeRerankIsStart = true;
BgeRerankBtnText = "正在初始化...";
await Task.Run(() =>
{
try
{
RerankConfig.LoadModel(_aiModel.EndPoint, _aiModel.ModelName);
BgeRerankBtnText = "初始化完成";
BgeRerankIsStart = false;
}
catch (System.Exception ex)
{
_ = Message.Error(ex.Message, 2);
BgeRerankIsStart = false;
}
});
}
private async Task CmdLogHandler(string message)
{
await InvokeAsync(() =>
@@ -331,6 +369,9 @@ namespace AntSK.Pages.Setting.AIModel
case AIType.BgeEmbedding:
_aiModel.AIModelType = AIModelType.Embedding;
break;
case AIType.BgeRerank:
_aiModel.AIModelType = AIModelType.Rerank;
break;
default:
_aiModel.AIModelType = AIModelType.Chat;
break;