forked from AIDotNet/fast-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加模型管理设计 增加模型日志记录 优化代码
- Loading branch information
Showing
33 changed files
with
6,013 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Contracts/FastWiki.Service.Contracts/Model/CreateFastModeInput.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
src/Contracts/FastWiki.Service.Contracts/Model/FastModelDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Service/FastWiki.Service/Application/Model/Commands/CreateFastModeCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
7 changes: 7 additions & 0 deletions
7
src/Service/FastWiki.Service/Application/Model/Commands/RemoveFastModelCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
28 changes: 28 additions & 0 deletions
28
src/Service/FastWiki.Service/Application/Model/ModelCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Service/FastWiki.Service/Application/Model/ModelQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Service/FastWiki.Service/Application/Model/Queries/GetModelListQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Service/FastWiki.Service/DataAccess/Repositories/Model/FastModelRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
@@ -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); | ||
|
@@ -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); | ||
|
Oops, something went wrong.