package com.ch.jedge.jbot2.context; import com.alibaba.fastjson.JSONArray; import com.ch.jedge.jbot2.JedgeBotGptService; import com.ch.jedge.jbot2.llm.JedgeLLMContext; import com.ch.jedge.jbot2.llm.world.JedgeLLMDmWorld; import com.ch.jedge.jbot2.llm.world.knl.JedgeLLMKnWorld; import com.ch.jedge.jbot2.llm.world.task.JedgeLLMTaskPool; import com.changhong.qlib.util.Log.ISLog; import static com.ch.jedge.utils.JedgeBotConst.max_history_size; import static com.ch.jedge.utils.JedgeBotConst.max_history_total_size; /** * 聊天的会话,需要记录 领域、话题 等约束 * 需要就如下内容进行缓存。 * * - 知识: * - 词库: * - 意图 * - 活跃关键字: (知识点) * * 在对话过程中,系统需要根据聊天的内容,手动或动态切换领域和话题等。 * 领域将更新其后台知识库, 每次命中,可切换其关键字池、 * · 加载默认的知识条目、加载其知识条目下的关键字,引导查询多条知识条目在当前对话缓存中 * · */ public class JedgeTalkSession { public boolean isStream = true; public long sid = 0; public String currentPrompt = ""; public String currentResponse = ""; public boolean knTalk; public boolean voiceTalk; public String domain; public int rn = 6; public String currentKnBase; public boolean localGlm = true; public String srcMod; public String cbUri; public boolean printSingle = true; public long ttl; public final JSONArray history_ = new JSONArray(); private final JedgeBotGptService mBotService; private final JedgeLLMTaskPool taskPool; public JedgeTalkSession(JedgeBotGptService service, JedgeLLMContext llmCtx) { mBotService = service; //世界任务池 JedgeLLMKnWorld KnWorld = service.getLLMContext().getKnWorld(); JedgeLLMDmWorld worldModel = service.getLLMContext().getDmWorld(); taskPool = new JedgeLLMTaskPool(llmCtx, KnWorld, worldModel); //启动会话任务池 taskPool.startTaskWatching(); } public void clearHistory() { history_.clear(); } public void append2LastQA(String returnText) { ttl = 0; // mGdeApp.getMgModule().infoLog(String.format(">>%s", returnText)); currentResponse += (returnText); // mGdeApp.getMgModule().printClientLog(String.format(">>%s", currentResponse)); } public void buildLastHistory() { JSONArray qa = new JSONArray(); qa.add(currentPrompt); qa.add(currentResponse); currentResponse = ""; currentPrompt = ""; history_.add(qa); if(history_.size()>max_history_size) { history_.remove(0); //移除第0个聊天历史 } String log; log = history_.toJSONString(); while (log.length()>max_history_total_size) { //指令缓存最大256个 history_.remove(0); //移除第0个聊天历史 log = (history_.toJSONString()); } ISLog.fastLog(log); ttl = 0; } public JedgeBotGptService getGptBotService() { return mBotService; } }