using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class chatGLM : LLM { public chatGLM() { url = "http://localhost:8000"; } /// /// 历史对话 /// [SerializeField] private List> m_History = new List>(); /// /// 发送消息 /// /// public override void PostMsg(string _msg, Action _callback) { base.PostMsg(_msg, _callback); } /// /// 发送数据 /// /// /// /// public override IEnumerator Request(string _postWord, System.Action _callback) { stopwatch.Restart(); string jsonPayload = JsonConvert.SerializeObject(new RequestData { prompt = _postWord, history = m_History }); using (UnityWebRequest request = new UnityWebRequest(url, "POST")) { byte[] data = System.Text.Encoding.UTF8.GetBytes(jsonPayload); request.uploadHandler = (UploadHandler)new UploadHandlerRaw(data); request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return request.SendWebRequest(); if (request.responseCode == 200) { string _msg = request.downloadHandler.text; ResponseData response = JsonConvert.DeserializeObject(_msg); //记录历史对话 m_History = response.history; //添加记录 m_DataList.Add(new SendData("assistant", _msg)); //回调 _callback(response.response); } } stopwatch.Stop(); Debug.Log("chatGLM耗时:" + stopwatch.Elapsed.TotalSeconds); } #region 报文定义 [Serializable] private class RequestData { public string prompt; public List> history; } [Serializable] private class ResponseData { public string response; public List> history; public int status; public string time; } #endregion }