因为Deep Seek 官方API暂时不可使用,还必须使用云接口,找了阿里云的接口进行业务对接

先看结果
StartCoroutine(Request(“说一个新年成语”, Response));
结果
恭喜发财
在这里插入图片描述


using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class TestHandler : MonoBehaviour
{
 
    //deepseek-v3
    static string model = "deepseek-v3";
    private static string apiKey = "填自己的KEY";
    private static string apiUrl = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

    private void Awake()
    {
        StartCoroutine(Request("说一个新年成语", Response));
    }

    private void Response(string res)
    {
        Debug.Log(res);
    }


    public static IEnumerator Request(string _postWord,System.Action<string> callback)
    {
        UnityEngine.Debug.Log("输入问题:" + _postWord);

        using (UnityWebRequest request = new UnityWebRequest(apiUrl, "POST"))
        {
            // 将本地图片文件读取并编码为Base64格式
            //  byte[] imageBytes = File.ReadAllBytes(imagePath);
            //  string base64Image = Convert.ToBase64String(imageBytes);
            // string imageBase64Data = $"data:image/png;base64,{base64Image}";

            // 配置消息内容
            MessageData message0 = new MessageData
            {
                role = "user",
                content = new MessageContent[]
                {
                    new MessageContent { type = "text", text = _postWord },
                }
            };

            var requestBody = new
            {
                model = model,
                messages = new[] { message0 }
            };

            string jsonData = JsonConvert.SerializeObject(requestBody);
            UnityEngine.Debug.Log("Request JSON: " + jsonData);

            byte[] data = System.Text.Encoding.UTF8.GetBytes(jsonData);
            request.uploadHandler = new UploadHandlerRaw(data);
            request.downloadHandler = new DownloadHandlerBuffer();

            request.SetRequestHeader("Content-Type", "application/json");
            request.SetRequestHeader("Authorization", $"Bearer {apiKey}");

            yield return request.SendWebRequest();

            if (request.isNetworkError || request.isHttpError)
            {
                UnityEngine.Debug.LogError(request.error);
                callback.Invoke("Error");
            }
            else
            {
                UnityEngine.Debug.Log("Response: " + request.downloadHandler.text);


                var response = JsonConvert.DeserializeObject<DeepSeekResponse>(request.downloadHandler.text);
                string botMessage = response.choices[0].message.content;
               // res = botMessage;
                callback.Invoke(botMessage);
            }
        }
    }

   
}

[System.Serializable]
public class DeepSeekResponse
{
    public Choice[] choices;
}

[System.Serializable]
public class Choice
{
    public Message message;
}

[System.Serializable]
public class Message
{
    public string content;

}
public class MessageData
{
    public string role { get; set; }
    public MessageContent[] content { get; set; }
}

public class MessageContent
{
    public string type { get; set; }
    public string text { get; set; }
    public ImageUrl image_url { get; set; }
}

public class ImageUrl
{
    public string url { get; set; }
}
Logo

分享前沿Unity技术干货和开发经验,精彩的Unity活动和社区相关信息

更多推荐