12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using Newtonsoft.Json;
- using System;
- using System.Net.Http;
- using System.Threading.Tasks;
- using Fort23.Core;
- using UnityEngine;
- public class DeepSeekAI
- {
- /// <summary>
- /// DeepSeek APi 访问地址
- /// </summary>
- private const string BASE_PATH = "https://api.deepseek.com/chat/completions";
- /// <summary>
- /// DeepSeek配置
- /// </summary>
- private Configuration configuration;
- /// <summary>
- /// 设置API KEY
- /// </summary>
- /// <param name="apiKey"></param>
- /// <exception cref="ArgumentException"></exception>
- public DeepSeekAI(string apiKey)
- {
- if (string.IsNullOrEmpty(apiKey))
- {
- throw new ArgumentException("api key is null",nameof(apiKey));
- }
- configuration=new Configuration(apiKey);
- }
- /// <summary>
- /// 发送对话结束消息内容到DeepSeek
- /// </summary>
- public async CTask<ChatCompletionResponse> 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<ChatCompletionResponse>(reslutJson);
- }
- }
|