60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
|
|
using FastReport.Utils;
|
|
using Newtonsoft.Json;
|
|
using NPOI.POIFS.Crypt;
|
|
using OpenAI;
|
|
using OpenAI.Managers;
|
|
using OpenAI.ObjectModels.RequestModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BLL.Common
|
|
{
|
|
public class OpenAIhelper
|
|
{
|
|
private const string ApiKey = "sk-ee51958b5a4f4136b01ad4d866e1ba94";
|
|
private const string AIURL = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
|
|
|
|
public static string ChatCompletion(string content)
|
|
{
|
|
try
|
|
{
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + ApiKey);
|
|
|
|
var requestData = new
|
|
{
|
|
model = "qwen3.6-plus",
|
|
messages = new[] {
|
|
new { role = "user", content = content }
|
|
},
|
|
temperature = 0.7
|
|
};
|
|
|
|
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();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|