From d72cc529ba538ea7c308dc6f733bee9fd7d176a1 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Mon, 21 Jul 2025 21:15:02 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Managers/AiGateWayManager.cs | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs index 77281aad..66523c1d 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs @@ -135,7 +135,7 @@ public class AiGateWayManager : DomainService await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.Usage.InputTokens ?? 0, data.Usage.OutputTokens ?? 0); } - + await response.WriteAsJsonAsync(data, cancellationToken); } @@ -157,15 +157,14 @@ public class AiGateWayManager : DomainService { var response = httpContext.Response; // 设置响应头,声明是 SSE 流 - response.ContentType = "text/event-stream"; - response.Headers.Append("Cache-Control", "no-cache"); - response.Headers.Append("Connection", "keep-alive"); + response.ContentType = "text/event-stream;charset=utf-8;"; + response.Headers.TryAdd("Cache-Control", "no-cache"); + response.Headers.TryAdd("Connection", "keep-alive"); var gateWay = LazyServiceProvider.GetRequiredService(); var completeChatResponse = gateWay.CompleteChatStreamAsync(request, cancellationToken); var tokenUsage = new ThorUsageResponse(); - await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true); //缓存队列算法 // 创建一个队列来缓存消息 @@ -183,14 +182,14 @@ public class AiGateWayManager : DomainService { if (messageQueue.TryDequeue(out var message)) { - await writer.WriteLineAsync(message); - await writer.FlushAsync(cancellationToken); + await response.WriteAsync(message, Encoding.UTF8, cancellationToken).ConfigureAwait(false); + await response.Body.FlushAsync(cancellationToken).ConfigureAwait(false); } if (!isComplete) { // 如果没有完成,才等待,已完成,全部输出 - await Task.Delay(outputInterval, cancellationToken); + await Task.Delay(outputInterval, cancellationToken).ConfigureAwait(false); } } }, cancellationToken); @@ -206,13 +205,10 @@ public class AiGateWayManager : DomainService tokenUsage = data.Usage; } - var message = JsonConvert.SerializeObject(data, new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver() - }); + var message = System.Text.Json.JsonSerializer.Serialize(data, ThorJsonSerializer.DefaultOptions); backupSystemContent.Append(data.Choices.FirstOrDefault()?.Delta.Content); // 将消息加入队列而不是直接写入 - messageQueue.Enqueue($"data: {message}\n"); + messageQueue.Enqueue($"data: {message}\n\n"); } } catch (Exception e) @@ -237,11 +233,11 @@ public class AiGateWayManager : DomainService ContractResolver = new CamelCasePropertyNamesContractResolver() }); backupSystemContent.Append(errorContent); - messageQueue.Enqueue($"data: {message}\n"); + messageQueue.Enqueue($"data: {message}\n\n"); } //断开连接 - messageQueue.Enqueue("data: [DONE]\n"); + messageQueue.Enqueue("data: [DONE]\n\n"); // 标记完成并发送结束标记 isComplete = true;