SGGL_SHJ/SGGL/BLL/Common/OpenAIhelper.cs

57 lines
1.7 KiB
C#

using FastReport.Utils;
using Newtonsoft.Json;
using NPOI.POIFS.Crypt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace BLL.Common
{
public class OpenAIhelper
{
private const string ApiKey = "sk-a5014a22a49049d1b2179a6c0d8eec1c";
private const string AIURL = "https://api.deepseek.com/chat/completions";
public static string ChatCompletion(string content)
{
try
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + ApiKey);
var requestData = new
{
model = "deepseek-v4-flash",
messages = new[] {
new { role = "user", content = content }
},
temperature = 0.3
};
string json = JsonConvert.SerializeObject(requestData);
var chatContent = new StringContent(json, Encoding.UTF8, "application/json");
// 同步请求
var response = client.PostAsync(AIURL, chatContent).Result;
string resultJson = response.Content.ReadAsStringAsync().Result;
// 解析返回
dynamic result = JsonConvert.DeserializeObject(resultJson);
return result.choices[0].message.content.ToString();
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
}
}