support model download and file list

This commit is contained in:
James Yeung
2024-03-12 18:08:23 +08:00
parent af09ae7c3e
commit ea9044719a
6 changed files with 118 additions and 11 deletions

1
.gitignore vendored
View File

@@ -340,3 +340,4 @@ ASALocalRun/
/src/AntSK/AntSK.db
/src/AntSK/appsettings.Development.json
/src/AntSK.db
/src/AntSK/llama_models

View File

@@ -6,5 +6,7 @@
public static string Chat { get; set; }
public static string Embedding { get; set; }
public static string FileDirectory { get; set; }
}
}

View File

@@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="Downloader" Version="3.0.6" />
</ItemGroup>
<ItemGroup>

View File

@@ -25,10 +25,10 @@
<FormItem Label="模型类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<RadioGroup @bind-Value="context.AIModelType">
<Radio RadioButton Value="@(AIModelType.Chat)">会话模型</Radio>
<Radio RadioButton Value="@(AIModelType.Embedding)">向量模型</Radio>
</RadioGroup>
</FormItem>
@if (context.AIModelType == AIModelType.Embedding)
<Radio RadioButton Value="@(AIModelType.Embedding)">向量模型</Radio>
</RadioGroup>
</FormItem>
@if (context.AIModelType == AIModelType.Embedding)
{
<FormItem Label="注意事项" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<b>请不要使用不同维度的向量模型,否则会导致无法向量存储</b>
@@ -74,13 +74,15 @@
@if (context.AIType == AIType.LLamaSharp)
{
<FormItem Label="模型路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Input Placeholder="请输入模型路径" @bind-Value="@context.ModelName" />
<InputGroup>
<AutoComplete Options="_modelFiles" Placeholder="请输入模型路径" @bind-Value="@context.ModelName" />
<Button OnClick="()=>_downloadModalVisible=true">从Haggingface下载</Button>
</InputGroup>
</FormItem>
}
@if (context.AIType == AIType.Mock)
{
}
<FormItem Label=" " Style="margin-top:32px" WrapperCol="LayoutModel._submitFormLayout.WrapperCol">
<Button Type="primary" OnClick="HandleSubmit">
@@ -95,6 +97,27 @@
</ChildContent>
</PageContainer>
@code {
<Modal @ref="_modal" Visible="_downloadModalVisible" Footer="null" Closable Title="模型下载" OnCancel="OnCancel" DestroyOnClose>
<Flex Gap="10" Vertical>
}
<Alert>
<p>支持LLamaSharp的本地模型 支持gguf类型推荐使用llama或者qwen</p>
<p>如果模型加载报内存错误可能是和llama.cpp版本不一致</p>
<a href="https://hf-mirror.com/models?search=gguf" target="_blank" rel="noopener noreferrer">打开下载地址</a>
</Alert>
<InputGroup>
<Input Disabled="_downloadStarted" Placeholder="请输入下载地址" @bind-Value="_downloadUrl" Style="width:80%"></Input>
@if (!_downloadStarted)
{
<Button OnClick="StartDownload">开始</Button>
}
else
{
<Button OnClick="Stop">停止</Button>
}
</InputGroup>
<AntDesign.Progress Percent="_downloadProgress"></AntDesign.Progress>
</Flex>
</Modal>

View File

@@ -1,8 +1,11 @@
using AntDesign;
using AntDesign.ProLayout;
using AntSK.Domain.Options;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using Downloader;
using Microsoft.AspNetCore.Components;
using System.ComponentModel;
namespace AntSK.Pages.Setting.AIModel
{
@@ -16,6 +19,17 @@ namespace AntSK.Pages.Setting.AIModel
private AIModels _aiModel = new AIModels();
private string _downloadUrl;
private bool _downloadModalVisible;
private double _downloadProgress;
private bool _downloadFinished;
private bool _downloadStarted;
IDownload _download;
private Modal _modal;
string[] _modelFiles;
IEnumerable<string> _menuKeys;
private List<MenuDataItem> menuList = new List<MenuDataItem>();
@@ -26,6 +40,8 @@ namespace AntSK.Pages.Setting.AIModel
{
_aiModel = _aimodels_Repositories.GetFirst(p => p.Id == ModelId);
}
_modelFiles = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), LLamaSharpOption.FileDirectory));
}
private void HandleSubmit()
@@ -69,5 +85,68 @@ namespace AntSK.Pages.Setting.AIModel
{
NavigationManager.NavigateTo("/setting/modellist");
}
private async Task StartDownload()
{
if (string.IsNullOrWhiteSpace(_downloadUrl))
{
return;
}
_download = DownloadBuilder.New()
.WithUrl(_downloadUrl)
.WithDirectory(Path.Combine(Directory.GetCurrentDirectory(), LLamaSharpOption.FileDirectory))
.WithConfiguration(new DownloadConfiguration()
{
ParallelCount = 5,
})
.Build();
_download.DownloadProgressChanged += DownloadProgressChanged;
_download.DownloadFileCompleted += DownloadFileCompleted;
_download.DownloadStarted += DownloadStarted;
await _download.StartAsync();
//download.Stop(); // cancel current download
}
private void DownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e)
{
_downloadProgress = e.ProgressPercentage;
InvokeAsync(StateHasChanged);
}
private void DownloadFileCompleted(object? sender, AsyncCompletedEventArgs e)
{
_downloadFinished = true;
_aiModel.ModelName = _download.Package.FileName;
_downloadModalVisible = false;
_downloadStarted = false;
u _modelFiles = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), LLamaSharpOption.FileDirectory));
InvokeAsync(StateHasChanged);
}
private void DownloadStarted(object? sender, DownloadStartedEventArgs e)
{
_downloadStarted = true;
InvokeAsync(StateHasChanged);
}
private void OnCancel()
{
if (_downloadStarted)
{
return;
}
_downloadModalVisible = false;
}
private void Stop()
{
_downloadStarted=false;
_download?.Stop();
}
}
}

View File

@@ -33,9 +33,10 @@
"TableNamePrefix": "km-"
},
"LLamaSharp": {
"RunType": "GPU",
"RunType": "GPU",
"Chat": "D:\\Code\\AI\\AntBlazor\\model\\qwen1_5-1_8b-chat-q8_0.gguf",
"Embedding": "D:\\Code\\AI\\AntBlazor\\model\\qwen1_5-1_8b-chat-q8_0.gguf"
"Embedding": "D:\\Code\\AI\\AntBlazor\\model\\qwen1_5-1_8b-chat-q8_0.gguf",
"FileDirectory": "./llama_models"
},
"Login": {
"User": "admin",