using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Contracts; using UnityEngine; public class LLM:MonoBehaviour { /// /// api地址 /// [SerializeField] protected string url; /// /// 提示词,与消息一起发送 /// [Header("发送的提示词设定")] [SerializeField] protected string m_Prompt = string.Empty; /// /// 语言 /// /// 上下文保留条数 /// [Header("上下文保留条数")] [SerializeField] protected int m_HistoryKeepCount = 15; /// /// 缓存对话 /// [SerializeField] public List m_DataList = new List(); /// /// 计算方法调用的时间 /// [SerializeField] protected Stopwatch stopwatch=new Stopwatch(); /// /// 发送消息 /// public virtual void PostMsg(string _msg,Action _callback) { //上下文条数设置 CheckHistory(); //提示词处理 string message = m_Prompt + " The language of reply is " + lan + " here's my question:" + _msg; UnityEngine.Debug.Log("玩家post:" + message); //缓存发送的信息列表 m_DataList.Add(new SendData("user", message)); StartCoroutine(Request(message, _callback)); } public virtual IEnumerator Request(string _postWord, System.Action _callback) { yield return new WaitForEndOfFrame(); } /// /// 设置保留的上下文条数,防止太长 /// public virtual void CheckHistory() { if(m_DataList.Count> m_HistoryKeepCount) { m_DataList.RemoveAt(0); } } [Serializable] public class SendData { [SerializeField] public string role; [SerializeField] public string content; public SendData() { } public SendData(string _role, string _content) { role = _role; content = _content; } } }