添加聊天记录
This commit is contained in:
parent
17e27c0cbc
commit
fc90edb4a7
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.DotNetCore.Json
|
||||
{
|
||||
public class CustomDateTimeConverter : JsonConverter<DateTime>
|
||||
{
|
||||
private readonly string _format;
|
||||
|
||||
public CustomDateTimeConverter(string format)
|
||||
{
|
||||
_format = format;
|
||||
}
|
||||
|
||||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return DateTime.Parse(reader.GetString());
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.ToString(_format));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,7 +40,7 @@ namespace HuanMeng.MiaoYu.Code.Cache.Special
|
|||
return characterCaches;
|
||||
}
|
||||
characterCaches = mapper.Map<List<CharacterCache>>(characters);
|
||||
string sqlString = $"select TOP 100 CharacterId,count(DISTINCT UserId ) UserCount from T_User_Char where isdelete=0 and TenantId='{_dao.daoDbMiaoYu.context.TenantInfo?.TenantId}' GROUP BY CharacterId";
|
||||
string sqlString = $"select TOP 100 CharacterId,count(DISTINCT UserId ) UserCount from T_User_Chat where isdelete=0 and TenantId='{_dao.daoDbMiaoYu.context.TenantInfo?.TenantId}' GROUP BY CharacterId";
|
||||
//获取查看次数
|
||||
var characteChatCounts = _dao.daoDbMiaoYu.SqlQueryList<CharacteChatCountModel>(sqlString);
|
||||
//查询配置表
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@ using HuanMeng.DotNetCore.Base;
|
|||
using HuanMeng.MiaoYu.Code.Cache;
|
||||
using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
||||
using HuanMeng.MiaoYu.Model.Dto.Chat;
|
||||
using HuanMeng.MiaoYu.Model.EnumModel.Chat;
|
||||
using HuanMeng.Utility;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
|
@ -23,26 +26,104 @@ namespace HuanMeng.MiaoYu.Code.Chat
|
|||
{
|
||||
}
|
||||
|
||||
public async Task<BaseResponse<List<ChatMessageDto>>> GetChatMessage(int characterId, long timeStamp)
|
||||
/// <summary>
|
||||
/// 获取用户和角色的聊天记录
|
||||
/// </summary>
|
||||
/// <param name="characterId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public async Task<BaseResponse<List<ChatMessageDto>>> GetChatMessage(int characterId)
|
||||
{
|
||||
|
||||
var charact = MiaoYuCache.CharacterList.FirstOrDefault(it => it.Id == characterId);
|
||||
if (charact == null)
|
||||
{
|
||||
throw new ArgumentException("角色不存在");
|
||||
}
|
||||
if (_UserId == 0)
|
||||
{
|
||||
return new BaseResponse<List<ChatMessageDto>>(ResonseCode.Success, "", new List<ChatMessageDto>());
|
||||
|
||||
|
||||
var chat = new ChatMessageDto()
|
||||
{
|
||||
Id=0,
|
||||
Role = ChatRole.assistant.ToString(),
|
||||
ClaudeType = "text",
|
||||
Content = charact.Prologue,
|
||||
Timestamp = DateTime.Now,
|
||||
UserIcon = charact.IconImage
|
||||
};
|
||||
var list = new List<ChatMessageDto>();
|
||||
list.Add(chat);
|
||||
return new BaseResponse<List<ChatMessageDto>>(ResonseCode.Success, "", list);
|
||||
}
|
||||
var userChatSession = Dao.daoDbMiaoYu.context.T_User_Chat.Where(it => it.CharacterId == characterId && it.UserId == _UserId && !it.IsDelete).FirstOrDefault();
|
||||
//if(timeStamp.)
|
||||
var userChatSession = await Dao.daoDbMiaoYu.context.T_User_Chat.Where(it => it.CharacterId == characterId && it.UserId == _UserId && !it.IsDelete).FirstOrDefaultAsync();
|
||||
if (userChatSession == null)
|
||||
{
|
||||
userChatSession = new T_User_Chat()
|
||||
{
|
||||
SessionId=Guid.NewGuid(),
|
||||
CharacterId=characterId,
|
||||
CreateAt=DateTime.Now,
|
||||
IsDelete=false,
|
||||
SessionName="现在",
|
||||
SessionId = Guid.NewGuid(),
|
||||
CharacterId = characterId,
|
||||
CreateAt = DateTime.Now,
|
||||
IsDelete = false,
|
||||
SessionName = "新会话",
|
||||
UpdateAt = DateTime.Now,
|
||||
ModelConfigId = charact.ModelConfigId,
|
||||
TenantId = charact.TenantId,
|
||||
UserId = _UserId,
|
||||
};
|
||||
Dao.daoDbMiaoYu.context.T_User_Chat.Add(userChatSession);
|
||||
Dao.daoDbMiaoYu.context.SaveChanges();
|
||||
}
|
||||
//Dao.daoDbMiaoYu.context.T_Chat.Where(it=>it.CharacterId)
|
||||
return null;
|
||||
var chatList = await Dao.daoDbMiaoYu.context.T_Chat.Where(it => it.SessionId == userChatSession.SessionId && it.UserId == _UserId && it.Type == (int)ChatType.正常).OrderByDescending(it => it.SendMessageDay).ToListAsync();
|
||||
if (chatList == null || chatList.Count == 0)
|
||||
{
|
||||
//添加第一句
|
||||
var chat = new T_Chat()
|
||||
{
|
||||
Role = ChatRole.initialization.ToString(),
|
||||
CharacterId = characterId,
|
||||
ClaudeId = "",
|
||||
ClaudeType = "text",
|
||||
CreateTime = DateTime.Now,
|
||||
Content = "",
|
||||
Input_tokens = 0,
|
||||
Output_tokens = 0,
|
||||
SendDateDay = int.Parse(DateTime.Now.ToString("yyyyMMdd")),
|
||||
TimeStamp = DateTime.Now,//DateTimeExtensions.ToUnixTimestamp(),.ToUnixTimestamp()
|
||||
SendMessageDay = DateTime.Now.ToUnixTimestamp(),
|
||||
SessionId = userChatSession.SessionId,
|
||||
TenantId = userChatSession.TenantId,
|
||||
Type = 0,
|
||||
UpdateTime = DateTime.Now,
|
||||
UserId = userChatSession.UserId,
|
||||
ClaudeModel = ""
|
||||
};
|
||||
Dao.daoDbMiaoYu.context.T_Chat.Add(chat);
|
||||
Dao.daoDbMiaoYu.context.SaveChanges();
|
||||
chatList = new List<T_Chat>();
|
||||
chatList.Add(chat);
|
||||
}
|
||||
var message = Mapper.Map<List<ChatMessageDto>>(chatList);
|
||||
//设置头像
|
||||
message.Where(it =>
|
||||
it.Role == ChatRole.assistant.ToString() ||
|
||||
it.Role == ChatRole.initialization.ToString()
|
||||
).ToList()
|
||||
.ForEach(it => it.UserIcon = charact.IconImage);
|
||||
//用户头像
|
||||
var userData = await Dao.daoDbMiaoYu.context.T_User_Data.FirstOrDefaultAsync(it => it.UserId == _UserId);
|
||||
var userIconUrl = userData?.UserIconUrl ?? "";
|
||||
message.Where(it => it.Role == ChatRole.user.ToString()).ToList().ForEach(it => it.UserIcon = userIconUrl);
|
||||
|
||||
//将第一句修改角色
|
||||
var initia = message.FirstOrDefault(it => it.Role == ChatRole.initialization.ToString());
|
||||
if (initia != null)
|
||||
{
|
||||
initia.Role = ChatRole.assistant.ToString();
|
||||
}
|
||||
return new BaseResponse<List<ChatMessageDto>>(ResonseCode.Success, "", message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -94,19 +94,7 @@ namespace HuanMeng.MiaoYu.Model.Dto.Chat
|
|||
// public string LabelName { get; set; }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 聊天列表信息
|
||||
/// </summary>
|
||||
public class ChatMessageDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Role { get; set; }
|
||||
public string Content { get; set; }
|
||||
public DateTime Timestamp { get; set; }
|
||||
public int MessageType { get; set; }
|
||||
public string UserIcon { get; set; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 聊天列表
|
||||
/// </summary>
|
||||
|
|
@ -145,43 +133,5 @@ namespace HuanMeng.MiaoYu.Model.Dto.Chat
|
|||
public int CharacterId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 聊天表信息状态
|
||||
/// </summary>
|
||||
public enum ChatType
|
||||
{
|
||||
/// <summary>
|
||||
/// 正常
|
||||
/// </summary>
|
||||
正常 = 0,
|
||||
|
||||
/// <summary>
|
||||
/// 重新生成
|
||||
/// </summary>
|
||||
重新生成 = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 删除
|
||||
/// </summary>
|
||||
删除 = 2
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 角色分类
|
||||
/// </summary>
|
||||
public enum Role
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户
|
||||
/// </summary>
|
||||
user,
|
||||
/// <summary>
|
||||
/// AI
|
||||
/// </summary>
|
||||
assistant,
|
||||
/// <summary>
|
||||
/// 提示
|
||||
/// </summary>
|
||||
tips
|
||||
}
|
||||
}
|
||||
|
|
|
|||
61
src/0-core/HuanMeng.MiaoYu.Model/Dto/Chat/ChatMessageDto.cs
Normal file
61
src/0-core/HuanMeng.MiaoYu.Model/Dto/Chat/ChatMessageDto.cs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
using AutoMapper;
|
||||
using AutoMapper.Configuration.Annotations;
|
||||
|
||||
using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
||||
using HuanMeng.MiaoYu.Model.EnumModel.Chat;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Model.Dto.Chat
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天列表信息
|
||||
/// </summary>
|
||||
[AutoMap(typeof(T_Chat))]
|
||||
public class ChatMessageDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天记录Id
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
/// <summary>
|
||||
/// 角色
|
||||
/// </summary>
|
||||
public string Role { get; set; }
|
||||
/// <summary>
|
||||
/// 文本内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
/// <summary>
|
||||
/// 发送时间戳
|
||||
/// </summary>
|
||||
public DateTime Timestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息类型
|
||||
/// </summary>
|
||||
public string ClaudeType { get; set; }
|
||||
/// <summary>
|
||||
/// 消息类型
|
||||
/// </summary>
|
||||
//[SourceMember(nameof(T_Chat.ClaudeType))]
|
||||
public int MessageType
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
var x = (ChatMessageType)Enum.Parse(typeof(ChatMessageType), ClaudeType);
|
||||
return (int)x;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 角色头像
|
||||
/// </summary>
|
||||
public string UserIcon { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Model.EnumModel.Chat
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息类型
|
||||
/// </summary>
|
||||
public enum ChatMessageType
|
||||
{
|
||||
/// <summary>
|
||||
/// 文本消息
|
||||
/// </summary>
|
||||
text = 0,
|
||||
|
||||
}
|
||||
}
|
||||
31
src/0-core/HuanMeng.MiaoYu.Model/EnumModel/Chat/ChatRole.cs
Normal file
31
src/0-core/HuanMeng.MiaoYu.Model/EnumModel/Chat/ChatRole.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Model.EnumModel.Chat
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天角色
|
||||
/// </summary>
|
||||
public enum ChatRole
|
||||
{
|
||||
/// <summary>
|
||||
/// 第一次初始化语句
|
||||
/// </summary>
|
||||
initialization,
|
||||
/// <summary>
|
||||
/// 用户
|
||||
/// </summary>
|
||||
user,
|
||||
/// <summary>
|
||||
/// AI
|
||||
/// </summary>
|
||||
assistant,
|
||||
/// <summary>
|
||||
/// 提示
|
||||
/// </summary>
|
||||
tips
|
||||
}
|
||||
}
|
||||
29
src/0-core/HuanMeng.MiaoYu.Model/EnumModel/Chat/ChatType.cs
Normal file
29
src/0-core/HuanMeng.MiaoYu.Model/EnumModel/Chat/ChatType.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Model.EnumModel.Chat
|
||||
{
|
||||
/// <summary>
|
||||
/// 聊天记录类型
|
||||
/// </summary>
|
||||
public enum ChatType
|
||||
{
|
||||
/// <summary>
|
||||
/// 正常
|
||||
/// </summary>
|
||||
正常 = 0,
|
||||
|
||||
/// <summary>
|
||||
/// 重新生成
|
||||
/// </summary>
|
||||
重新生成 = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 删除
|
||||
/// </summary>
|
||||
删除 = 2
|
||||
}
|
||||
}
|
||||
32
src/0-core/HuanMeng.Utility/DateTimeExtensions.cs
Normal file
32
src/0-core/HuanMeng.Utility/DateTimeExtensions.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace HuanMeng.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间扩展
|
||||
/// </summary>
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取时间戳,秒
|
||||
/// </summary>
|
||||
/// <param name="dateTime"></param>
|
||||
/// <returns></returns>
|
||||
public static long ToUnixTimestamp(this DateTime dateTime)
|
||||
{
|
||||
return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取是时间戳,毫秒
|
||||
/// </summary>
|
||||
/// <param name="dateTime"></param>
|
||||
/// <returns></returns>
|
||||
public static long ToUnixTimestampMilliseconds(this DateTime dateTime)
|
||||
{
|
||||
return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -62,7 +62,7 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取聊天列表信息
|
||||
/// 获取聊天记录
|
||||
/// </summary>
|
||||
/// <param name="characterId"></param>
|
||||
/// <returns></returns>
|
||||
|
|
@ -70,8 +70,11 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
|
|||
[AllowAnonymous]
|
||||
public async Task<BaseResponse<ChatListDto>> GetChatInfo(int characterId)
|
||||
{
|
||||
var obj = JsonConvert.DeserializeObject<ChatListDto>("{\"ChatList\":[{\"Id\":\"1\",\"Role\":\"user\",\"Content\":\"Hello, how are you?\",\"Timestamp\":\"2022-03-01 12:00:00 \",\"MessageType\":0,\"UserIcon\":\"\"},{\"Id\":\"2\",\"Role\":\"assistant\",\"Content\":\"I'm fine, thanks!\",\"Timestamp\":\"2022-03-01 12:05:00 \",\"UserIcon\":\"\"}]}");
|
||||
return new BaseResponse<ChatListDto>(ResonseCode.Success, "", obj);
|
||||
ChatListDto chatListDto = new ChatListDto();
|
||||
ChatBLL chatBLL = new ChatBLL(ServiceProvider);
|
||||
var list = await chatBLL.GetChatMessage(characterId);
|
||||
chatListDto.ChatList = list.Data;
|
||||
return new BaseResponse<ChatListDto>(ResonseCode.Success, "", chatListDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ using HuanMeng.MiaoYu.Code.Cache;
|
|||
using HuanMeng.MiaoYu.Code.Chat;
|
||||
using Serilog;
|
||||
using HuanMeng.MiaoYu.Model.Dto;
|
||||
using System.Text.Json.Serialization;
|
||||
using HuanMeng.DotNetCore.Json;
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
//Log.Logger = new LoggerConfiguration()
|
||||
// .WriteTo.Console()
|
||||
|
|
@ -45,7 +47,12 @@ if (type != null)
|
|||
|
||||
builder.Services.AddAutoMapper(mapperDomain);
|
||||
#endregion
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy = null;
|
||||
options.JsonSerializerOptions.Converters.Add(new CustomDateTimeConverter("yyyy-MM-dd HH:mm:ss"));
|
||||
});
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user