1.申请key,由于目前网站正在维护升级,所以暂时申请不到,下课(bushi)右上角点击API开放平台DeepSeek,并且获取key(服务器升级维护中,暂时无法申请)
2.创建简单(jingmei)的UI,制作玩家输入和发送按钮以及展示输出的Text
3.编写UI相关代码,点击按钮后把数据传递到GetDeepSeek.SendMessageToDeepSeek函数中
4.编写GetDeepSeek相关代码,其中key自己申请,url为所示https://api.deepseek.com/v1/chat/completions
5.确认信息无误并点击发送按钮后,进入协程(接下来涉及到了一些概念)
创建请求体,序列化请求体,创建网络请求,上传处理器,下载处理器,请求头信息——简单来说,上一步就是,层层打包数据,然后发送到apiUrl,请求方式为POST,请求头就是向服务器发送请求时指定一些必要的参数,上传处理器和请求头的参数要求一般会在对应api文档里会有写。
6.发送请求并yield return 直至请求完成后才执行下一步
7.检查下载处理器获取的参数是否有错误(404,500等),debug一下text并层层解刨(见最后一张图)
8.根据debug信息定义相应数据结构
9.提取要输出的回复,转到精美的ui上
补充:可通过text查看得到的数据
这样就可以简单的实现了,如果想实现虚拟角色对话或者其他功能的话,可以在发送数据时额外携带一些限定参数,比如在发送的内容前加上“你是一个可爱的猫娘,现在我想对你说”+data+“请回复我时直接告诉我你的答案,不要出现ai等字眼”(滑稽)
附带源码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AboutUI : MonoBehaviour
{
public Text playerInput;
public GetDeepSeek getDeepSeek;
private void Awake()
{
getDeepSeek = GetComponent<GetDeepSeek>();
}
public void OnButtonClick()
{
if (string.IsNullOrWhiteSpace(playerInput.text))
{
return;
}
string data = playerInput.text;
getDeepSeek.SendMessageToDeepSeek(data);
}
}
using System.Collections;
using UnityEngine;
using Newtonsoft.Json;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GetDeepSeek : MonoBehaviour
{
private string apiKey = "请自己申请";
private string apiUrl = "https://api.deepseek.com/v1/chat/completions";
public Text text;
public void SendMessageToDeepSeek(string data)
{
StartCoroutine(PostRequest(data));
}
IEnumerator PostRequest(string message)
{
//创建请求体
var requestBody = new
{
model = "deepseek-chat",
messages = new[]
{
new { role = "user", content = message }
}
};
//序列化请求体
string jsonBody = JsonConvert.SerializeObject(requestBody);
//创建网络请求
UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonBody);
//上传处理器
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
//下载处理器
request.downloadHandler = new DownloadHandlerBuffer();
//请求头信息
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer " + apiKey);
//发送请求
Debug.Log("发送");
yield return request.SendWebRequest();
Debug.Log("响应");
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("Error: " + request.error);
Debug.LogError("Response: " + request.downloadHandler.text); // 打印详细错误信息
}
else
{
// 处理响应
string responseJson = request.downloadHandler.text;
Debug.Log("Response: " + responseJson);
// 解析响应
var response = JsonConvert.DeserializeObject<DeepSeekResponse>(responseJson);
if (response != null && response.choices.Length > 0)
{
string reply = response.choices[0].message.content;
Debug.Log("DeepSeek says: " + reply);
text.text = reply;
}
else
{
Debug.LogError("Failed to parse response.");
}
}
request.Dispose();
}
// 定义响应数据结构
private class DeepSeekResponse
{
public Choice[] choices;
}
private class Choice
{
public Message message;
}
private class Message
{
public string content;
}
}