using LitJson; using System; using System.Collections; using System.Text; using UnityEngine; using UnityEngine.Networking; using DongWuYiXue.Main; public class ChatZxkAI : LLM { //public string userName; public string Authorization; public string mode; public string bingLi; //public string bingLiMode; [HideInInspector] public string tipWords; public int maxRetries = 3; string assistant; private UnityWebRequest currentRequest; private bool coroutineRunning = false; string chatId; private void Awake() { chatId = Guid.NewGuid().ToString(); } public override void PostMsg(string _msg, Action _callback) { base.PostMsg(_msg, _callback); } public override IEnumerator Request(string _content, Action _callback) { StopMsg(); // 标记协程正在运行 coroutineRunning = true; currentRequest = null; //开始计时 stopwatch.Restart(); //组装json StringBuilder str = new(); int retryCount = 0; bool success = false; str.Append("{"); str.Append("\"chatId\": \"" + chatId + "\","); //str.Append("\"chatId\": \"asd\", "); str.Append("\"stream\": false,"); str.Append("\"detail\": false,"); //str.Append("\"responseChatItemId\":\""+ chatId + "\","); str.Append("\"variables\":"); //str.Append("{\"uid\":\"" + Guid.NewGuid() + "\","); //str.Append("{\"uid\": \"hhhh\", "); //str.Append("\"name\":\"" + "root" + "\","); //str.Append("{\"模式选择\":\"" + mode + "\","); //str.Append("\"病例选择\":\"" + bingLi + "\"},"); //str.Append("{\"模式选择\":\"" + mode + bingLi + "\"},"); str.Append("{\"模式选择\":\"" + mode + "\"},"); str.Append("\"messages\": [{"); str.Append("\"role\":\"user\","); str.Append("\"content\":\"" + _content + "\""); str.Append("}]}"); Debug.Log(str.ToString().Replace("\n", "").Replace("\r", "").Replace(" ", "")); //发送json while (retryCount < maxRetries && !success) { currentRequest = new(url, "POST"); currentRequest.timeout = 15; currentRequest.SetRequestHeader("Authorization", Authorization); currentRequest.SetRequestHeader("Content-Type", "application/json"); byte[] data = Encoding.UTF8.GetBytes(str.ToString().Replace("\n", "").Replace("\r", "").Replace(" ", "")); currentRequest.uploadHandler = new UploadHandlerRaw(data); currentRequest.downloadHandler = new DownloadHandlerBuffer(); yield return currentRequest.SendWebRequest(); //接收json if (currentRequest.responseCode == 200 && coroutineRunning) { string _msg = currentRequest.downloadHandler.text; Debug.Log(_msg); JsonData responseBody = JsonMapper.ToObject(_msg); Debug.Log(responseBody["choices"][0]["message"]["content"].ToString() + "\nAI-耗时" + stopwatch.Elapsed.TotalSeconds); assistant = responseBody["choices"][0]["message"]["content"].ToString(); //回调 _callback?.Invoke(responseBody["choices"][0]["message"]["content"].ToString()); success = true; } else if (coroutineRunning) { retryCount++; if (retryCount < maxRetries) { Debug.Log("正在重试,第 " + (retryCount + 1) + " 次..."); yield return new WaitForSeconds(1f); // 等待 1 秒后重试 } } // 清理当前请求 CleanupCurrentRequest(); } if (!success && coroutineRunning) { _callback?.Invoke("error"); } //结束计时 stopwatch.Stop(); // 标记协程已结束 coroutineRunning = false; } // 清理当前请求的方法 private void CleanupCurrentRequest() { if (currentRequest != null) { Debug.Log("CleanupCurrentRequest"); currentRequest.uploadHandler?.Dispose(); currentRequest.downloadHandler?.Dispose(); currentRequest.Dispose(); currentRequest = null; } } public override void StopMsg() { coroutineRunning = false; CleanupCurrentRequest(); base.StopMsg(); } }