127 lines
4.4 KiB
C#
Raw Normal View History

2025-09-24 19:24:19 +08:00
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<string> _callback)
{
base.PostMsg(_msg, _callback);
}
public override IEnumerator Request(string _content, Action<string> _callback)
{
StopMsg();
// <20><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
coroutineRunning = true;
currentRequest = null;
//<2F><>ʼ<EFBFBD><CABC>ʱ
stopwatch.Restart();
//<2F><>װ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("{\"ģʽѡ<CABD><D1A1>\":\"" + mode + "\",");
//str.Append("\"<22><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>\":\"" + bingLi + "\"},");
//str.Append("{\"ģʽѡ<CABD><D1A1>\":\"" + mode + bingLi + "\"},");
str.Append("{\"ģʽѡ<CABD><D1A1>\":\"" + mode + "\"},");
str.Append("\"messages\": [{");
str.Append("\"role\":\"user\",");
str.Append("\"content\":\"" + _content + "\"");
str.Append("}]}");
Debug.Log(str.ToString().Replace("\n", "").Replace("\r", "").Replace(" ", ""));
//<2F><><EFBFBD><EFBFBD>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();
//<2F><><EFBFBD><EFBFBD>json
if (currentRequest.responseCode == 200 && coroutineRunning)
{
string _msg = currentRequest.downloadHandler.text;
2025-09-25 15:47:17 +08:00
Debug.Log(_msg);
2025-09-24 19:24:19 +08:00
JsonData responseBody = JsonMapper.ToObject(_msg);
Debug.Log(responseBody["choices"][0]["message"]["content"].ToString() + "\nAI-<2D><>ʱ" + stopwatch.Elapsed.TotalSeconds);
assistant = responseBody["choices"][0]["message"]["content"].ToString();
//<2F>ص<EFBFBD>
_callback?.Invoke(responseBody["choices"][0]["message"]["content"].ToString());
success = true;
}
else if (coroutineRunning)
{
retryCount++;
if (retryCount < maxRetries)
{
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD> " + (retryCount + 1) + " <20><>...");
yield return new WaitForSeconds(1f); // <20>ȴ<EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
CleanupCurrentRequest();
}
if (!success && coroutineRunning)
{
_callback?.Invoke("error");
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
stopwatch.Stop();
// <20><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD>ѽ<EFBFBD><D1BD><EFBFBD>
coroutineRunning = false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
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();
}
}