Browse Source

feat: Add a query interface for conversation history messages

pull/1421/head
colin 2 weeks ago
parent
commit
892650ab4f
  1. 6
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/ChatMessageDto.cs
  2. 6
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/SendTextChatMessageDto.cs
  3. 9
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/TextChatMessageDto.cs
  4. 11
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/TextChatMessageGetListInput.cs
  5. 4
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/IChatAppService.cs
  6. 21
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/AbpAIManagementApplicationMappers.cs
  7. 24
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/Chats/ChatAppService.cs
  8. 12
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ITextChatMessageRecordRepository.cs
  9. 25
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/EfCoreTextChatMessageRecordRepository.cs
  10. 8
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.HttpApi/LINGYUN/Abp/AIManagement/Chats/ChatController.cs

6
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/ChatMessageDto.cs

@ -8,7 +8,13 @@ public abstract class ChatMessageDto : ExtensibleAuditedEntityDto<Guid>
public DateTime CreatedAt { get; set; }
public string Role { get; set; }
public Guid? UserId { get; set; }
public Guid? ConversationId { get; set; }
public string? ReplyMessage { get; set; }
public DateTime? ReplyAt { get; set; }
}

6
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/SendTextChatMessageDto.cs

@ -7,11 +7,11 @@ namespace LINGYUN.Abp.AIManagement.Chats.Dtos;
public class SendTextChatMessageDto
{
[Required]
[DynamicStringLength(typeof(WorkspaceDefinitionRecordConsts), nameof(WorkspaceDefinitionRecordConsts.MaxNameLength))]
public string Workspace { get; set; }
public Guid ConversationId { get; set; }
[Required]
public Guid ConversationId { get; set; }
[DynamicStringLength(typeof(WorkspaceDefinitionRecordConsts), nameof(WorkspaceDefinitionRecordConsts.MaxNameLength))]
public string Workspace { get; set; }
[Required]
[DynamicStringLength(typeof(TextChatMessageRecordConsts), nameof(TextChatMessageRecordConsts.MaxContentLength))]

9
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/TextChatMessageDto.cs

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LINGYUN.Abp.AIManagement.Chats.Dtos;
public class TextChatMessageDto
namespace LINGYUN.Abp.AIManagement.Chats.Dtos;
public class TextChatMessageDto : ChatMessageDto
{
public string Content { get; set; }
}

11
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/Dtos/TextChatMessageGetListInput.cs

@ -0,0 +1,11 @@
using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Application.Dtos;
namespace LINGYUN.Abp.AIManagement.Chats.Dtos;
public class TextChatMessageGetListInput : PagedAndSortedResultRequestDto
{
[Required]
public Guid ConversationId { get; set; }
}

4
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Chats/IChatAppService.cs

@ -1,9 +1,13 @@
using LINGYUN.Abp.AIManagement.Chats.Dtos;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
namespace LINGYUN.Abp.AIManagement.Chats;
public interface IChatAppService : IApplicationService
{
IAsyncEnumerable<string> SendMessageAsync(SendTextChatMessageDto input);
Task<PagedResultDto<TextChatMessageDto>> GetListAsync(TextChatMessageGetListInput input);
}

21
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/AbpAIManagementApplicationMappers.cs

@ -1,4 +1,6 @@
using LINGYUN.Abp.AIManagement.Workspaces;
using LINGYUN.Abp.AIManagement.Chats;
using LINGYUN.Abp.AIManagement.Chats.Dtos;
using LINGYUN.Abp.AIManagement.Workspaces;
using LINGYUN.Abp.AIManagement.Workspaces.Dtos;
using Riok.Mapperly.Abstractions;
using Volo.Abp.Mapperly;
@ -12,4 +14,21 @@ public partial class WorkspaceDefinitionRecordToWorkspaceDefinitionRecordDtoMapp
{
public override partial WorkspaceDefinitionRecordDto Map(WorkspaceDefinitionRecord source);
public override partial void Map(WorkspaceDefinitionRecord source, WorkspaceDefinitionRecordDto destination);
}
[Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Target)]
[MapExtraProperties(DefinitionChecks = MappingPropertyDefinitionChecks.None)]
public partial class TextChatMessageRecordToTextChatMessageDtoMapper : MapperBase<TextChatMessageRecord, TextChatMessageDto>
{
[MapPropertyFromSource(nameof(TextChatMessageDto.Role), Use = nameof(ConvertChatRole))]
public override partial TextChatMessageDto Map(TextChatMessageRecord source);
[MapPropertyFromSource(nameof(TextChatMessageDto.Role), Use = nameof(ConvertChatRole))]
public override partial void Map(TextChatMessageRecord source, TextChatMessageDto destination);
[UserMapping(Default = false)]
private static string ConvertChatRole(TextChatMessageRecord record)
{
return record.Role.Value;
}
}

