JedgeTalkSession.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.ch.jedge.jbot2.context;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.ch.jedge.jbot2.JedgeBotGptService;
  4. import com.ch.jedge.jbot2.llm.JedgeLLMContext;
  5. import com.ch.jedge.jbot2.llm.world.JedgeLLMDmWorld;
  6. import com.ch.jedge.jbot2.llm.world.knl.JedgeLLMKnWorld;
  7. import com.ch.jedge.jbot2.llm.world.task.JedgeLLMTaskPool;
  8. import com.changhong.qlib.util.Log.ISLog;
  9. import static com.ch.jedge.utils.JedgeBotConst.max_history_size;
  10. import static com.ch.jedge.utils.JedgeBotConst.max_history_total_size;
  11. /**
  12. * 聊天的会话,需要记录 领域、话题 等约束
  13. * 需要就如下内容进行缓存。
  14. *
  15. * - 知识:
  16. * - 词库:
  17. * - 意图
  18. * - 活跃关键字: (知识点)
  19. *
  20. * 在对话过程中,系统需要根据聊天的内容,手动或动态切换领域和话题等。
  21. * 领域将更新其后台知识库, 每次命中,可切换其关键字池、
  22. * · 加载默认的知识条目、加载其知识条目下的关键字,引导查询多条知识条目在当前对话缓存中
  23. * ·
  24. */
  25. public class JedgeTalkSession {
  26. public boolean isStream = true;
  27. public long sid = 0;
  28. public String currentPrompt = "";
  29. public String currentResponse = "";
  30. public boolean knTalk;
  31. public boolean voiceTalk;
  32. public String domain;
  33. public int rn = 6;
  34. public String currentKnBase;
  35. public boolean localGlm = true;
  36. public String srcMod;
  37. public String cbUri;
  38. public boolean printSingle = true;
  39. public long ttl;
  40. public final JSONArray history_ = new JSONArray();
  41. private final JedgeBotGptService mBotService;
  42. private final JedgeLLMTaskPool taskPool;
  43. public JedgeTalkSession(JedgeBotGptService service, JedgeLLMContext llmCtx) {
  44. mBotService = service;
  45. //世界任务池
  46. JedgeLLMKnWorld KnWorld = service.getLLMContext().getKnWorld();
  47. JedgeLLMDmWorld worldModel = service.getLLMContext().getDmWorld();
  48. taskPool = new JedgeLLMTaskPool(llmCtx, KnWorld, worldModel);
  49. //启动会话任务池
  50. taskPool.startTaskWatching();
  51. }
  52. public void clearHistory() {
  53. history_.clear();
  54. }
  55. public void append2LastQA(String returnText) {
  56. ttl = 0;
  57. // mGdeApp.getMgModule().infoLog(String.format(">>%s", returnText));
  58. currentResponse += (returnText);
  59. // mGdeApp.getMgModule().printClientLog(String.format(">>%s", currentResponse));
  60. }
  61. public void buildLastHistory() {
  62. JSONArray qa = new JSONArray();
  63. qa.add(currentPrompt);
  64. qa.add(currentResponse);
  65. currentResponse = "";
  66. currentPrompt = "";
  67. history_.add(qa);
  68. if(history_.size()>max_history_size) {
  69. history_.remove(0); //移除第0个聊天历史
  70. }
  71. String log;
  72. log = history_.toJSONString();
  73. while (log.length()>max_history_total_size) { //指令缓存最大256个
  74. history_.remove(0); //移除第0个聊天历史
  75. log = (history_.toJSONString());
  76. }
  77. ISLog.fastLog(log);
  78. ttl = 0;
  79. }
  80. public JedgeBotGptService getGptBotService() {
  81. return mBotService;
  82. }
  83. }