95 lines
3.5 KiB
C#
95 lines
3.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 System.Net;
|
|
|
|
|
|
namespace VideoAnalysisCore.AICore.GPT.ChatGPT
|
|
{
|
|
|
|
public class ChatGPTClient
|
|
{
|
|
public static string Host = AppCommon.Config.ChatGpt.ChatGpt.Host;
|
|
public static string ApiKey = AppCommon.Config.ChatGpt.ChatGpt.ApiKey;
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
public ChatGPTClient(IHttpClientFactory httpClientFactory)
|
|
{
|
|
_httpClientFactory = httpClientFactory;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ChatSSE[流式传输 更稳定]
|
|
/// </summary>
|
|
/// <param name="chatReq"></param>
|
|
/// <returns>Return HttpResponseMessage for SSE</returns>
|
|
public async Task<(Usage u, string res)?> ChatSSE(ChatRequest chatReq)
|
|
{
|
|
throw new Exception($"未实现");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Chat
|
|
/// </summary>
|
|
/// <param name="chatReq"></param>
|
|
/// <returns>Return HttpResponseMessage for SSE</returns>
|
|
public async Task<(Usage u, string res)?> Chat(ChatRequest chatReq)
|
|
{
|
|
var requestBody = chatReq.ToJson();
|
|
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 || res.error != null)
|
|
throw new Exception($" ChatGPT模型返回异常 返回参数: " +
|
|
$" {res?.ToJson()}");
|
|
|
|
if (string.IsNullOrEmpty(chatResContent))
|
|
return null;
|
|
return (res.usage, chatResContent);
|
|
}
|
|
|
|
|
|
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", AppCommon.Config.ChatGpt.ChatGpt.ApiKey);
|
|
client.Timeout = TimeSpan.FromSeconds(60 *20);//超时时间20分钟
|
|
client.DefaultRequestVersion = HttpVersion.Version20;
|
|
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
|
|
client.DefaultRequestHeaders.ConnectionClose = true;
|
|
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
return await client.PostAsync(uriBuilder.Uri, content);
|
|
}
|
|
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);
|
|
}
|
|
|
|
}
|
|
} |