using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Fort23.Core;
using UnityEngine;
public class DeepSeekAI
{
///
/// DeepSeek APi 访问地址
///
private const string BASE_PATH = "https://api.deepseek.com/chat/completions";
///
/// DeepSeek配置
///
private Configuration configuration;
///
/// 设置API KEY
///
///
///
public DeepSeekAI(string apiKey)
{
if (string.IsNullOrEmpty(apiKey))
{
throw new ArgumentException("api key is null",nameof(apiKey));
}
configuration=new Configuration(apiKey);
}
///
/// 发送对话结束消息内容到DeepSeek
///
public async CTask SendChatCompletionToDeepSeek(ChatCompletionRequest requestMessage)
{
//把消息对象序列成Json字符串
string jsonMessage = JsonConvert.SerializeObject(requestMessage);
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, BASE_PATH);
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", $"Bearer {configuration.ApiKey}");
var content = new StringContent(jsonMessage, null, "application/json");
Debug.Log("DeepSeek SendRequest:" + jsonMessage);
request.Content = content;
//发送API请求
var response = await client.SendAsync(request);
//验证响应码是否是200 如果是200则说明接口请求成功
response.EnsureSuccessStatusCode();
//读取API响应内容
string reslutJson = await response.Content.ReadAsStringAsync();
Debug.Log("DeepSeek Response:" + reslutJson);
return JsonConvert.DeserializeObject(reslutJson);
}
}