Files
Yi.Admin/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs

278 lines
11 KiB
C#
Raw Normal View History

2025-07-05 15:11:56 +08:00
using System.Collections.Concurrent;
2025-06-25 17:12:09 +08:00
using System.Runtime.CompilerServices;
2025-07-05 15:11:56 +08:00
using System.Text;
using Microsoft.AspNetCore.Http;
2025-06-21 01:08:14 +08:00
using Microsoft.Extensions.DependencyInjection;
2025-07-05 15:11:56 +08:00
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
2025-06-21 01:08:14 +08:00
using Volo.Abp.Domain.Services;
2025-07-05 15:11:56 +08:00
using Yi.Framework.AiHub.Application.Contracts.Dtos;
2025-07-09 21:52:00 +08:00
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAi;
2025-07-17 23:10:26 +08:00
using Yi.Framework.AiHub.Domain.AiGateWay;
2025-06-27 22:13:26 +08:00
using Yi.Framework.AiHub.Domain.Entities.Model;
2025-06-25 17:12:09 +08:00
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.SqlSugarCore.Abstractions;
2025-06-21 01:08:14 +08:00
namespace Yi.Framework.AiHub.Domain.Managers;
public class AiGateWayManager : DomainService
{
2025-06-25 17:12:09 +08:00
private readonly ISqlSugarRepository<AiAppAggregateRoot> _aiAppRepository;
2025-07-05 15:11:56 +08:00
private readonly ILogger<AiGateWayManager> _logger;
private readonly AiMessageManager _aiMessageManager;
private readonly UsageStatisticsManager _usageStatisticsManager;
2025-07-18 20:46:30 +08:00
private readonly ISpecialCompatible _specialCompatible;
2025-06-21 01:08:14 +08:00
2025-07-05 15:11:56 +08:00
public AiGateWayManager(ISqlSugarRepository<AiAppAggregateRoot> aiAppRepository, ILogger<AiGateWayManager> logger,
2025-07-18 20:46:30 +08:00
AiMessageManager aiMessageManager, UsageStatisticsManager usageStatisticsManager,
ISpecialCompatible specialCompatible)
2025-06-21 01:08:14 +08:00
{
2025-06-25 17:12:09 +08:00
_aiAppRepository = aiAppRepository;
2025-07-05 15:11:56 +08:00
_logger = logger;
_aiMessageManager = aiMessageManager;
_usageStatisticsManager = usageStatisticsManager;
2025-07-18 20:46:30 +08:00
_specialCompatible = specialCompatible;
2025-06-21 01:08:14 +08:00
}
2025-06-25 17:12:09 +08:00
/// <summary>
/// 获取模型
/// </summary>
/// <param name="modelId"></param>
/// <returns></returns>
private async Task<AiModelDescribe> GetModelAsync(string modelId)
2025-06-21 01:08:14 +08:00
{
2025-06-25 17:12:09 +08:00
var allApp = await _aiAppRepository._DbQueryable.Includes(x => x.AiModels).ToListAsync();
foreach (var app in allApp)
2025-06-21 01:08:14 +08:00
{
2025-06-25 17:12:09 +08:00
var model = app.AiModels.FirstOrDefault(x => x.ModelId == modelId);
if (model is not null)
2025-06-21 01:08:14 +08:00
{
2025-06-25 17:12:09 +08:00
return new AiModelDescribe
{
AppId = app.Id,
AppName = app.Name,
Endpoint = app.Endpoint,
ApiKey = app.ApiKey,
OrderNum = model.OrderNum,
HandlerName = model.HandlerName,
ModelId = model.ModelId,
ModelName = model.Name,
2025-07-17 23:16:16 +08:00
Description = model.Description,
2025-07-17 23:52:00 +08:00
AppExtraUrl = app.ExtraUrl,
ModelExtraInfo = model.ExtraInfo
2025-06-25 17:12:09 +08:00
};
2025-06-21 01:08:14 +08:00
}
}
2025-06-21 01:41:05 +08:00
2025-06-25 17:12:09 +08:00
throw new UserFriendlyException($"{modelId}模型当前版本不支持");
}
/// <summary>
2025-07-09 19:12:53 +08:00
/// 聊天完成-流式
2025-06-25 17:12:09 +08:00
/// </summary>
2025-07-17 23:10:26 +08:00
/// <param name="request"></param>
2025-06-25 17:12:09 +08:00
/// <param name="cancellationToken"></param>
/// <returns></returns>
2025-07-17 23:10:26 +08:00
public async IAsyncEnumerable<ThorChatCompletionsResponse> CompleteChatStreamAsync(
ThorChatCompletionsRequest request,
2025-06-25 17:12:09 +08:00
[EnumeratorCancellation] CancellationToken cancellationToken)
{
2025-07-18 20:46:30 +08:00
_specialCompatible.Compatible(request);
2025-07-17 23:10:26 +08:00
var modelDescribe = await GetModelAsync(request.Model);
2025-07-17 23:16:16 +08:00
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
2025-07-18 20:46:30 +08:00
2025-07-17 23:10:26 +08:00
await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken))
2025-06-25 17:12:09 +08:00
{
yield return result;
}
2025-06-21 01:08:14 +08:00
}
2025-07-05 15:11:56 +08:00
2025-07-09 19:12:53 +08:00
/// <summary>
/// 聊天完成-非流式
/// </summary>
/// <param name="httpContext"></param>
2025-07-17 23:10:26 +08:00
/// <param name="request"></param>
2025-07-09 19:12:53 +08:00
/// <param name="userId"></param>
/// <param name="sessionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
2025-07-17 23:10:26 +08:00
public async Task CompleteChatForStatisticsAsync(HttpContext httpContext,
ThorChatCompletionsRequest request,
2025-07-09 19:12:53 +08:00
Guid? userId = null,
Guid? sessionId = null,
CancellationToken cancellationToken = default)
{
2025-07-18 20:46:30 +08:00
_specialCompatible.Compatible(request);
2025-07-09 19:12:53 +08:00
var response = httpContext.Response;
// 设置响应头,声明是 json
response.ContentType = "application/json; charset=UTF-8";
await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
2025-07-17 23:10:26 +08:00
var modelDescribe = await GetModelAsync(request.Model);
2025-07-17 23:16:16 +08:00
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
2025-07-17 23:10:26 +08:00
var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken);
2025-07-09 19:12:53 +08:00
if (userId is not null)
{
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
new MessageInputDto
{
2025-07-17 23:10:26 +08:00
Content = request.Messages.LastOrDefault().Content ?? string.Empty,
ModelId = request.Model,
TokenUsage = data.Usage,
2025-07-09 19:12:53 +08:00
});
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
new MessageInputDto
{
2025-07-17 23:10:26 +08:00
Content = data.Choices.FirstOrDefault()?.Delta.Content,
ModelId = request.Model,
TokenUsage = data.Usage
2025-07-09 19:12:53 +08:00
});
2025-07-17 23:10:26 +08:00
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.Usage.InputTokens ?? 0,
data.Usage.OutputTokens ?? 0);
2025-07-09 19:12:53 +08:00
}
2025-07-17 23:10:26 +08:00
var body = JsonConvert.SerializeObject(data, new JsonSerializerSettings
2025-07-09 19:12:53 +08:00
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
await writer.WriteLineAsync(body);
await writer.FlushAsync(cancellationToken);
}
2025-07-05 15:11:56 +08:00
/// <summary>
/// 聊天完成-缓存处理
/// </summary>
/// <param name="httpContext"></param>
2025-07-17 23:10:26 +08:00
/// <param name="request"></param>
/// <param name="userId"></param>
2025-07-05 15:11:56 +08:00
/// <param name="sessionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
2025-07-09 19:12:53 +08:00
public async Task CompleteChatStreamForStatisticsAsync(
2025-07-05 15:11:56 +08:00
HttpContext httpContext,
2025-07-17 23:10:26 +08:00
ThorChatCompletionsRequest request,
2025-07-05 15:11:56 +08:00
Guid? userId = null,
Guid? sessionId = null,
CancellationToken cancellationToken = default)
{
var response = httpContext.Response;
// 设置响应头,声明是 SSE 流
response.ContentType = "text/event-stream";
response.Headers.Append("Cache-Control", "no-cache");
response.Headers.Append("Connection", "keep-alive");
var gateWay = LazyServiceProvider.GetRequiredService<AiGateWayManager>();
2025-07-17 23:10:26 +08:00
var completeChatResponse = gateWay.CompleteChatStreamAsync(request, cancellationToken);
var tokenUsage = new ThorUsageResponse();
2025-07-05 15:11:56 +08:00
await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
//缓存队列算法
// 创建一个队列来缓存消息
var messageQueue = new ConcurrentQueue<string>();
StringBuilder backupSystemContent = new StringBuilder();
// 设置输出速率例如每50毫秒输出一次
2025-07-08 18:24:21 +08:00
var outputInterval = TimeSpan.FromMilliseconds(75);
2025-07-05 15:11:56 +08:00
// 标记是否完成接收
var isComplete = false;
// 启动一个后台任务来消费队列
var outputTask = Task.Run(async () =>
{
while (!(isComplete && messageQueue.IsEmpty))
{
if (messageQueue.TryDequeue(out var message))
{
await writer.WriteLineAsync(message);
await writer.FlushAsync(cancellationToken);
}
if (!isComplete)
{
// 如果没有完成,才等待,已完成,全部输出
await Task.Delay(outputInterval, cancellationToken);
}
}
}, cancellationToken);
//IAsyncEnumerable 只能在最外层捕获异常(如果你有其他办法的话...
try
{
await foreach (var data in completeChatResponse)
{
2025-07-17 23:10:26 +08:00
if (data.Usage is not null && data.Usage.TotalTokens is not null)
2025-07-05 15:11:56 +08:00
{
2025-07-17 23:10:26 +08:00
tokenUsage = data.Usage;
2025-07-05 15:11:56 +08:00
}
2025-07-17 23:10:26 +08:00
var message = JsonConvert.SerializeObject(data, new JsonSerializerSettings
2025-07-05 15:11:56 +08:00
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
2025-07-17 23:10:26 +08:00
backupSystemContent.Append(data.Choices.FirstOrDefault()?.Delta.Content);
2025-07-05 15:11:56 +08:00
// 将消息加入队列而不是直接写入
messageQueue.Enqueue($"data: {message}\n");
}
}
catch (Exception e)
{
_logger.LogError(e, $"Ai对话异常");
2025-07-17 23:10:26 +08:00
var errorContent = $"Ai异常异常信息\n当前Ai模型{request.Model}\n异常信息{e.Message}";
var model = new ThorChatCompletionsResponse()
{
Choices = new List<ThorChatChoiceResponse>()
{
new ThorChatChoiceResponse()
{
Delta = new ThorChatMessage()
{
Content = errorContent
}
}
}
};
2025-07-05 15:11:56 +08:00
var message = JsonConvert.SerializeObject(model, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
backupSystemContent.Append(errorContent);
messageQueue.Enqueue($"data: {message}\n");
}
//断开连接
messageQueue.Enqueue("data: [DONE]\n");
// 标记完成并发送结束标记
isComplete = true;
await outputTask;
if (userId is not null)
{
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
new MessageInputDto
{
2025-07-17 23:10:26 +08:00
Content = request.Messages.LastOrDefault()?.Content ?? string.Empty,
ModelId = request.Model,
2025-07-05 15:11:56 +08:00
TokenUsage = tokenUsage,
});
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
new MessageInputDto
{
Content = backupSystemContent.ToString(),
2025-07-17 23:10:26 +08:00
ModelId = request.Model,
2025-07-05 15:11:56 +08:00
TokenUsage = tokenUsage
});
2025-07-17 23:10:26 +08:00
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, tokenUsage.InputTokens ?? 0,
tokenUsage.OutputTokens ?? 0);
2025-07-05 15:11:56 +08:00
}
}
2025-06-21 01:08:14 +08:00
}