新增 接入ChatGPT-4.o

This commit is contained in:
小肥羊 2024-12-12 11:22:54 +08:00
parent 2a955b5b43
commit 6981314257
6 changed files with 41 additions and 27 deletions

View File

@ -1,23 +1,13 @@
using VideoAnalysisCore.Common; using VideoAnalysisCore.Common;
using Learn.VideoAnalysis.Controllers.Dto;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Reflection; 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 MapsterMapper;
using Mapster; using Mapster;
using VideoAnalysisCore.AICore.SherpaOnnx; using VideoAnalysisCore.AICore.SherpaOnnx;
using System.Net;
using System.Security.Policy;
using System.IO;
using UserCenter.Model.Enum; using UserCenter.Model.Enum;
using VideoAnalysisCore.AICore.GPT.ChatGPT;
using VideoAnalysisCore.AICore.GPT;
namespace Learn.VideoAnalysis.Controllers namespace Learn.VideoAnalysis.Controllers
{ {
@ -28,11 +18,14 @@ namespace Learn.VideoAnalysis.Controllers
private readonly ILogger<ApiController> _logger; private readonly ILogger<ApiController> _logger;
private readonly IMapper mp; private readonly IMapper mp;
private readonly Repository<VideoTask> videoTaskDB; private readonly Repository<VideoTask> videoTaskDB;
public ApiController(ILogger<ApiController> logger, Repository<VideoTask> videoTaskDB, IMapper mp) private readonly IBserGPT chatGPT;
public ApiController(ILogger<ApiController> logger, Repository<VideoTask> videoTaskDB,
IMapper mp, IBserGPT chatGPT)
{ {
_logger = logger; _logger = logger;
this.videoTaskDB = videoTaskDB; this.videoTaskDB = videoTaskDB;
this.mp = mp; this.mp = mp;
this.chatGPT = chatGPT;
} }
private string GetClientIpAddress() private string GetClientIpAddress()
@ -80,6 +73,21 @@ namespace Learn.VideoAnalysis.Controllers
return Ok(res); return Ok(res);
} }
/// <summary>
/// Test
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
[HttpGet(Name = "Test")]
public async Task<IActionResult> Test(long taskId)
{
//ÖØÐ¿ªÊ¼Ö´ÐÐGPT·ÖÎö
RedisExpand.InsertChannel(RedisChannelEnum.ChatModelAnalysis
, taskId);
return Ok();
}
[NonAction] [NonAction]
private static List<TimeBase> MergeTimeBases(IEnumerable<TimeBase> timeBases) private static List<TimeBase> MergeTimeBases(IEnumerable<TimeBase> timeBases)
{ {

View File

@ -80,7 +80,8 @@ namespace Learn.VideoAnalysis
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddSingleton<ChatGPTClient>(); builder.Services.AddSingleton<ChatGPTClient>();
builder.Services.AddSingleton<IBserGPT, KIMI_GPT>(); //builder.Services.AddSingleton<IBserGPT, KIMI_GPT>();
builder.Services.AddSingleton<IBserGPT, Chat_GPT>();
var app = builder.Build(); var app = builder.Build();

View File

@ -85,11 +85,11 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
public async Task<(Usage u, string res)?> Chat(ChatRequest chatReq) public async Task<(Usage u, string res)?> Chat(ChatRequest chatReq)
{ {
var requestBody = System.Text.Json.JsonSerializer.Serialize(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<ChatRes>(); var res = await chatResp.Content.ReadFromJsonAsync<ChatRes>();
var chatResContent = res?.choices.FirstOrDefault()?.message.content.Trim(); var chatResContent = res?.choices.FirstOrDefault()?.message.content.Trim();
if (res is null) if (res is null || res.error != null)
throw new Exception($" ChatGPT模型返回异常 返回参数: " + throw new Exception($" ChatGPT模型返回异常 返回参数: " +
$" {System.Text.Json.JsonSerializer.Serialize(res)}"); $" {System.Text.Json.JsonSerializer.Serialize(res)}");
@ -111,7 +111,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
var client = _httpClientFactory.CreateClient(); var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Authorization = client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", AppCommon.Config.ChatGpt.ChatGpt.ApiKey); 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.DefaultRequestVersion = HttpVersion.Version20;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower; client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
client.DefaultRequestHeaders.ConnectionClose = true; client.DefaultRequestHeaders.ConnectionClose = true;

View File

@ -42,6 +42,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
public string _object { get; set; } public string _object { get; set; }
public int created { get; set; } public int created { get; set; }
public string model { get; set; } public string model { get; set; }
public ChatResError error { get; set; }
public Choice[] choices { get; set; } public Choice[] choices { get; set; }
public Usage usage { get; set; } public Usage usage { get; set; }
/// <summary> /// <summary>
@ -65,5 +66,9 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
public object logprobs { get; set; } public object logprobs { get; set; }
public string finish_reason { get; set; } public string finish_reason { get; set; }
} }
public class ChatResError
{
public string message { get; set; }
public string type { get; set; }
}
} }

View File

@ -26,9 +26,6 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
/// <param name="logger"></param> /// <param name="logger"></param>
public Chat_GPT(ChatGPTClient moonshotClient, Repository<CourseGradingCriteria> criteria, Repository<VideoTask> videoTaskDB) public Chat_GPT(ChatGPTClient moonshotClient, Repository<CourseGradingCriteria> criteria, Repository<VideoTask> videoTaskDB)
{ {
ChatGPTClient.Host = AppCommon.Config.ChatGpt.KIMI.Host;
ChatGPTClient.ApiKey = AppCommon.Config.ChatGpt.KIMI.ApiKey;
this.chatClient = moonshotClient; this.chatClient = moonshotClient;
criteriaDB = criteria; criteriaDB = criteria;
this.videoTaskDB = videoTaskDB; this.videoTaskDB = videoTaskDB;
@ -76,7 +73,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
criteriaBuilder.Append("|"); criteriaBuilder.Append("|");
} }
var resFormat = """{"":number,"":array|bool|object,"":string}"""; var resFormat = """[{"":number,"":array|bool|object,"":string}]""";
var postMessages = var postMessages =
$"你是一个教学经验老道老师对教学工作有着深入的理解和丰富的经验,能够准确把握教学大纲的要求和教学重点。" + $"你是一个教学经验老道老师对教学工作有着深入的理解和丰富的经验,能够准确把握教学大纲的要求和教学重点。" +
$"熟练掌握各种教学管理方法和手段,能够制定科学合理的教学计划和教学评估体系。" + $"熟练掌握各种教学管理方法和手段,能够制定科学合理的教学计划和教学评估体系。" +
@ -85,7 +82,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
$"字幕列表 {captions.Captions} " + $"字幕列表 {captions.Captions} " +
$"基于字幕内容回答提出的所有问题 问题格式(问题编号:问题描述:结束秒:结果类型|下一个问题)" + $"基于字幕内容回答提出的所有问题 问题格式(问题编号:问题描述:结束秒:结果类型|下一个问题)" +
$"问题列表 {criteriaBuilder} " $"问题列表 {criteriaBuilder} "
+ $"返回固定的JSON数组格式({resFormat})"; + $"返回固定的JSON格式({resFormat})";
var maxTokens = 4000; var maxTokens = 4000;
var chatRep = new ChatRequest var chatRep = new ChatRequest
@ -108,9 +105,12 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
chatResContent = chatResContent?.Replace("字幕内容", "课堂情况"); chatResContent = chatResContent?.Replace("字幕内容", "课堂情况");
chatResContent = chatResContent?.Replace("\n", ""); chatResContent = chatResContent?.Replace("\n", "");
chatResContent = chatResContent?.Replace("```json", "");
chatResContent = chatResContent?.Replace("```", "");
chatResContent = chatResContent?.Replace("}{", "},{"); chatResContent = chatResContent?.Replace("}{", "},{");
chatResContent = chatResContent?.Replace("}|{", "},{"); chatResContent = chatResContent?.Replace("}|{", "},{");
chatResContent = chatResContent?.Trim(); chatResContent = chatResContent?.Trim();
if (!chatResContent.StartsWith("[")) if (!chatResContent.StartsWith("["))
chatResContent = "[" + chatResContent; chatResContent = "[" + chatResContent;
if (!chatResContent.EndsWith("]")) if (!chatResContent.EndsWith("]"))

View File

@ -25,7 +25,7 @@ namespace VideoAnalysisCore.AICore.GPT.KIMI
/// </summary> /// </summary>
public class KIMI_GPT : IBserGPT public class KIMI_GPT : IBserGPT
{ {
private readonly ChatGPTClient moonshotClient; private readonly MoonshotClient moonshotClient;
private readonly Repository<CourseGradingCriteria> criteriaDB; private readonly Repository<CourseGradingCriteria> criteriaDB;
private readonly Repository<VideoTask> videoTaskDB; private readonly Repository<VideoTask> videoTaskDB;
/// <summary> /// <summary>
@ -33,10 +33,10 @@ namespace VideoAnalysisCore.AICore.GPT.KIMI
/// </summary> /// </summary>
/// <param name="moonshotClient"></param> /// <param name="moonshotClient"></param>
/// <param name="logger"></param> /// <param name="logger"></param>
public KIMI_GPT(ChatGPTClient moonshotClient, Repository<CourseGradingCriteria> criteria, Repository<VideoTask> videoTaskDB) public KIMI_GPT(MoonshotClient moonshotClient, Repository<CourseGradingCriteria> criteria, Repository<VideoTask> videoTaskDB)
{ {
ChatGPTClient.Host = AppCommon.Config.ChatGpt.KIMI.Host; MoonshotClient.Host = AppCommon.Config.ChatGpt.KIMI.Host;
ChatGPTClient.ApiKey = AppCommon.Config.ChatGpt.KIMI.ApiKey; MoonshotClient.ApiKey = AppCommon.Config.ChatGpt.KIMI.ApiKey;
this.moonshotClient = moonshotClient; this.moonshotClient = moonshotClient;
criteriaDB = criteria; criteriaDB = criteria;