Skip to content

Commit

Permalink
增加模型管理界面
Browse files Browse the repository at this point in the history
增加模型管理设计
增加模型日志记录
优化代码
  • Loading branch information
239573049 committed Mar 20, 2024
1 parent 4eded53 commit c787767
Show file tree
Hide file tree
Showing 33 changed files with 6,013 additions and 49 deletions.
21 changes: 21 additions & 0 deletions FastWiki.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastru
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastWiki.Infrastructure.Common", "src\Infrastructure\FastWiki.Infrastructure.Common\FastWiki.Infrastructure.Common.csproj", "{0C90BE14-96EC-4E1B-A017-258E36ED6B1C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AIDotNet.Abstractions", "..\AIDotNet.API\src\AIDotNet.Abstractions\AIDotNet.Abstractions.csproj", "{F5FC30A0-5CE7-4D27-BFDA-3E7581D2FC70}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AIDotNet.OpenAI", "..\AIDotNet.API\src\extensions\AIDotNet.OpenAI\AIDotNet.OpenAI.csproj", "{B420B7E3-38AB-445C-8789-E2E0736F46C7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AIDotNet.SparkDesk", "..\AIDotNet.API\src\extensions\AIDotNet.SparkDesk\AIDotNet.SparkDesk.csproj", "{A45EDC61-1937-4894-B165-CAEFF71230D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -45,6 +51,18 @@ Global
{0C90BE14-96EC-4E1B-A017-258E36ED6B1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C90BE14-96EC-4E1B-A017-258E36ED6B1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C90BE14-96EC-4E1B-A017-258E36ED6B1C}.Release|Any CPU.Build.0 = Release|Any CPU
{F5FC30A0-5CE7-4D27-BFDA-3E7581D2FC70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5FC30A0-5CE7-4D27-BFDA-3E7581D2FC70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5FC30A0-5CE7-4D27-BFDA-3E7581D2FC70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5FC30A0-5CE7-4D27-BFDA-3E7581D2FC70}.Release|Any CPU.Build.0 = Release|Any CPU
{B420B7E3-38AB-445C-8789-E2E0736F46C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B420B7E3-38AB-445C-8789-E2E0736F46C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B420B7E3-38AB-445C-8789-E2E0736F46C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B420B7E3-38AB-445C-8789-E2E0736F46C7}.Release|Any CPU.Build.0 = Release|Any CPU
{A45EDC61-1937-4894-B165-CAEFF71230D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A45EDC61-1937-4894-B165-CAEFF71230D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A45EDC61-1937-4894-B165-CAEFF71230D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A45EDC61-1937-4894-B165-CAEFF71230D2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -56,6 +74,9 @@ Global
{EF8AE318-7534-4687-8663-BB6B5F5DE5F2} = {789CE025-90BF-4A37-B13A-9E6CFE9F7F4C}
{D6D3A051-B8FE-4356-B582-921A6726ED40} = {2A86D1DF-3A54-483F-9F59-0F2F82ED7834}
{0C90BE14-96EC-4E1B-A017-258E36ED6B1C} = {D6D3A051-B8FE-4356-B582-921A6726ED40}
{F5FC30A0-5CE7-4D27-BFDA-3E7581D2FC70} = {D6D3A051-B8FE-4356-B582-921A6726ED40}
{B420B7E3-38AB-445C-8789-E2E0736F46C7} = {D6D3A051-B8FE-4356-B582-921A6726ED40}
{A45EDC61-1937-4894-B165-CAEFF71230D2} = {D6D3A051-B8FE-4356-B582-921A6726ED40}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C6848D28-988E-4785-AF4D-2554B6E6F9E4}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class UpdateChatApplicationInput

public string Name { get; set; }

public string ChatType { get; set; }

/// <summary>
/// 提示词
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace FastWiki.Service.Contracts.Model;

public class CreateFastModeInput
{
public string Name { get; set; }

/// <summary>
/// 模型类型
/// </summary>
public string Type { get; set; }

/// <summary>
/// 模型代理地址
/// </summary>
public string Url { get; set; }

/// <summary>
/// 模型密钥
/// </summary>
public string ApiKey { get; set; }

/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }

/// <summary>
/// AI支持的模型
/// </summary>
public List<string> Models { get; set; } = [];

/// <summary>
/// 优先级
/// </summary>
public int Order { get; set; }
}
53 changes: 53 additions & 0 deletions src/Contracts/FastWiki.Service.Contracts/Model/FastModelDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace FastWiki.Service.Contracts.Model;

public class FastModelDto
{
public string Id { get; set; }

public string Name { get; set; }

/// <summary>
/// 模型类型
/// </summary>
public string Type { get; set; }

/// <summary>
/// 模型代理地址
/// </summary>
public string Url { get; set; }

/// <summary>
/// 模型密钥
/// </summary>
public string ApiKey { get; set; }

/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }

/// <summary>
/// AI支持的模型
/// </summary>
public List<string> Models { get; set; } = [];

/// <summary>
/// 优先级
/// </summary>
public int Order { get; set; }

/// <summary>
/// 测试时间
/// </summary>
public long TestTime { get; set; }

/// <summary>
/// 已消耗配额
/// </summary>
public long UsedQuota { get; set; }

/// <summary>
/// 启用
/// </summary>
public bool Enable { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FastWiki.Service.Contracts.Model;

namespace FastWiki.Service.Application.Model.Commands;

/// <summary>
/// ´´½¨Ä£ÐÍ
/// </summary>
/// <param name="Input"></param>
public record CreateFastModeCommand(CreateFastModeInput Input) : Command;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FastWiki.Service.Application.Model.Commands;

/// <summary>
/// ɾ³ýÖ¸¶¨Ä£ÐÍ
/// </summary>
/// <param name="Id"></param>
public record RemoveFastModelCommand(string Id): Command;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using FastWiki.Service.Application.Model.Commands;
using FastWiki.Service.Domain.Model.Aggregates;
using FastWiki.Service.Domain.Model.Repositories;

namespace FastWiki.Service.Application.Model;

public sealed class ModelCommandHandler(IFastModelRepository fastModelRepository)
{
[EventHandler]
public async Task CreateFastModeAsync(CreateFastModeCommand command)
{
if (await fastModelRepository.ExistAsync(command.Input.Name))
{
throw new UserFriendlyException("Ä£ÐÍÃû³ÆÒÑ´æÔÚ");
}

var model = new FastModel(command.Input.Name, command.Input.Type, command.Input.Url, command.Input.ApiKey,
command.Input.Description, command.Input.Models, command.Input.Order);

await fastModelRepository.AddAsync(model);
}

[EventHandler]
public async Task RemoveFastModelAsync(RemoveFastModelCommand command)
{
await fastModelRepository.RemoveAsync(command.Id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using FastWiki.Service.Application.Model.Queries;
using FastWiki.Service.Contracts.Model;
using FastWiki.Service.Domain.Model.Repositories;

namespace FastWiki.Service.Application.Model;

public sealed class ModelQueryHandler(IFastModelRepository fastModelRepository)
{
[EventHandler]
public async Task GetModelListAsync(GetModelListQuery query)
{
var models = await fastModelRepository.GetModelListAsync(query.Keyword, query.Page, query.PageSize);

var count = await fastModelRepository.GetModelCountAsync(query.Keyword);

query.Result = new PaginatedListBase<FastModelDto>
{
Result = models.Select(x => new FastModelDto
{
Id = x.Id,
Name = x.Name,
Type = x.Type,
Description = x.Description
}).ToList(),
Total = count
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using FastWiki.Service.Contracts.Model;

namespace FastWiki.Service.Application.Model.Queries;

public record GetModelListQuery(string Keyword, int Page, int PageSize) : Query<PaginatedListBase<FastModelDto>>
{
public override PaginatedListBase<FastModelDto> Result { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FastWiki.Service.Domain.Model.Aggregates;
using FastWiki.Service.Domain.Model.Repositories;

namespace FastWiki.Service.DataAccess.Repositories.Model;

public sealed class FastModelRepository : Repository<WikiDbContext, FastModel, string>, IFastModelRepository
{
public FastModelRepository(WikiDbContext context, IUnitOfWork unitOfWork) : base(context, unitOfWork)
{
}


public async Task<List<FastModel>> GetModelListAsync(string keyword, int page, int pageSize)
{
var query = CreateModelQuery(keyword);
return await query.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
}

public async Task<long> GetModelCountAsync(string keyword)
{
var query = CreateModelQuery(keyword);
return await query.LongCountAsync();
}

public Task<bool> ExistAsync(string name)
{
return Context.FastModels.AsNoTracking().AnyAsync(x => x.Name == name);
}

public async Task<bool> RemoveAsync(string id)
{
var result = await Context.FastModels.Where(x => x.Id == id).ExecuteDeleteAsync();

return result > 0;
}

private IQueryable<FastModel> CreateModelQuery(string keyword)
{
var query = Context.FastModels.AsQueryable();
if (!string.IsNullOrEmpty(keyword))
{
query = query.Where(x =>
x.Name.Contains(keyword) || x.Type.Contains(keyword) || x.Description.Contains(keyword));
}

return query.AsNoTracking();
}
}
39 changes: 39 additions & 0 deletions src/Service/FastWiki.Service/DataAccess/WikiDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FastWiki.Service.Domain.Storage.Aggregates;
using System.Text.Json;
using FastWiki.Service.Domain.Model.Aggregates;

namespace FastWiki.Service.DataAccess;

Expand All @@ -21,6 +22,10 @@ public class WikiDbContext(MasaDbContextOptions<WikiDbContext> options) : MasaDb

public DbSet<ChatShare> ChatShares { get; set; }

public DbSet<FastModel> FastModels { get; set; }

public DbSet<ModelLogger> ModelLoggers { get; set; }

protected override void OnModelCreatingExecuting(ModelBuilder modelBuilder)
{
base.OnModelCreatingExecuting(modelBuilder);
Expand Down Expand Up @@ -126,6 +131,40 @@ private static void ConfigEntities(ModelBuilder modelBuilder)
entity.HasIndex(x => x.ChatApplicationId);
});

modelBuilder.Entity<FastModel>(entity =>
{
entity.ToTable("wiki-fast-models");
entity.HasKey(e => e.Id);

entity.HasIndex(x => x.Name);
entity.HasIndex(x => x.Type);

entity.Property(e => e.Name).HasMaxLength(30);
entity.Property(e => e.Type).HasMaxLength(100);
entity.Property(e => e.Url).HasMaxLength(200);
entity.Property(e => e.ApiKey).HasMaxLength(100);
entity.Property(e => e.Description).HasMaxLength(200);
entity.Property(e => e.Models).HasMaxLength(-1);

entity.Property(x => x.Models)
.HasConversion(item => JsonSerializer.Serialize(item, new JsonSerializerOptions()),
item => JsonSerializer.Deserialize<List<string>>(item, new JsonSerializerOptions()));
});

modelBuilder.Entity<ModelLogger>(entity =>
{
entity.ToTable("wiki-model-logger");
entity.HasKey(e => e.Id);
entity.Property(e => e.Id).ValueGeneratedOnAdd();

entity.HasIndex(x => x.FastModelId);
entity.HasIndex(x => x.UserId);
entity.HasIndex(x => x.ApplicationId);
entity.HasIndex(x => x.ApiKey);
entity.HasIndex(x => x.Type);
entity.HasIndex(x => x.CreationTime);
});

var user = new User("admin", "admin", "Aa123456",
"https://blog-simple.oss-cn-shenzhen.aliyuncs.com/Avatar.jpg", "[email protected]", "13049809673", false,
RoleType.Admin);
Expand Down
Loading

0 comments on commit c787767

Please sign in to comment.