diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs index 69410d1a..1d1d74d2 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs @@ -184,7 +184,7 @@ public class AiChatService : ApplicationService //断开连接 - messageQueue.Enqueue("data: done\n"); + messageQueue.Enqueue("data: [DONE]\n"); // 标记完成并发送结束标记 isComplete = true; diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs index 1d2d6357..05a74946 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs @@ -68,14 +68,17 @@ public class AzureRestChatService : IChatService try { var jsonObj = MapToJObject(line); - var content = GetContent(jsonObj); - var tokenUsage = GetTokenUsage(jsonObj); - result= new CompleteChatResponse + if (jsonObj is not null) { - TokenUsage = tokenUsage, - IsFinish = tokenUsage is not null, - Content = content - }; + var content = GetContent(jsonObj); + var tokenUsage = GetTokenUsage(jsonObj); + result = new CompleteChatResponse + { + TokenUsage = tokenUsage, + IsFinish = tokenUsage is not null, + Content = content + }; + } } catch (Exception e) { @@ -88,6 +91,11 @@ public class AzureRestChatService : IChatService private JObject? MapToJObject(string line) { + if (line == "data: [DONE]"||string.IsNullOrWhiteSpace(line) ) + { + return null; + } + if (string.IsNullOrWhiteSpace(line)) return null; string prefix = "data: "; @@ -111,12 +119,24 @@ public class AzureRestChatService : IChatService var usage = jsonObj.SelectToken("usage"); if (usage is not null && usage.Type != JTokenType.Null) { - var result = new TokenUsage() + var result = new TokenUsage(); + var completionTokens = usage["completion_tokens"]; + if (completionTokens is not null && completionTokens.Type != JTokenType.Null) { - OutputTokenCount = usage["completion_tokens"].ToObject(), - InputTokenCount = usage["prompt_tokens"].ToObject(), - TotalTokenCount = usage["total_tokens"].ToObject() - }; + result.OutputTokenCount = completionTokens.ToObject(); + } + + var promptTokens = usage["prompt_tokens"]; + if (promptTokens is not null && promptTokens.Type != JTokenType.Null) + { + result.InputTokenCount = promptTokens.ToObject(); + } + + var totalTokens = usage["total_tokens"]; + if (totalTokens is not null && totalTokens.Type != JTokenType.Null) + { + result.TotalTokenCount = totalTokens.ToObject(); + } return result; } diff --git a/Yi.Ai.Vue3/types/import_meta.d.ts b/Yi.Ai.Vue3/types/import_meta.d.ts index 7ac88368..caa83c90 100644 --- a/Yi.Ai.Vue3/types/import_meta.d.ts +++ b/Yi.Ai.Vue3/types/import_meta.d.ts @@ -6,6 +6,7 @@ interface ImportMetaEnv { readonly VITE_WEB_ENV: string; readonly VITE_WEB_BASE_API: string; readonly VITE_API_URL: string; + readonly VITE_BUILD_COMPRESS: string; } declare interface ImportMeta {