新增 接入ChatGPT-4.o
This commit is contained in:
parent
2a955b5b43
commit
6981314257
|
|
@ -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<ApiController> _logger;
|
||||
private readonly IMapper mp;
|
||||
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;
|
||||
this.videoTaskDB = videoTaskDB;
|
||||
this.mp = mp;
|
||||
this.chatGPT = chatGPT;
|
||||
}
|
||||
|
||||
private string GetClientIpAddress()
|
||||
|
|
@ -80,6 +73,21 @@ namespace Learn.VideoAnalysis.Controllers
|
|||
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]
|
||||
private static List<TimeBase> MergeTimeBases(IEnumerable<TimeBase> timeBases)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -80,7 +80,8 @@ namespace Learn.VideoAnalysis
|
|||
|
||||
builder.Services.AddHttpClient();
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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<ChatRes>();
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
/// <summary>
|
||||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,9 +26,6 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
|
|||
/// <param name="logger"></param>
|
||||
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;
|
||||
criteriaDB = criteria;
|
||||
this.videoTaskDB = videoTaskDB;
|
||||
|
|
@ -76,7 +73,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
|
|||
criteriaBuilder.Append("|");
|
||||
}
|
||||
|
||||
var resFormat = """{"问题编号":number,"结果":array|bool|object,"问题解释":string}""";
|
||||
var resFormat = """[{"问题编号":number,"结果":array|bool|object,"问题解释":string}]""";
|
||||
var postMessages =
|
||||
$"你是一个教学经验老道老师对教学工作有着深入的理解和丰富的经验,能够准确把握教学大纲的要求和教学重点。" +
|
||||
$"熟练掌握各种教学管理方法和手段,能够制定科学合理的教学计划和教学评估体系。" +
|
||||
|
|
@ -85,7 +82,7 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
|
|||
$"字幕列表 {captions.Captions} " +
|
||||
$"基于字幕内容回答提出的所有问题 问题格式(问题编号:问题描述:结束秒:结果类型|下一个问题)" +
|
||||
$"问题列表 {criteriaBuilder} "
|
||||
+ $"返回固定的JSON数组格式({resFormat})";
|
||||
+ $"返回固定的JSON格式({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("]"))
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ namespace VideoAnalysisCore.AICore.GPT.KIMI
|
|||
/// </summary>
|
||||
public class KIMI_GPT : IBserGPT
|
||||
{
|
||||
private readonly ChatGPTClient moonshotClient;
|
||||
private readonly MoonshotClient moonshotClient;
|
||||
private readonly Repository<CourseGradingCriteria> criteriaDB;
|
||||
private readonly Repository<VideoTask> videoTaskDB;
|
||||
/// <summary>
|
||||
|
|
@ -33,10 +33,10 @@ namespace VideoAnalysisCore.AICore.GPT.KIMI
|
|||
/// </summary>
|
||||
/// <param name="moonshotClient"></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;
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue