fix: 兼容claude ai

This commit is contained in:
ccnetcore
2025-06-27 22:49:08 +08:00
parent 2b07061c18
commit bbedd01a72
3 changed files with 34 additions and 13 deletions

View File

@@ -184,7 +184,7 @@ public class AiChatService : ApplicationService
//断开连接 //断开连接
messageQueue.Enqueue("data: done\n"); messageQueue.Enqueue("data: [DONE]\n");
// 标记完成并发送结束标记 // 标记完成并发送结束标记
isComplete = true; isComplete = true;

View File

@@ -68,14 +68,17 @@ public class AzureRestChatService : IChatService
try try
{ {
var jsonObj = MapToJObject(line); var jsonObj = MapToJObject(line);
var content = GetContent(jsonObj); if (jsonObj is not null)
var tokenUsage = GetTokenUsage(jsonObj);
result= new CompleteChatResponse
{ {
TokenUsage = tokenUsage, var content = GetContent(jsonObj);
IsFinish = tokenUsage is not null, var tokenUsage = GetTokenUsage(jsonObj);
Content = content result = new CompleteChatResponse
}; {
TokenUsage = tokenUsage,
IsFinish = tokenUsage is not null,
Content = content
};
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -88,6 +91,11 @@ public class AzureRestChatService : IChatService
private JObject? MapToJObject(string line) private JObject? MapToJObject(string line)
{ {
if (line == "data: [DONE]"||string.IsNullOrWhiteSpace(line) )
{
return null;
}
if (string.IsNullOrWhiteSpace(line)) if (string.IsNullOrWhiteSpace(line))
return null; return null;
string prefix = "data: "; string prefix = "data: ";
@@ -111,12 +119,24 @@ public class AzureRestChatService : IChatService
var usage = jsonObj.SelectToken("usage"); var usage = jsonObj.SelectToken("usage");
if (usage is not null && usage.Type != JTokenType.Null) 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<int>(), result.OutputTokenCount = completionTokens.ToObject<int>();
InputTokenCount = usage["prompt_tokens"].ToObject<int>(), }
TotalTokenCount = usage["total_tokens"].ToObject<int>()
}; var promptTokens = usage["prompt_tokens"];
if (promptTokens is not null && promptTokens.Type != JTokenType.Null)
{
result.InputTokenCount = promptTokens.ToObject<int>();
}
var totalTokens = usage["total_tokens"];
if (totalTokens is not null && totalTokens.Type != JTokenType.Null)
{
result.TotalTokenCount = totalTokens.ToObject<int>();
}
return result; return result;
} }

View File

@@ -6,6 +6,7 @@ interface ImportMetaEnv {
readonly VITE_WEB_ENV: string; readonly VITE_WEB_ENV: string;
readonly VITE_WEB_BASE_API: string; readonly VITE_WEB_BASE_API: string;
readonly VITE_API_URL: string; readonly VITE_API_URL: string;
readonly VITE_BUILD_COMPRESS: string;
} }
declare interface ImportMeta { declare interface ImportMeta {