feat: 完成ai message、session搭建

This commit is contained in:
ccnetcore
2025-06-21 13:02:38 +08:00
parent 29985e2118
commit ac04e846fa
18 changed files with 353 additions and 58 deletions

View File

@@ -6,21 +6,28 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OpenAI.Chat;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos;
using Yi.Framework.AiHub.Application.Contracts.Options;
using Yi.Framework.AiHub.Domain.Managers;
namespace Yi.Framework.AiHub.Application.Services;
public class AiService : ApplicationService
/// <summary>
/// ai服务
/// </summary>
public class AiChatService : ApplicationService
{
private readonly AiGateWayOptions _options;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly AiMessageManager _aiMessageManager;
public AiService(IOptions<AiGateWayOptions> options, IHttpContextAccessor httpContextAccessor)
public AiChatService(IOptions<AiGateWayOptions> options, IHttpContextAccessor httpContextAccessor,
AiMessageManager aiMessageManager)
{
_options = options.Value;
this._httpContextAccessor = httpContextAccessor;
_aiMessageManager = aiMessageManager;
}
@@ -75,7 +82,7 @@ public class AiService : ApplicationService
}
var gateWay = LazyServiceProvider.GetRequiredService<AiGateWayManager>();
var completeChatResponse = gateWay.CompleteChatAsync(input.Model, history,cancellationToken);
var completeChatResponse = gateWay.CompleteChatAsync(input.Model, history, cancellationToken);
await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
await foreach (var data in completeChatResponse)
{
@@ -88,10 +95,24 @@ public class AiService : ApplicationService
await writer.WriteLineAsync($"data: {message}\n");
await writer.FlushAsync(cancellationToken); // 确保立即推送数据
}
//断开连接
await writer.WriteLineAsync($"data: done\n");
await writer.FlushAsync(cancellationToken); // 确保立即推送数据
if (CurrentUser.IsAuthenticated && input.SessionId.HasValue)
{
// 等待接入token
// await _aiMessageManager.CreateMessageAsync(CurrentUser.GetId(), input.SessionId.Value, new MessageInputDto
// {
// Content = null,
// Role = null,
// DeductCost = 0,
// TotalTokens = 0,
// ModelId = null,
// Remark = null
// });
}
}

View File

@@ -0,0 +1,40 @@
using Mapster;
using Microsoft.AspNetCore.Authorization;
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Application.Services;
public class MessageService : ApplicationService
{
private readonly ISqlSugarRepository<MessageAggregateRoot> _repository;
public MessageService(ISqlSugarRepository<MessageAggregateRoot> repository)
{
_repository = repository;
}
/// <summary>
/// 查询消息
/// 需要会话id
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[Authorize]
public async Task<PagedResultDto<MessageDto>> GetListAsync(MessageGetListInput input)
{
RefAsync<int> total = 0;
var userId = CurrentUser.GetId();
var entities = await _repository._DbQueryable
.Where(x => x.SessionId == input.SessionId)
.Where(x=>x.UserId == userId)
.OrderByDescending(x => x.Id)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<MessageDto>(total, entities.Adapt<List<MessageDto>>());
}
}

View File

@@ -0,0 +1,83 @@
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Application.Contracts.Dtos;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Application.Services;
public class SessionService : CrudAppService<SessionAggregateRoot, SessionDto, Guid, SessionGetListInput>
{
private readonly ISqlSugarRepository<SessionAggregateRoot, Guid> _repository;
public readonly ISqlSugarRepository<MessageAggregateRoot, Guid> _messageRepository;
public SessionService(ISqlSugarRepository<SessionAggregateRoot, Guid> repository, ISqlSugarRepository<MessageAggregateRoot, Guid> messageRepository) : base(repository)
{
_repository = repository;
_messageRepository = messageRepository;
}
/// <summary>
/// 创建会话
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[Authorize]
public override async Task<SessionDto> CreateAsync(SessionDto input)
{
var entity = await MapToEntityAsync(input);
entity.UserId = CurrentUser.GetId();
await _repository.InsertAsync(entity);
return entity.Adapt<SessionDto>();
}
/// <summary>
/// 详情会话
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Authorize]
public override Task<SessionDto> GetAsync(Guid id)
{
return base.GetAsync(id);
}
/// <summary>
/// 编辑会话
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
[Authorize]
public override Task<SessionDto> UpdateAsync(Guid id, SessionDto input)
{
return base.UpdateAsync(id, input);
}
/// <summary>
/// 删除会话
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Authorize]
public override async Task DeleteAsync(Guid id)
{
await base.DeleteAsync(id);
//对应的消息一起删除
await _messageRepository.DeleteAsync(x => x.SessionId == id);
}
/// <summary>
/// 查询会话
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[Authorize]
public override Task<PagedResultDto<SessionDto>> GetListAsync(SessionGetListInput input)
{
return base.GetListAsync(input);
}
}