优化 gpt返回结果实体解析方式
This commit is contained in:
parent
d3cf043e09
commit
8f4f48fd66
|
|
@ -1,16 +1,11 @@
|
||||||
using VideoAnalysisCore.Common;
|
using VideoAnalysisCore.Common;
|
||||||
using VideoAnalysisRazor;
|
|
||||||
using Learn.VideoAnalysis.Components;
|
using Learn.VideoAnalysis.Components;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using AntDesign.ProLayout;
|
using AntDesign.ProLayout;
|
||||||
using VideoAnalysisCore.AICore.ChatGPT;
|
using VideoAnalysisCore.AICore.ChatGPT;
|
||||||
using VideoAnalysisCore.AICore.ChatGPT.KIMI;
|
using VideoAnalysisCore.AICore.ChatGPT.KIMI;
|
||||||
using VideoAnalysisCore.AICore.SherpaOnnx;
|
using VideoAnalysisCore.AICore.SherpaOnnx;
|
||||||
using SqlSugar;
|
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using VideoAnalysisCore.AICore.ChatGPT.Dto;
|
|
||||||
using System.Text.Json;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
@ -18,7 +19,24 @@ namespace VideoAnalysisCore.AICore.ChatGPT.Dto
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 结果
|
/// 结果
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public JToken 结果 { get; set; }
|
public JsonDocument 结果 { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转换结果为对象
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public T? ToObject<T>()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return System.Text.Json.JsonSerializer.Deserialize<T>(结果.RootElement.GetRawText());
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 问题解释
|
/// 问题解释
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ using VideoAnalysisCore.Enum;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using FreeRedis;
|
using FreeRedis;
|
||||||
using VideoAnalysisCore.Model.Dto;
|
using VideoAnalysisCore.Model.Dto;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using AntDesign;
|
using AntDesign;
|
||||||
using SqlSugar.IOC;
|
using SqlSugar.IOC;
|
||||||
|
|
||||||
|
|
@ -86,7 +85,7 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
var modelId = reqTokenCount > 32 * 1000 ? "moonshot-v1-128k" : "moonshot-v1-32k";
|
var modelId = reqTokenCount > 32 * 1000 ? "moonshot-v1-128k" : "moonshot-v1-32k";
|
||||||
var chatRep = new ChatReq
|
var chatRep = new ChatReq
|
||||||
{
|
{
|
||||||
max_tokens = 32*1024 - (reqTokenCount + 20),
|
max_tokens = 32*1024 - (reqTokenCount + 30),
|
||||||
temperature = 0.3f,
|
temperature = 0.3f,
|
||||||
frequency_penalty = 0,
|
frequency_penalty = 0,
|
||||||
presence_penalty = 0,
|
presence_penalty = 0,
|
||||||
|
|
@ -100,14 +99,13 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
RedisExpand.SetTaskGPTCached(task, chatResp);
|
RedisExpand.SetTaskGPTCached(task, chatResp);
|
||||||
if (chatResp is null || chatResp.error != null)
|
if (chatResp is null || chatResp.error != null)
|
||||||
throw new Exception($"KIMI模型返回异常 Chat 返回参数: " +
|
throw new Exception($"KIMI模型返回异常 Chat 返回参数: " +
|
||||||
$" {System.Text.Json.JsonSerializer.Serialize(chatResp)}");
|
$" {JsonSerializer.Serialize(chatResp)}");
|
||||||
var chatResContent = chatResp?.choices.FirstOrDefault()?.message.content;
|
var chatResContent = chatResp?.choices.FirstOrDefault()?.message.content;
|
||||||
|
|
||||||
|
|
||||||
if (chatResContent is null)
|
if (chatResContent is null)
|
||||||
throw new Exception("KIMIGPT返回message无效结果");
|
throw new Exception("KIMIGPT返回message无效结果");
|
||||||
|
var questionRes = JsonSerializer.Deserialize<QuestionRes[]>(chatResContent);
|
||||||
var questionRes = JsonConvert.DeserializeObject<QuestionRes[]>(chatResContent);
|
|
||||||
var gptRes = new TaskRes(captions);
|
var gptRes = new TaskRes(captions);
|
||||||
if (questionRes is null)
|
if (questionRes is null)
|
||||||
throw new Exception("KIMIGPT返回无效结果");
|
throw new Exception("KIMIGPT返回无效结果");
|
||||||
|
|
@ -119,20 +117,20 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
var criteriaDic = criteriaArr.ToDictionary(s => s.Id);
|
var criteriaDic = criteriaArr.ToDictionary(s => s.Id);
|
||||||
gptRes.Assessment = new AssessmentDto()
|
gptRes.Assessment = new AssessmentDto()
|
||||||
{
|
{
|
||||||
Bad = arr1.Where(s => s.结果.ToObject<int>() < 6)
|
Bad = arr1.Where(s => s.ToObject<int>() < 6)
|
||||||
.Select(s => new CourseCriteria()
|
.Select(s => new CourseCriteria()
|
||||||
{
|
{
|
||||||
Id = criteriaDic[s.问题编号].Id,
|
Id = criteriaDic[s.问题编号].Id,
|
||||||
ImprovedMethods = criteriaDic[s.问题编号].ImprovedMethods,
|
ImprovedMethods = criteriaDic[s.问题编号].ImprovedMethods,
|
||||||
Analyze = s.问题解释 ?? string.Empty,
|
Analyze = s.问题解释 ?? string.Empty,
|
||||||
Score = s.结果.ToObject<int>(),
|
Score = s.ToObject<int>(),
|
||||||
Prompt = criteriaDic[s.问题编号].Flaw,
|
Prompt = criteriaDic[s.问题编号].Flaw,
|
||||||
}).ToArray(),
|
}).ToArray(),
|
||||||
Merit = arr1.Where(s => s.结果.ToObject<int>() >= 6)
|
Merit = arr1.Where(s => s.ToObject<int>() >= 6)
|
||||||
.Select(s => new CourseCriteria()
|
.Select(s => new CourseCriteria()
|
||||||
{
|
{
|
||||||
Id = criteriaDic[s.问题编号].Id,
|
Id = criteriaDic[s.问题编号].Id,
|
||||||
Score = s.结果.ToObject<int>(),
|
Score = s.ToObject<int>(),
|
||||||
//ImprovedMethods = criteriaDic[s.问题编号].ImprovedMethods,
|
//ImprovedMethods = criteriaDic[s.问题编号].ImprovedMethods,
|
||||||
Analyze = s.问题解释 ?? string.Empty,
|
Analyze = s.问题解释 ?? string.Empty,
|
||||||
Prompt = criteriaDic[s.问题编号].Advantage,
|
Prompt = criteriaDic[s.问题编号].Advantage,
|
||||||
|
|
@ -140,18 +138,18 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
};
|
};
|
||||||
|
|
||||||
//高频词汇
|
//高频词汇
|
||||||
gptRes.Hotwords = arr2[(int)QuestionTypeEnum.高频词].结果.ToObject<string[]>() ?? ["暂无数据"];
|
gptRes.Hotwords = arr2[(int)QuestionTypeEnum.高频词].ToObject<string[]>() ?? ["暂无数据"];
|
||||||
|
|
||||||
//时间段概览
|
//时间段概览
|
||||||
gptRes.TimeOverview = arr2[(int)QuestionTypeEnum.时间段概览]
|
gptRes.TimeOverview = arr2[(int)QuestionTypeEnum.时间段概览]
|
||||||
.结果.ToObject<TimeBase[]>();
|
.ToObject<TimeBase[]>();
|
||||||
//提问类型
|
//提问类型
|
||||||
gptRes.QuestionType = arr2[(int)QuestionTypeEnum.提问类型]
|
gptRes.QuestionType = arr2[(int)QuestionTypeEnum.提问类型]
|
||||||
.结果.ToObject<Dictionary<TeacherAnswerTypeEnum, int>>();
|
.ToObject<Dictionary<TeacherAnswerTypeEnum, int>>();
|
||||||
|
|
||||||
//分析上课时间段情况 分析 独立学习 小组合作 随堂练习等情况
|
//分析上课时间段情况 分析 独立学习 小组合作 随堂练习等情况
|
||||||
var extraTimeBase = arr2[(int)QuestionTypeEnum.额外课堂情况]
|
var extraTimeBase = arr2[(int)QuestionTypeEnum.额外课堂情况]
|
||||||
.结果.ToObject<TimeBase[]>();
|
.ToObject<TimeBase[]>();
|
||||||
if (extraTimeBase is not null)
|
if (extraTimeBase is not null)
|
||||||
foreach (var item in extraTimeBase)
|
foreach (var item in extraTimeBase)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
/// <returns>Return HttpResponseMessage for SSE</returns>
|
/// <returns>Return HttpResponseMessage for SSE</returns>
|
||||||
public async Task<ChatRes?> Chat(ChatReq chatReq)
|
public async Task<ChatRes?> Chat(ChatReq chatReq)
|
||||||
{
|
{
|
||||||
var requestBody = Newtonsoft.Json.JsonConvert.SerializeObject(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);
|
||||||
return await chatResp.Content.ReadFromJsonAsync<ChatRes>();
|
return await chatResp.Content.ReadFromJsonAsync<ChatRes>();
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +84,7 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
var responseObj = JToken.Parse(responseText);
|
var responseObj = JToken.Parse(responseText);
|
||||||
return responseObj?["data"]?["total_tokens"]?.ToObject<int>();
|
return responseObj?["data"]?["total_tokens"]?.ToObject<int>();
|
||||||
}
|
}
|
||||||
var error = Newtonsoft.Json.JsonConvert.DeserializeObject<ErrorResponse>(responseText);
|
var error = JsonConvert.DeserializeObject<ErrorResponse>(responseText);
|
||||||
_logger.LogError($"{error?.error?.type}: {error?.error?.message}");
|
_logger.LogError($"{error?.error?.type}: {error?.error?.message}");
|
||||||
throw new Exception($"{error?.error.type}: {error?.error.message}");
|
throw new Exception($"{error?.error.type}: {error?.error.message}");
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +97,7 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<int?> GetAsTiMateTokenCount(ChatReq chatReq)
|
public async Task<int?> GetAsTiMateTokenCount(ChatReq chatReq)
|
||||||
{
|
{
|
||||||
var chatReqText = Newtonsoft.Json.JsonConvert.SerializeObject(chatReq);
|
var chatReqText = JsonConvert.SerializeObject(chatReq);
|
||||||
return await GetAsTiMateTokenCount(chatReqText);
|
return await GetAsTiMateTokenCount(chatReqText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -226,9 +226,9 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
|
||||||
var responseText = await response.Content.ReadAsStringAsync();
|
var responseText = await response.Content.ReadAsStringAsync();
|
||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseText) ?? default;
|
return JsonConvert.DeserializeObject<T>(responseText) ?? default;
|
||||||
}
|
}
|
||||||
var error = Newtonsoft.Json.JsonConvert.DeserializeObject<ErrorResponse>(responseText);
|
var error = JsonConvert.DeserializeObject<ErrorResponse>(responseText);
|
||||||
_logger.LogError($"{error?.error.type}: {error?.error.message}");
|
_logger.LogError($"{error?.error.type}: {error?.error.message}");
|
||||||
throw new Exception($"{error?.error.type}: {error?.error.message}");
|
throw new Exception($"{error?.error.type}: {error?.error.message}");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace VideoAnalysisCore.Enum
|
namespace VideoAnalysisCore.Enum
|
||||||
{
|
{
|
||||||
enum QuestionTypeEnum
|
public enum QuestionTypeEnum
|
||||||
{
|
{
|
||||||
|
|
||||||
[Display(Prompt = "分析授课中使用的高频词" +
|
[Display(Prompt = "分析授课中使用的高频词" +
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue