| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 | 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";    private const string HUOSHAN_BASE_PATH = "https://ark.cn-beijing.volces.com/api/v3/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, HUOSHAN_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);    }}
 |