24
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/Chats/ChatAppService.cs

@ -3,17 +3,24 @@ using LINGYUN.Abp.AI.Models;
using LINGYUN.Abp.AIManagement.Chats.Dtos;
using Microsoft.Extensions.AI;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Specifications;
namespace LINGYUN.Abp.AIManagement.Chats;
public class ChatAppService : AIManagementApplicationServiceBase, IChatAppService
{
private readonly IAgentService _agentService;
public ChatAppService(IAgentService agentService)
private readonly ITextChatMessageRecordRepository _repository;
public ChatAppService(
IAgentService agentService,
ITextChatMessageRecordRepository repository)
{
_agentService = agentService;
_repository = repository;
}
public IAsyncEnumerable<string> SendMessageAsync(SendTextChatMessageDto input)
public virtual IAsyncEnumerable<string> SendMessageAsync(SendTextChatMessageDto input)
{
var chatMessage = new TextChatMessage(
input.Workspace,
@ -25,4 +32,17 @@ public class ChatAppService : AIManagementApplicationServiceBase, IChatAppServic
return _agentService.SendMessageAsync(chatMessage);
}
public async virtual Task<PagedResultDto<TextChatMessageDto>> GetListAsync(TextChatMessageGetListInput input)
{
var specification = new ExpressionSpecification<TextChatMessageRecord>(
x => x.ConversationId == input.ConversationId);
var totalCount = await _repository.GetCountAsync(specification);
var textChatMessages = await _repository.GetListAsync(specification,
input.Sorting, input.MaxResultCount, input.SkipCount);
return new PagedResultDto<TextChatMessageDto>(totalCount,
ObjectMapper.Map<List<TextChatMessageRecord>, List<TextChatMessageDto>>(textChatMessages));
}
}

12
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ITextChatMessageRecordRepository.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Specifications;
namespace LINGYUN.Abp.AIManagement.Chats;
public interface ITextChatMessageRecordRepository : IBasicRepository<TextChatMessageRecord, Guid>
@ -11,4 +12,15 @@ public interface ITextChatMessageRecordRepository : IBasicRepository<TextChatMes
Guid conversationId,
int maxResultCount = 0,
CancellationToken cancellationToken = default);
Task<int> GetCountAsync(
ISpecification<TextChatMessageRecord> specification,
CancellationToken cancellationToken = default);
Task<List<TextChatMessageRecord>> GetListAsync(
ISpecification<TextChatMessageRecord> specification,
string? sorting = $"{nameof(TextChatMessageRecord.CreatedAt)}",
int maxResultCount = 10,
int skipCount = 0,
CancellationToken cancellationToken = default);
}

25
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/EfCoreTextChatMessageRecordRepository.cs

@ -3,10 +3,12 @@ using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Specifications;
namespace LINGYUN.Abp.AIManagement.EntityFrameworkCore;
public class EfCoreTextChatMessageRecordRepository : EfCoreRepository<IAIManagementDbContext, TextChatMessageRecord, Guid>, ITextChatMessageRecordRepository
@ -29,4 +31,27 @@ public class EfCoreTextChatMessageRecordRepository : EfCoreRepository<IAIManagem
.OrderBy(x => x.CreationTime)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public async virtual Task<int> GetCountAsync(
ISpecification<TextChatMessageRecord> specification,
CancellationToken cancellationToken = default)
{
return await (await GetQueryableAsync())
.Where(specification.ToExpression())
.CountAsync(GetCancellationToken(cancellationToken));
}
public async virtual Task<List<TextChatMessageRecord>> GetListAsync(
ISpecification<TextChatMessageRecord> specification,
string? sorting = $"{nameof(TextChatMessageRecord.CreatedAt)}",
int maxResultCount = 10,
int skipCount = 0,
CancellationToken cancellationToken = default)
{
return await (await GetQueryableAsync())
.Where(specification.ToExpression())
.OrderBy(!sorting.IsNullOrWhiteSpace() ? sorting : $"{nameof(TextChatMessageRecord.CreatedAt)}")
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
}

8
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.HttpApi/LINGYUN/Abp/AIManagement/Chats/ChatController.cs

@ -1,7 +1,9 @@
using LINGYUN.Abp.AIManagement.Chats.Dtos;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
namespace LINGYUN.Abp.AIManagement.Chats;
@ -27,4 +29,10 @@ public class ChatController : AbpControllerBase, IChatAppService
yield return content;
}
}
[HttpGet]
public virtual Task<PagedResultDto<TextChatMessageDto>> GetListAsync(TextChatMessageGetListInput input)
{
return _service.GetListAsync(input);
}
}

Loading…
Cancel
Save