diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs index 3d4f11183..6fe8db044 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs @@ -54,8 +54,10 @@ public class AgentService : IAgentService, IScopedDependency tokenUsageInfo.WithConversationId(message.ConversationId); tokenUsageInfo.WithMessageId(messageId); +#if DEBUG Console.WriteLine(); - Console.WriteLine($"消耗Token: {tokenUsageInfo}"); + Console.WriteLine(tokenUsageInfo); +#endif await StoreTokenUsageInfo(tokenUsageInfo); } @@ -64,9 +66,9 @@ public class AgentService : IAgentService, IScopedDependency { var messages = new List(); - if (!message.ConversationId.IsNullOrWhiteSpace()) + if (message.ConversationId.HasValue) { - var historyMessages = await _chatMessageStore.GetHistoryMessagesAsync(message.ConversationId); + var historyMessages = await _chatMessageStore.GetHistoryMessagesAsync(message.ConversationId.Value); foreach (var chatMessage in historyMessages) { diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/IChatMessageStore.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/IChatMessageStore.cs index 023f79b20..886a0e953 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/IChatMessageStore.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/IChatMessageStore.cs @@ -1,4 +1,5 @@ using LINGYUN.Abp.AI.Models; +using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -7,5 +8,5 @@ public interface IChatMessageStore { Task SaveMessageAsync(ChatMessage message); - Task> GetHistoryMessagesAsync(string conversationId); + Task> GetHistoryMessagesAsync(Guid conversationId); } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/InMemoryChatMessageStore.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/InMemoryChatMessageStore.cs index 293e57202..b81ccef2b 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/InMemoryChatMessageStore.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Chats/InMemoryChatMessageStore.cs @@ -14,7 +14,7 @@ public class InMemoryChatMessageStore : IChatMessageStore { private static readonly ConcurrentDictionary> _userMessageCache = new ConcurrentDictionary>(); - public Task> GetHistoryMessagesAsync(string conversationId) + public Task> GetHistoryMessagesAsync(Guid conversationId) { var messages = new List(); diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/ChatMessage.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/ChatMessage.cs index e173d053a..46770a3f5 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/ChatMessage.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/ChatMessage.cs @@ -8,7 +8,7 @@ public abstract class ChatMessage public string? Id { get; private set; } - public string? ConversationId { get; private set; } + public Guid? ConversationId { get; private set; } public string? ReplyMessage { get; private set; } @@ -33,7 +33,7 @@ public abstract class ChatMessage return this; } - public virtual ChatMessage WithConversationId(string conversationId) + public virtual ChatMessage WithConversationId(Guid conversationId) { ConversationId = conversationId; return this; diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/TokenUsageInfo.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/TokenUsageInfo.cs index 08238d79b..770454a3a 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/TokenUsageInfo.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/TokenUsageInfo.cs @@ -1,11 +1,12 @@ -using System.Text; +using System; +using System.Text; namespace LINGYUN.Abp.AI.Models; public class TokenUsageInfo { public string Workspace { get; } public string? MessageId { get; private set; } - public string? ConversationId { get; private set; } + public Guid? ConversationId { get; private set; } public long? InputTokenCount { get; set; } public long? OutputTokenCount { get; set; } public long? TotalTokenCount { get; set; } @@ -21,7 +22,7 @@ public class TokenUsageInfo return this; } - public virtual TokenUsageInfo WithConversationId(string? conversationId) + public virtual TokenUsageInfo WithConversationId(Guid? conversationId) { ConversationId = conversationId; return this; diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain.Shared/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecordConsts.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain.Shared/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecordConsts.cs index 3af24e28f..34283daae 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain.Shared/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecordConsts.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain.Shared/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecordConsts.cs @@ -1,6 +1,5 @@ namespace LINGYUN.Abp.AIManagement.Chats; public static class ChatMessageRecordConsts { - public static int MaxConversationIdLength { get; set; } = 64; public static int MaxChatRoleLength { get; set; } = 20; } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecord.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecord.cs index 2b08d4c67..91bd1c0f1 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecord.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageRecord.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.AI; using System; -using Volo.Abp; using Volo.Abp.Data; using Volo.Abp.Domain.Entities.Auditing; using Volo.Abp.MultiTenancy; @@ -16,7 +15,7 @@ public abstract class ChatMessageRecord : AuditedAggregateRoot, IMultiTena public DateTime CreatedAt { get; private set; } - public string? ConversationId { get; private set; } + public Guid? ConversationId { get; private set; } public string? ReplyMessage { get; private set; } @@ -42,9 +41,9 @@ public abstract class ChatMessageRecord : AuditedAggregateRoot, IMultiTena TenantId = tenantId; } - public virtual ChatMessageRecord SetConversationId(string conversationId) + public virtual ChatMessageRecord SetConversationId(Guid conversationId) { - ConversationId = Check.NotNullOrWhiteSpace(conversationId, nameof(conversationId), ChatMessageRecordConsts.MaxConversationIdLength); + ConversationId = conversationId; return this; } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageStore.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageStore.cs index feb7b9f80..add01791f 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageStore.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ChatMessageStore.cs @@ -35,7 +35,7 @@ public class ChatMessageStore : IChatMessageStore, ITransientDependency _messageRecordRepository = messageRecordRepository; } - public async virtual Task> GetHistoryMessagesAsync(string conversationId) + public async virtual Task> GetHistoryMessagesAsync(Guid conversationId) { var maxLatestHistoryMessagesToKeep = await _settingProvider.GetAsync( AIManagementSettingNames.ChatMessage.MaxLatestHistoryMessagesToKeep, 0); diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ITextChatMessageRecordRepository.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ITextChatMessageRecordRepository.cs index ad641cb43..de20f6e27 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ITextChatMessageRecordRepository.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ITextChatMessageRecordRepository.cs @@ -8,7 +8,7 @@ namespace LINGYUN.Abp.AIManagement.Chats; public interface ITextChatMessageRecordRepository : IBasicRepository { Task> GetHistoryMessagesAsync( - string conversationId, + Guid conversationId, int maxResultCount = 0, CancellationToken cancellationToken = default); } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/AIManagementDbContextModelBuilderExtensions.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/AIManagementDbContextModelBuilderExtensions.cs index 90a2d3fc9..185fd3c86 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/AIManagementDbContextModelBuilderExtensions.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/AIManagementDbContextModelBuilderExtensions.cs @@ -27,8 +27,6 @@ public static class AIManagementDbContextModelBuilderExtensions .HasMaxLength(ChatMessageRecordConsts.MaxChatRoleLength) .HasConversion(new ChatRoleValueConverter()) .IsRequired(); - b.Property(x => x.ConversationId) - .HasMaxLength(ChatMessageRecordConsts.MaxConversationIdLength); b.Property(x => x.Content) .HasMaxLength(TextChatMessageRecordConsts.MaxContentLength) .IsRequired(); diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/EfCoreTextChatMessageRecordRepository.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/EfCoreTextChatMessageRecordRepository.cs index 3377e6304..d5d075bd4 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/EfCoreTextChatMessageRecordRepository.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/EfCoreTextChatMessageRecordRepository.cs @@ -18,7 +18,7 @@ public class EfCoreTextChatMessageRecordRepository : EfCoreRepository> GetHistoryMessagesAsync( - string conversationId, + Guid conversationId, int maxResultCount = 0, CancellationToken cancellationToken = default) {