diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ThorChatCompletionsRequest.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ThorChatCompletionsRequest.cs index a3fb0c2c..390018dd 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ThorChatCompletionsRequest.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ThorChatCompletionsRequest.cs @@ -13,9 +13,8 @@ public class ThorChatCompletionsRequest { Messages = new List(); } - - [JsonPropertyName("store")] - public bool? Store { get; set; } + + [JsonPropertyName("store")] public bool? Store { get; set; } /// /// 表示对话中支持的模态类型数组。可以为 null。 @@ -26,14 +25,15 @@ public class ThorChatCompletionsRequest /// /// 表示对话中的音频请求参数。可以为 null。 /// - [JsonPropertyName("audio")] public ThorChatAudioRequest? Audio { get; set; } + [JsonPropertyName("audio")] + public ThorChatAudioRequest? Audio { get; set; } /// /// 包含迄今为止对话的消息列表 /// [JsonPropertyName("messages")] public List Messages { get; set; } - + /// /// 模型唯一编码值,如 gpt-4,gpt-3.5-turbo,moonshot-v1-8k,看底层具体平台定义 /// @@ -229,18 +229,25 @@ public class ThorChatCompletionsRequest { if (value is JsonElement jsonElement) { - if (jsonElement.ValueKind == JsonValueKind.String) - { - ToolChoice = new ThorToolChoice - { - Type = jsonElement.GetString() - }; - } - else if (jsonElement.ValueKind == JsonValueKind.Object) + // if (jsonElement.ValueKind == JsonValueKind.String) + // { + // ToolChoice = new ThorToolChoice + // { + // Type = jsonElement.GetString() + // }; + // } + if (jsonElement.ValueKind == JsonValueKind.Object) { ToolChoice = jsonElement.Deserialize(); } } + else if (value is string text) + { + ToolChoice = new ThorToolChoice + { + Type = text + }; + } else { ToolChoice = (ThorToolChoice)value; diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs index ddeef24a..40d9575c 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs @@ -44,7 +44,7 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot public Guid? SessionId { get; set; } [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)] - public string Content { get; set; } + public string? Content { get; set; } public string Role { get; set; } public string ModelId { get; set; } 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 8c3962f9..77281aad 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 @@ -83,7 +83,7 @@ public class AiGateWayManager : DomainService var modelDescribe = await GetModelAsync(request.Model); var chatService = LazyServiceProvider.GetRequiredKeyedService(modelDescribe.HandlerName); - + await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken)) { yield return result; @@ -109,8 +109,7 @@ public class AiGateWayManager : DomainService _specialCompatible.Compatible(request); var response = httpContext.Response; // 设置响应头,声明是 json - response.ContentType = "application/json; charset=UTF-8"; - await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true); + //response.ContentType = "application/json; charset=UTF-8"; var modelDescribe = await GetModelAsync(request.Model); var chatService = LazyServiceProvider.GetRequiredKeyedService(modelDescribe.HandlerName); @@ -136,13 +135,8 @@ public class AiGateWayManager : DomainService await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.Usage.InputTokens ?? 0, data.Usage.OutputTokens ?? 0); } - - var body = JsonConvert.SerializeObject(data, new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver() - }); - await writer.WriteLineAsync(body); - await writer.FlushAsync(cancellationToken); + + await response.WriteAsJsonAsync(data, cancellationToken); } ///