158 lines
6.5 KiB
C#
158 lines
6.5 KiB
C#
using VideoAnalysisCore.Common;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Net.Http;
|
|
using Newtonsoft.Json;
|
|
using System.Net.Http.Json;
|
|
using AntDesign;
|
|
using OneOf.Types;
|
|
using System.Net;
|
|
using VideoAnalysisCore.AICore.GPT.KIMI;
|
|
using System.Threading;
|
|
using System;
|
|
|
|
namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|
{
|
|
|
|
public class DeepSeekGPTClient
|
|
{
|
|
public static string Host = AppCommon.Config.ChatGpt.DeepSeek.Host;
|
|
public static string ApiKey = AppCommon.Config.ChatGpt.DeepSeek.ApiKey;
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
public DeepSeekGPTClient(IHttpClientFactory httpClientFactory)
|
|
{
|
|
_httpClientFactory = httpClientFactory;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Chat
|
|
/// </summary>
|
|
/// <param name="chatReq"></param>
|
|
/// <returns>Return HttpResponseMessage for SSE</returns>
|
|
public async Task<(Usage u, string res,string reasoning)?> Chat(ChatRequest chatReq)
|
|
{
|
|
if (chatReq.stream) return await ChatSSE(chatReq);
|
|
var requestBody = System.Text.Json.JsonSerializer.Serialize(chatReq);
|
|
var chatResp = await PostJsonStreamAsync(string.Empty, requestBody);
|
|
var res1 = await chatResp.Content.ReadAsStringAsync();
|
|
if (res1 is null || string.IsNullOrEmpty(res1))
|
|
throw new Exception($" GPT模型返回空内容 返回参数: " +
|
|
$" {res1}");
|
|
var res = await chatResp.Content.ReadFromJsonAsync<ChatRes>();
|
|
if (res is null || res.error != null)
|
|
throw new Exception($" GPT模型返回异常 返回参数: " +
|
|
$" {System.Text.Json.JsonSerializer.Serialize(res)}");
|
|
var chatResContent = res?.choices.FirstOrDefault()?.message.content.Trim();
|
|
var chatResReasoning = res?.choices.FirstOrDefault()?.message.reasoning_content?.Trim();
|
|
|
|
if (string.IsNullOrEmpty(chatResContent))
|
|
return null;
|
|
return (res.usage, chatResContent, chatResReasoning);
|
|
}
|
|
|
|
|
|
private async Task<HttpResponseMessage> PostJsonStreamAsync(string path, string json)
|
|
{
|
|
var uriBuilder = new UriBuilder(Host + path);
|
|
var maxRestart = 4;
|
|
var errorMSG = new Exception[maxRestart];
|
|
for (int i = 0; i < maxRestart; i++)
|
|
{
|
|
try
|
|
{
|
|
var client = _httpClientFactory.CreateClient();
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", ApiKey);
|
|
client.Timeout = TimeSpan.FromSeconds(60 * 20);//超时时间20分钟
|
|
client.DefaultRequestVersion = HttpVersion.Version20;
|
|
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
|
|
client.DefaultRequestHeaders.ConnectionClose = true;
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri);
|
|
request.Content = new StringContent(json, Encoding.UTF8, "application/json"); ;
|
|
return await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
errorMSG[i] = e;
|
|
Console.WriteLine("====================[请求异常,重试]====================");
|
|
Console.WriteLine(uriBuilder.Uri);
|
|
Console.WriteLine(e.Message);
|
|
Console.WriteLine(e.StackTrace);
|
|
Console.WriteLine("==============================================");
|
|
|
|
}
|
|
Thread.Sleep(1000);
|
|
}
|
|
throw errorMSG.Last(s => s != null);
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// ChatSSE[流式传输 更稳定]
|
|
/// </summary>
|
|
/// <param name="chatReq"></param>
|
|
/// <returns>Return HttpResponseMessage for SSE</returns>
|
|
public async Task<(Usage u, string res, string reasoning)?> ChatSSE(ChatRequest chatReq)
|
|
{
|
|
chatReq.stream = true;
|
|
var requestBody = System.Text.Json.JsonSerializer.Serialize(chatReq);
|
|
var chatResp = await PostJsonStreamAsync(string.Empty, requestBody);
|
|
using var stream = await chatResp.Content.ReadAsStreamAsync();
|
|
using var reader = new StreamReader(stream, Encoding.UTF8);
|
|
string line;
|
|
var messageBuilder = new StringBuilder();
|
|
var messageBuilder1 = new StringBuilder();
|
|
var lastChat = new ChatResSSE();
|
|
var splitCount = "data:".Length;
|
|
while (true)
|
|
{
|
|
line = await reader.ReadLineAsync();
|
|
if (line is null || string.IsNullOrEmpty(line))
|
|
throw new Exception("AI返回无效内容 =>Null/Empty");
|
|
else if (line.EndsWith("[DONE]"))
|
|
{
|
|
// 表示一条消息结束
|
|
string message = messageBuilder.ToString();
|
|
string message2 = messageBuilder.ToString();
|
|
messageBuilder.Clear();
|
|
var u = lastChat?.usage;
|
|
if (u == null || string.IsNullOrEmpty(message))
|
|
return null;
|
|
return (u, message, message2);
|
|
}
|
|
else if (line.StartsWith("data:"))
|
|
{
|
|
try
|
|
{
|
|
var data = System.Text.Json.JsonSerializer.Deserialize<ChatResSSE>(line.Substring(splitCount).Trim());
|
|
lastChat = data;
|
|
var delta = data?.choices.FirstOrDefault()?.delta;
|
|
var str = delta?.content;
|
|
var strReasoning = delta?.reasoning_content;
|
|
if (!string.IsNullOrEmpty(str))
|
|
messageBuilder.Append(str);
|
|
if (!string.IsNullOrEmpty(strReasoning))
|
|
messageBuilder1.Append(strReasoning);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("异常 ChatSSE=>");
|
|
Console.WriteLine(line);
|
|
Console.WriteLine(e.Message);
|
|
Console.WriteLine(e.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
} |