diff --git a/VideoAnalysis/Controllers/ApiController.cs b/VideoAnalysis/Controllers/ApiController.cs index 9a9435d..ca4dcae 100644 --- a/VideoAnalysis/Controllers/ApiController.cs +++ b/VideoAnalysis/Controllers/ApiController.cs @@ -1,23 +1,13 @@ using VideoAnalysisCore.Common; -using Learn.VideoAnalysis.Controllers.Dto; using Microsoft.AspNetCore.Mvc; using System.Reflection; -using VideoAnalysisCore.Enum; -using VideoAnalysisCore.Model; -using VideoAnalysisCore.AICore.FFMPGE; -using VideoAnalysisCore.Model.Dto; -using VideoAnalysisCore.AICore.ChatGPT.Dto; -using AntDesign; -using System.Threading.Tasks; -using FFmpeg.NET.Services; using MapsterMapper; using Mapster; using VideoAnalysisCore.AICore.SherpaOnnx; -using System.Net; -using System.Security.Policy; -using System.IO; using UserCenter.Model.Enum; +using VideoAnalysisCore.AICore.GPT.ChatGPT; +using VideoAnalysisCore.AICore.GPT; namespace Learn.VideoAnalysis.Controllers { @@ -28,11 +18,14 @@ namespace Learn.VideoAnalysis.Controllers private readonly ILogger _logger; private readonly IMapper mp; private readonly Repository videoTaskDB; - public ApiController(ILogger logger, Repository videoTaskDB, IMapper mp) + private readonly IBserGPT chatGPT; + public ApiController(ILogger logger, Repository videoTaskDB, + IMapper mp, IBserGPT chatGPT) { _logger = logger; this.videoTaskDB = videoTaskDB; this.mp = mp; + this.chatGPT = chatGPT; } private string GetClientIpAddress() @@ -80,6 +73,21 @@ namespace Learn.VideoAnalysis.Controllers return Ok(res); } + + /// + /// Test + /// + /// + /// + [HttpGet(Name = "Test")] + public async Task Test(long taskId) + { + //重新开始执行GPT分析 + RedisExpand.InsertChannel(RedisChannelEnum.ChatModelAnalysis + , taskId); + return Ok(); + } + [NonAction] private static List MergeTimeBases(IEnumerable timeBases) { diff --git a/VideoAnalysis/Program.cs b/VideoAnalysis/Program.cs index 071d5e4..5efc6a4 100644 --- a/VideoAnalysis/Program.cs +++ b/VideoAnalysis/Program.cs @@ -80,7 +80,8 @@ namespace Learn.VideoAnalysis builder.Services.AddHttpClient(); builder.Services.AddSingleton(); - builder.Services.AddSingleton(); + //builder.Services.AddSingleton(); + builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTClient.cs b/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTClient.cs index 902c340..28690de 100644 --- a/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTClient.cs +++ b/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTClient.cs @@ -85,11 +85,11 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT public async Task<(Usage u, string res)?> Chat(ChatRequest chatReq) { var requestBody = System.Text.Json.JsonSerializer.Serialize(chatReq); - var chatResp = await PostJsonStreamAsync("/v1/chat/completions", requestBody); + var chatResp = await PostJsonStreamAsync("v1/chat/completions", requestBody); var res = await chatResp.Content.ReadFromJsonAsync(); var chatResContent = res?.choices.FirstOrDefault()?.message.content.Trim(); - if (res is null) + if (res is null || res.error != null) throw new Exception($" ChatGPT妯″瀷杩斿洖寮傚父 杩斿洖鍙傛暟: " + $" {System.Text.Json.JsonSerializer.Serialize(res)}"); @@ -111,7 +111,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT var client = _httpClientFactory.CreateClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppCommon.Config.ChatGpt.ChatGpt.ApiKey); - client.Timeout = TimeSpan.FromSeconds(Timeout.Infinite);//瓒呮椂鏃堕棿20鍒嗛挓 + client.Timeout = TimeSpan.FromSeconds(60 *20);//瓒呮椂鏃堕棿20鍒嗛挓 client.DefaultRequestVersion = HttpVersion.Version20; client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower; client.DefaultRequestHeaders.ConnectionClose = true; diff --git a/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTModel.cs b/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTModel.cs index 28be7fc..8279331 100644 --- a/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTModel.cs +++ b/VideoAnalysisCore/AICore/GPT/ChatGPT/ChatGPTModel.cs @@ -42,6 +42,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT public string _object { get; set; } public int created { get; set; } public string model { get; set; } + public ChatResError error { get; set; } public Choice[] choices { get; set; } public Usage usage { get; set; } /// @@ -65,5 +66,9 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT public object logprobs { get; set; } public string finish_reason { get; set; } } - + public class ChatResError + { + public string message { get; set; } + public string type { get; set; } + } } diff --git a/VideoAnalysisCore/AICore/GPT/ChatGPT/Chat_GPT.cs b/VideoAnalysisCore/AICore/GPT/ChatGPT/Chat_GPT.cs index 8e49cd7..7a0bf79 100644 --- a/VideoAnalysisCore/AICore/GPT/ChatGPT/Chat_GPT.cs +++ b/VideoAnalysisCore/AICore/GPT/ChatGPT/Chat_GPT.cs @@ -26,9 +26,6 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT /// public Chat_GPT(ChatGPTClient moonshotClient, Repository criteria, Repository videoTaskDB) { - ChatGPTClient.Host = AppCommon.Config.ChatGpt.KIMI.Host; - ChatGPTClient.ApiKey = AppCommon.Config.ChatGpt.KIMI.ApiKey; - this.chatClient = moonshotClient; criteriaDB = criteria; this.videoTaskDB = videoTaskDB; @@ -76,7 +73,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT criteriaBuilder.Append("|"); } - var resFormat = """{"闂缂栧彿":number,"缁撴灉":array|bool|object,"闂瑙i噴":string}"""; + var resFormat = """[{"闂缂栧彿":number,"缁撴灉":array|bool|object,"闂瑙i噴":string}]"""; var postMessages = $"浣犳槸涓涓暀瀛︾粡楠岃侀亾鑰佸笀瀵规暀瀛﹀伐浣滄湁鐫娣卞叆鐨勭悊瑙e拰涓板瘜鐨勭粡楠岋紝鑳藉鍑嗙‘鎶婃彙鏁欏澶х翰鐨勮姹傚拰鏁欏閲嶇偣銆" + $"鐔熺粌鎺屾彙鍚勭鏁欏绠$悊鏂规硶鍜屾墜娈碉紝鑳藉鍒跺畾绉戝鍚堢悊鐨勬暀瀛﹁鍒掑拰鏁欏璇勪及浣撶郴銆" + @@ -85,7 +82,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT $"瀛楀箷鍒楄〃 {captions.Captions} " + $"鍩轰簬瀛楀箷鍐呭鍥炵瓟鎻愬嚭鐨勬墍鏈夐棶棰 闂鏍煎紡(闂缂栧彿:闂鎻忚堪:缁撴潫绉:缁撴灉绫诲瀷|涓嬩竴涓棶棰)" + $"闂鍒楄〃 {criteriaBuilder} " - + $"杩斿洖鍥哄畾鐨凧SON鏁扮粍鏍煎紡({resFormat})"; + + $"杩斿洖鍥哄畾鐨凧SON鏍煎紡({resFormat})"; var maxTokens = 4000; var chatRep = new ChatRequest @@ -108,9 +105,12 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT chatResContent = chatResContent?.Replace("瀛楀箷鍐呭", "璇惧爞鎯呭喌"); chatResContent = chatResContent?.Replace("\n", ""); + chatResContent = chatResContent?.Replace("```json", ""); + chatResContent = chatResContent?.Replace("```", ""); chatResContent = chatResContent?.Replace("}{", "},{"); chatResContent = chatResContent?.Replace("}|{", "},{"); chatResContent = chatResContent?.Trim(); + if (!chatResContent.StartsWith("[")) chatResContent = "[" + chatResContent; if (!chatResContent.EndsWith("]")) diff --git a/VideoAnalysisCore/AICore/GPT/KIMI/KIMI_GPT.cs b/VideoAnalysisCore/AICore/GPT/KIMI/KIMI_GPT.cs index aecfad7..36a2dd5 100644 --- a/VideoAnalysisCore/AICore/GPT/KIMI/KIMI_GPT.cs +++ b/VideoAnalysisCore/AICore/GPT/KIMI/KIMI_GPT.cs @@ -25,7 +25,7 @@ namespace VideoAnalysisCore.AICore.GPT.KIMI /// public class KIMI_GPT : IBserGPT { - private readonly ChatGPTClient moonshotClient; + private readonly MoonshotClient moonshotClient; private readonly Repository criteriaDB; private readonly Repository videoTaskDB; /// @@ -33,10 +33,10 @@ namespace VideoAnalysisCore.AICore.GPT.KIMI /// /// /// - public KIMI_GPT(ChatGPTClient moonshotClient, Repository criteria, Repository videoTaskDB) + public KIMI_GPT(MoonshotClient moonshotClient, Repository criteria, Repository videoTaskDB) { - ChatGPTClient.Host = AppCommon.Config.ChatGpt.KIMI.Host; - ChatGPTClient.ApiKey = AppCommon.Config.ChatGpt.KIMI.ApiKey; + MoonshotClient.Host = AppCommon.Config.ChatGpt.KIMI.Host; + MoonshotClient.ApiKey = AppCommon.Config.ChatGpt.KIMI.ApiKey; this.moonshotClient = moonshotClient; criteriaDB = criteria;