From 2268c115e3f6cd01a07a7d54961056da780c9bf0 Mon Sep 17 00:00:00 2001 From: zpc Date: Sat, 13 Jul 2024 23:00:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=92=E8=89=B2=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E7=B1=BB=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs | 22 +++++- .../HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs | 55 ++++++++++++- .../Cache/MiaoYuDataEntityCache.cs | 2 +- .../Cache/Special/CharacterEntityCache.cs | 58 ++++++++++++++ .../Character/CharacterBLL.cs | 35 +++++++++ .../DbSqlServer/Db_MiaoYu/MiaoYuContext.cs | 77 +++++++++++++++++-- .../DbSqlServer/Db_MiaoYu/T_Character.cs | 9 +-- .../DbSqlServer/Db_MiaoYu/T_Model_Config.cs | 49 ++++++++++++ .../DbSqlServer/Db_MiaoYu/T_User_Char.cs | 49 ++++++++++++ .../Dto/Character/CharacterCache.cs | 29 +++++++ .../HuanMeng.MiaoYu.Model.csproj | 1 + 11 files changed, 368 insertions(+), 18 deletions(-) create mode 100644 src/0-core/HuanMeng.MiaoYu.Code/Cache/Special/CharacterEntityCache.cs create mode 100644 src/0-core/HuanMeng.MiaoYu.Code/Character/CharacterBLL.cs create mode 100644 src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Model_Config.cs create mode 100644 src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_User_Char.cs create mode 100644 src/0-core/HuanMeng.MiaoYu.Model/Dto/Character/CharacterCache.cs diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs b/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs index 674a6f4..74af056 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs @@ -14,6 +14,7 @@ using HuanMeng.MiaoYu.Model.Dto; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; using System; @@ -30,6 +31,7 @@ namespace HuanMeng.MiaoYu.Code.Base /// public class MiaoYuBase : BLLBase { + public MiaoYuBase(IServiceProvider serviceProvider) : base(serviceProvider) { @@ -230,12 +232,28 @@ namespace HuanMeng.MiaoYu.Code.Base if (_miaoYuCache == null) { //new MiaoYuDataEntityCache(Dao).DataList - _miaoYuCache = new MiaoYuCache(Dao); - + _miaoYuCache = new MiaoYuCache(Dao, Mapper); + } return _miaoYuCache; } } #endregion + + #region 日志 + private Logger? _logger; + public Logger _Logger + { + + get + { + if (_logger == null) + { + _logger = _serviceProvider.GetRequiredService>(); + } + return _logger; + } + } + #endregion } } diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs index 650a431..27128cc 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuCache.cs @@ -1,12 +1,17 @@ +using AutoMapper; + +using HuanMeng.MiaoYu.Code.Cache.Special; +using HuanMeng.MiaoYu.Model.Dto.Character; + namespace HuanMeng.MiaoYu.Code.Cache { /// /// 缓存类 /// /// - public partial class MiaoYuCache(DAO dao) + public partial class MiaoYuCache(DAO dao, IMapper mapper) { #region 角色缓存表 /// @@ -34,5 +39,53 @@ namespace HuanMeng.MiaoYu.Code.Cache } #endregion + + #region 模型缓存表 + /// + /// 模型缓存表 + /// + private static object ModelConfigLock = new object(); + + /// + /// 模型缓存表 + /// + public MiaoYuDataEntityCache? ModelConfig { get; set; } + /// + /// 模型 + /// + public List ModelConfigList + { + get + { + if (ModelConfig == null) + { + ModelConfig = new MiaoYuDataEntityCache(dao, ModelConfigLock); + } + return ModelConfig.DataList ?? new List(); + } + } + + #endregion + + #region 角色信息 + /// + /// 角色缓存表 + /// + public CharacterEntityCache? CharacterCache { get; set; } + /// + /// 角色 + /// + public List CharacterList + { + get + { + if (CharacterCache == null) + { + CharacterCache = new CharacterEntityCache(dao, mapper); + } + return CharacterCache.DataList ?? new List(); + } + } + #endregion } } diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs index 95d2cbd..548aa49 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/MiaoYuDataEntityCache.cs @@ -3,7 +3,7 @@ namespace HuanMeng.MiaoYu.Code.Cache { /// - /// 妙语数据库实体类缓存 + /// 妙语数据库实体类缓存 /// public class MiaoYuDataEntityCache(DAO _dao, object lockObj, int cacheTime = 36000) : CommonDataEntityCache(lockObj, cacheTime) where T : class diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Cache/Special/CharacterEntityCache.cs b/src/0-core/HuanMeng.MiaoYu.Code/Cache/Special/CharacterEntityCache.cs new file mode 100644 index 0000000..23f2ae2 --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Code/Cache/Special/CharacterEntityCache.cs @@ -0,0 +1,58 @@ +using AutoMapper; + +using HuanMeng.MiaoYu.Code.DataAccess; +using HuanMeng.MiaoYu.Model.Dto.Character; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HuanMeng.MiaoYu.Code.Cache.Special +{ + /// + /// + /// + /// + public class CharacterEntityCache(DAO _dao, IMapper mapper) : MiaoYuDataEntityCache(_dao, CharacterCacheLock) + { + /// + /// 锁 + /// + private static object CharacterCacheLock = new object(); + + /// + /// 获取缓存数据 + /// + /// + public override List GetDataList() + { + List characterCaches = null; + //读取角色表 + var characters = _dao.daoDbMiaoYu.context.T_Character.ToList(); + if (characters == null) + { + return new List(); + } + characterCaches = mapper.Map>(characters); + //查询配置表 + var modelConfigs = _dao.daoDbMiaoYu.context.T_Model_Config.ToList(); + foreach (var characterCache in characterCaches) + { + var modelConfig = modelConfigs.FirstOrDefault(it => it.Id == characterCache.ModelConfigId); + if (modelConfig != null) + { + characterCache.ModelConfig = modelConfig; + } + } + //移除没有配置模型的类 + characterCaches.Where(it => it.ModelConfig == null).ToList().ForEach(characterCache => + { + characterCaches.Remove(characterCache); + }); + return characterCaches; + } + } + +} diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Character/CharacterBLL.cs b/src/0-core/HuanMeng.MiaoYu.Code/Character/CharacterBLL.cs new file mode 100644 index 0000000..18ab0e3 --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Code/Character/CharacterBLL.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HuanMeng.MiaoYu.Code.Character +{ + /// + /// 角色信息类 + /// + public class CharacterBLL : MiaoYuBase + { + public CharacterBLL(IServiceProvider serviceProvider) : base(serviceProvider) + { + } + + /// + /// + /// + /// + public Task GetCharacter(int index) + { + + var charactersList = MiaoYuCache.CharactersList.Where(it => it.Visibility).ToList(); + if (charactersList.Count == 0) + { + return null; + } + + return null; + } + + } +} diff --git a/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/MiaoYuContext.cs b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/MiaoYuContext.cs index f26e777..25c6fac 100644 --- a/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/MiaoYuContext.cs +++ b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/MiaoYuContext.cs @@ -68,11 +68,21 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext /// public virtual DbSet T_Chat { get; set; } + /// + /// + /// + public virtual DbSet T_Model_Config { get; set; } + /// /// 用户表 /// public virtual DbSet T_User { get; set; } + /// + /// + /// + public virtual DbSet T_User_Char { get; set; } + /// /// 用户信息表 /// @@ -109,20 +119,15 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext .HasComment("创建时间") .HasColumnType("datetime"); entity.Property(e => e.Gender).HasComment("性别0男1女2其他"); - entity.Property(e => e.Model) - .HasMaxLength(255) - .IsUnicode(false) - .HasComment("人物模型"); + entity.Property(e => e.ModelConfigId).HasComment("模型Id"); entity.Property(e => e.Name) .HasMaxLength(50) .HasComment("人物名字"); entity.Property(e => e.Prologue) .HasMaxLength(255) .HasComment("开场白"); - entity.Property(e => e.Style).HasComment("风格id"); entity.Property(e => e.System) - .HasMaxLength(255) - .IsUnicode(false) + .HasMaxLength(1000) .HasComment("人物初始设定"); entity.Property(e => e.TenantId).HasComment("租户Id"); entity.Property(e => e.UpdateTime) @@ -280,6 +285,40 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext } }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.Id).HasName("PK__T_Model___3214EC074121E040"); + + entity.Property(e => e.AnthropicVersion) + .HasMaxLength(255) + .HasComment("模型版本 anthropic-version"); + entity.Property(e => e.ApiKey) + .HasMaxLength(255) + .HasComment("模型key x-api-key"); + entity.Property(e => e.CreateTime) + .HasComment("创建时间") + .HasColumnType("datetime"); + entity.Property(e => e.MaxTokens).HasComment("模型运行最大的max_tokens"); + entity.Property(e => e.Model) + .HasMaxLength(50) + .HasComment("模型model"); + entity.Property(e => e.ModelName) + .HasMaxLength(50) + .HasComment("模型名称"); + entity.Property(e => e.TenantId).HasComment("租户"); + entity.Property(e => e.UpdateTime) + .HasComment("修改时间") + .HasColumnType("datetime"); + entity.Property(e => e.Url) + .HasMaxLength(255) + .HasComment("模型请求地址"); + //添加全局筛选器 + if (this.TenantInfo != null) + { + entity.HasQueryFilter(it => it.TenantId == this.TenantInfo.TenantId); + } + }); + modelBuilder.Entity(entity => { entity.HasKey(e => e.Id).HasName("PK__T_User__3214EC073733108B"); @@ -323,6 +362,30 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext } }); + modelBuilder.Entity(entity => + { + entity.Property(e => e.CharacterId).HasComment("角色Id"); + entity.Property(e => e.CreateDateTime) + .HasComment("创建时间") + .HasColumnType("datetime"); + entity.Property(e => e.IsEnable).HasComment("是否启用"); + entity.Property(e => e.ModelConfigId).HasComment("使用模型Id"); + entity.Property(e => e.SessionId).HasComment("会话Id"); + entity.Property(e => e.SessionName) + .HasMaxLength(50) + .HasComment("会话名称"); + entity.Property(e => e.TenantId).HasComment("租户"); + entity.Property(e => e.UpdateDateTime) + .HasComment("修改时间") + .HasColumnType("datetime"); + entity.Property(e => e.UserId).HasComment("用户Id"); + //添加全局筛选器 + if (this.TenantInfo != null) + { + entity.HasQueryFilter(it => it.TenantId == this.TenantInfo.TenantId); + } + }); + modelBuilder.Entity(entity => { entity.HasKey(e => new { e.Id, e.UserId }).HasName("PK__T_User_D__E36C60C3D959FD89"); diff --git a/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Character.cs b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Character.cs index 46dceb5..cac01e1 100644 --- a/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Character.cs +++ b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Character.cs @@ -29,9 +29,9 @@ public partial class T_Character: MultiTenantEntity public string Prologue { get; set; } = null!; /// - /// 风格id + /// 模型Id /// - public int? Style { get; set; } + public int ModelConfigId { get; set; } /// /// 公开/私密 0公开 1私密 @@ -53,11 +53,6 @@ public partial class T_Character: MultiTenantEntity /// public int Gender { get; set; } - /// - /// 人物模型 - /// - public string? Model { get; set; } - /// /// 人物初始设定 /// diff --git a/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Model_Config.cs b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Model_Config.cs new file mode 100644 index 0000000..60e8e3b --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_Model_Config.cs @@ -0,0 +1,49 @@ +using System; + +namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu; + +public partial class T_Model_Config: MultiTenantEntity +{ + public int Id { get; set; } + + /// + /// 模型名称 + /// + public string ModelName { get; set; } = null!; + + /// + /// 模型model + /// + public string Model { get; set; } = null!; + + /// + /// 模型运行最大的max_tokens + /// + public int MaxTokens { get; set; } + + /// + /// 模型key x-api-key + /// + public string ApiKey { get; set; } = null!; + + /// + /// 模型请求地址 + /// + public string Url { get; set; } = null!; + + /// + /// 模型版本 anthropic-version + /// + public string AnthropicVersion { get; set; } = null!; + + /// + /// 创建时间 + /// + public DateTime CreateTime { get; set; } + + /// + /// 修改时间 + /// + public DateTime UpdateTime { get; set; } + +} diff --git a/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_User_Char.cs b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_User_Char.cs new file mode 100644 index 0000000..834bd89 --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Model/DbSqlServer/Db_MiaoYu/T_User_Char.cs @@ -0,0 +1,49 @@ +using System; + +namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu; + +public partial class T_User_Char: MultiTenantEntity +{ + public int Id { get; set; } + + /// + /// 会话Id + /// + public Guid SessionId { get; set; } + + /// + /// 会话名称 + /// + public string SessionName { get; set; } = null!; + + /// + /// 角色Id + /// + public int CharacterId { get; set; } + + /// + /// 使用模型Id + /// + public int ModelConfigId { get; set; } + + /// + /// 创建时间 + /// + public DateTime CreateDateTime { get; set; } + + /// + /// 修改时间 + /// + public DateTime UpdateDateTime { get; set; } + + /// + /// 是否启用 + /// + public bool IsEnable { get; set; } + + + /// + /// 用户Id + /// + public int UserId { get; set; } +} diff --git a/src/0-core/HuanMeng.MiaoYu.Model/Dto/Character/CharacterCache.cs b/src/0-core/HuanMeng.MiaoYu.Model/Dto/Character/CharacterCache.cs new file mode 100644 index 0000000..323a08c --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Model/Dto/Character/CharacterCache.cs @@ -0,0 +1,29 @@ +using AutoMapper; + +using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HuanMeng.MiaoYu.Model.Dto.Character +{ + /// + /// 模型 + /// + [AutoMap(typeof(T_Character))] + public class CharacterCache : T_Character + { + /// + /// 模型配置 + /// + public T_Model_Config ModelConfig { get; set; } + + /// + /// 多少人聊天过 + /// + public int UseChatCount { get; set; } + } +} diff --git a/src/0-core/HuanMeng.MiaoYu.Model/HuanMeng.MiaoYu.Model.csproj b/src/0-core/HuanMeng.MiaoYu.Model/HuanMeng.MiaoYu.Model.csproj index db3d3b9..8e75674 100644 --- a/src/0-core/HuanMeng.MiaoYu.Model/HuanMeng.MiaoYu.Model.csproj +++ b/src/0-core/HuanMeng.MiaoYu.Model/HuanMeng.MiaoYu.Model.csproj @@ -8,6 +8,7 @@ + all