|
@@ -2,15 +2,19 @@ package com.ch.jedge.jbot;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
-import com.ch.jedge.jbot.corpus.CorpusTask;
|
|
|
|
-import com.ch.jedge.jbot.dict.JEDictUtil;
|
|
|
|
-import com.ch.jedge.jbot.dict.JedgeWordDict;
|
|
|
|
-import com.ch.jedge.jbot.intent.JedgeIntentTester;
|
|
|
|
-import com.ch.jedge.jbot.knbase.JedgeGptApp;
|
|
|
|
-import com.ch.jedge.jbot.knbase.JedgeGptDialogEngine;
|
|
|
|
-import com.ch.jedge.jbot.session.JedgeGdeKeywordPool;
|
|
|
|
|
|
+import com.ch.jedge.jbot.other.corpus.CorpusTask;
|
|
|
|
+import com.ch.jedge.jbot.other.knbase.JedgeGptApp;
|
|
|
|
+import com.ch.jedge.jbot.other.knbase.JedgeGptDialogEngine;
|
|
|
|
+import com.ch.jedge.jbot.other.session.JedgeGdeKeywordPool;
|
|
|
|
+import com.ch.jedge.jbot2.dict.JEDictUtil;
|
|
|
|
+import com.ch.jedge.jbot2.dict.JedgeWordDict;
|
|
import com.ch.jedge.jbot.session.JedgeServiceSession;
|
|
import com.ch.jedge.jbot.session.JedgeServiceSession;
|
|
-import com.changhong.jedge.*;
|
|
|
|
|
|
+import com.ch.jedge.utils.JedgeBotConst;
|
|
|
|
+import com.ch.jedge.utils.JedgeLlmUtil;
|
|
|
|
+import com.changhong.jedge.JMgbusModual;
|
|
|
|
+import com.changhong.jedge.JMgbusService;
|
|
|
|
+import com.changhong.jedge.JMgbusUtil;
|
|
|
|
+import com.changhong.jedge.MgbusApi;
|
|
import com.changhong.qlib.QData;
|
|
import com.changhong.qlib.QData;
|
|
import com.changhong.qlib.intf.QIData;
|
|
import com.changhong.qlib.intf.QIData;
|
|
import com.changhong.qlib.intf.QIDataList;
|
|
import com.changhong.qlib.intf.QIDataList;
|
|
@@ -24,8 +28,8 @@ import java.io.File;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
-import static com.ch.jedge.jbot.agent.JedgeGdeConsts.sval_default_app;
|
|
|
|
-import static com.ch.jedge.jbot.JEJBotLLMConst.*;
|
|
|
|
|
|
+import static com.ch.jedge.jbot.other.agent.JedgeGdeConsts.sval_default_app;
|
|
|
|
+import static com.ch.jedge.utils.JedgeBotConst.*;
|
|
|
|
|
|
public class JedgeBotGptService extends JMgbusService {
|
|
public class JedgeBotGptService extends JMgbusService {
|
|
|
|
|
|
@@ -42,7 +46,7 @@ public class JedgeBotGptService extends JMgbusService {
|
|
protected int vecSize = 1536;
|
|
protected int vecSize = 1536;
|
|
protected int chatMode = 2;
|
|
protected int chatMode = 2;
|
|
String knAsk_prompt;
|
|
String knAsk_prompt;
|
|
- private final JedgeIntentTester domainTester;
|
|
|
|
|
|
+ private final JedgeDomainTesterOld domainTester;
|
|
|
|
|
|
private String corpusPath = sval_corpus_cache_path_debug_;
|
|
private String corpusPath = sval_corpus_cache_path_debug_;
|
|
|
|
|
|
@@ -57,7 +61,7 @@ public class JedgeBotGptService extends JMgbusService {
|
|
public JedgeBotGptService(JMgbusModual holder) {
|
|
public JedgeBotGptService(JMgbusModual holder) {
|
|
super(holder);
|
|
super(holder);
|
|
|
|
|
|
- domainTester = new JedgeIntentTester(module, this);
|
|
|
|
|
|
+ domainTester = new JedgeDomainTesterOld(module, this);
|
|
|
|
|
|
auto_remove_call_info = false;
|
|
auto_remove_call_info = false;
|
|
String promptPath = FileUtils.contactPath("./", "cache", "prompt");
|
|
String promptPath = FileUtils.contactPath("./", "cache", "prompt");
|
|
@@ -480,10 +484,6 @@ public class JedgeBotGptService extends JMgbusService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- QIData msg = new QData();
|
|
|
|
- msg.putString("text", prompt);
|
|
|
|
- postServiceMessage("tvmufis", "/tvmfs/usermsg", msg);
|
|
|
|
-
|
|
|
|
JedgeServiceSession s = findSession(req, prompt);
|
|
JedgeServiceSession s = findSession(req, prompt);
|
|
|
|
|
|
if(!s.isStream) { //非流式请求,目前只针对chatGPT
|
|
if(!s.isStream) { //非流式请求,目前只针对chatGPT
|
|
@@ -491,19 +491,15 @@ public class JedgeBotGptService extends JMgbusService {
|
|
}
|
|
}
|
|
|
|
|
|
String finalPrompt = prompt;
|
|
String finalPrompt = prompt;
|
|
-// module.postThread(() -> {
|
|
|
|
-//// if(GdeTalk(s, finalPrompt)) {
|
|
|
|
-//// return;
|
|
|
|
-//// }
|
|
|
|
-////
|
|
|
|
|
|
+ module.postThread(() -> {
|
|
if(s.knTalk) {
|
|
if(s.knTalk) {
|
|
module.highLog(String.format("GPT Prompt>>%s", finalPrompt));
|
|
module.highLog(String.format("GPT Prompt>>%s", finalPrompt));
|
|
-// if (JedgeGptApp.isDebugMode())
|
|
|
|
-// returnMgbusMessage(s.srcMod, s.cbUri, "正在处理...\n", false);
|
|
|
|
//第一步、提取意图和知识关键字;
|
|
//第一步、提取意图和知识关键字;
|
|
QIData intent = checkWordsOfIntent(s, s.currentPrompt);
|
|
QIData intent = checkWordsOfIntent(s, s.currentPrompt);
|
|
knTalkLog(finalPrompt);
|
|
knTalkLog(finalPrompt);
|
|
if (intent != null) {
|
|
if (intent != null) {
|
|
|
|
+ //记录聊天历史,只记录发出的请求,不记录返回
|
|
|
|
+ s.buildLastHistory();
|
|
if (JMgbusUtil.isMgbusResultOk(intent)) {
|
|
if (JMgbusUtil.isMgbusResultOk(intent)) {
|
|
String returnPrompt = String.format("你是一个人工智能助手,用户输入:%s,请只根据系统的返回内容:%s,为用户输入生成一个直接的极简答复,不要生成其它内容。", finalPrompt, intent.getString("msg"));
|
|
String returnPrompt = String.format("你是一个人工智能助手,用户输入:%s,请只根据系统的返回内容:%s,为用户输入生成一个直接的极简答复,不要生成其它内容。", finalPrompt, intent.getString("msg"));
|
|
// makeRealGptStreamCall(s, returnPrompt);
|
|
// makeRealGptStreamCall(s, returnPrompt);
|
|
@@ -512,20 +508,21 @@ public class JedgeBotGptService extends JMgbusService {
|
|
} else if(intent.getInteger("code") == 601) {
|
|
} else if(intent.getInteger("code") == 601) {
|
|
if (JedgeGptApp.isDebugMode() && !s.voiceTalk)
|
|
if (JedgeGptApp.isDebugMode() && !s.voiceTalk)
|
|
returnMgbusMessage(s.srcMod, s.cbUri, "[模型直问]", true);
|
|
returnMgbusMessage(s.srcMod, s.cbUri, "[模型直问]", true);
|
|
- // makeRealGptStreamCall(s, finalPrompt);
|
|
|
|
|
|
+ makeRealGptStreamCall(s, finalPrompt);
|
|
} else
|
|
} else
|
|
returnMgbusMessage(s.srcMod, s.cbUri, "[动作失败]", true);
|
|
returnMgbusMessage(s.srcMod, s.cbUri, "[动作失败]", true);
|
|
} else {
|
|
} else {
|
|
if (JedgeGptApp.isDebugMode() && !s.voiceTalk)
|
|
if (JedgeGptApp.isDebugMode() && !s.voiceTalk)
|
|
returnMgbusMessage(s.srcMod, s.cbUri, "[模型直问]", true);
|
|
returnMgbusMessage(s.srcMod, s.cbUri, "[模型直问]", true);
|
|
- //makeRealGptStreamCall(s, finalPrompt);
|
|
|
|
|
|
+ makeRealGptStreamCall(s, finalPrompt);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
knTalkLog(finalPrompt);
|
|
knTalkLog(finalPrompt);
|
|
makeRealGptStreamCall(s, finalPrompt);
|
|
makeRealGptStreamCall(s, finalPrompt);
|
|
}
|
|
}
|
|
s.getGdtSession().refreshQARange(); //刷新聊天距离
|
|
s.getGdtSession().refreshQARange(); //刷新聊天距离
|
|
-// });
|
|
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
|
|
QIData resp = JedgeLlmUtil.simpleOk();
|
|
QIData resp = JedgeLlmUtil.simpleOk();
|
|
resp.put("sid", s.sid);
|
|
resp.put("sid", s.sid);
|
|
@@ -533,17 +530,19 @@ public class JedgeBotGptService extends JMgbusService {
|
|
}
|
|
}
|
|
|
|
|
|
private QIData checkWordsOfIntent(JedgeServiceSession s, String finalPrompt) {
|
|
private QIData checkWordsOfIntent(JedgeServiceSession s, String finalPrompt) {
|
|
- JedgeIntentTester dmTester = domainTester;
|
|
|
|
- String lastDomain = null;
|
|
|
|
- JedgeIntentTester lastDmTester = null;
|
|
|
|
|
|
+ JedgeDomainTesterOld dmTester = domainTester;
|
|
|
|
+ String lastDomain = s.getLastDomain();
|
|
|
|
+ JedgeDomainTesterOld lastDmTester = null;
|
|
QIData domainResult = null;
|
|
QIData domainResult = null;
|
|
int failCount = 0;
|
|
int failCount = 0;
|
|
while(dmTester!=null) {
|
|
while(dmTester!=null) {
|
|
- String newPrompt = domainTester.getIntentTestPrompt(finalPrompt);
|
|
|
|
|
|
+ String history = s.history_.toJSONString();
|
|
|
|
+ String newPrompt = domainTester.getIntentTestPrompt(finalPrompt, history);
|
|
domainResult = getJsonResultForPrompt(s, newPrompt);
|
|
domainResult = getJsonResultForPrompt(s, newPrompt);
|
|
lastDmTester = dmTester;
|
|
lastDmTester = dmTester;
|
|
if(domainResult!=null && domainResult.containsKey("domain")) { //子分类为其它时,直接在当前的domin
|
|
if(domainResult!=null && domainResult.containsKey("domain")) { //子分类为其它时,直接在当前的domin
|
|
String domain = domainResult.getString("domain");
|
|
String domain = domainResult.getString("domain");
|
|
|
|
+ s.addLastDomain(domain);
|
|
if (JedgeGptApp.isDebugMode() && !s.voiceTalk)
|
|
if (JedgeGptApp.isDebugMode() && !s.voiceTalk)
|
|
returnMgbusMessage(s.srcMod, s.cbUri, String.format("你想要:%s\n", domain), false);
|
|
returnMgbusMessage(s.srcMod, s.cbUri, String.format("你想要:%s\n", domain), false);
|
|
lastDomain = domain;
|
|
lastDomain = domain;
|
|
@@ -559,13 +558,13 @@ public class JedgeBotGptService extends JMgbusService {
|
|
}
|
|
}
|
|
|
|
|
|
if(StringUtils.isValidStr(lastDomain)) {
|
|
if(StringUtils.isValidStr(lastDomain)) {
|
|
- JedgeIntentTester.JedgeDomainDef tester = lastDmTester.getIntentTester(lastDomain);
|
|
|
|
|
|
+ JedgeDomainTesterOld.JedgeDomainDef tester = lastDmTester.getIntentTester(lastDomain);
|
|
//获得知识增强(通过domain中的Key)
|
|
//获得知识增强(通过domain中的Key)
|
|
//可通过额外的知识获得增强
|
|
//可通过额外的知识获得增强
|
|
//获取意图领域,并识别准确的意图
|
|
//获取意图领域,并识别准确的意图
|
|
if(tester!=null) {
|
|
if(tester!=null) {
|
|
// 获得子分类器,如果有则继续分类,如果没有,则调用intent生成器来完成生成。
|
|
// 获得子分类器,如果有则继续分类,如果没有,则调用intent生成器来完成生成。
|
|
- String newPrompt = tester.getIntentClassificationTemplate(finalPrompt);
|
|
|
|
|
|
+ String newPrompt = tester.getIntentClassificationTemplate(finalPrompt, "");
|
|
QIData finalResult = getJsonResultForPrompt(s, newPrompt); //提取参数
|
|
QIData finalResult = getJsonResultForPrompt(s, newPrompt); //提取参数
|
|
if(finalResult instanceof QIDataList) {
|
|
if(finalResult instanceof QIDataList) {
|
|
QIData p = null;
|
|
QIData p = null;
|
|
@@ -677,13 +676,13 @@ public class JedgeBotGptService extends JMgbusService {
|
|
if(session != null) {
|
|
if(session != null) {
|
|
if(session.printSingle) {
|
|
if(session.printSingle) {
|
|
module.infoLog(String.format("Gpt>>%s(%s)", text, isEnd));
|
|
module.infoLog(String.format("Gpt>>%s(%s)", text, isEnd));
|
|
-
|
|
|
|
if (isEnd) {
|
|
if (isEnd) {
|
|
// pullKnowledgeFromResponse(session, session.currentResponse);
|
|
// pullKnowledgeFromResponse(session, session.currentResponse);
|
|
session.buildLastHistory();
|
|
session.buildLastHistory();
|
|
} else {
|
|
} else {
|
|
//text = text.trim();
|
|
//text = text.trim();
|
|
- if(StringUtils.isValidStr(text))
|
|
|
|
|
|
+ if(StringUtils.isValidStr(text) &&
|
|
|
|
+ StringUtils.isValidStr(session.currentPrompt))
|
|
session.append2LastQA(text);
|
|
session.append2LastQA(text);
|
|
}
|
|
}
|
|
if (session.srcMod != null && session.cbUri != null) {
|
|
if (session.srcMod != null && session.cbUri != null) {
|
|
@@ -710,7 +709,7 @@ public class JedgeBotGptService extends JMgbusService {
|
|
//解决
|
|
//解决
|
|
for(String mkey : knKeyList) {
|
|
for(String mkey : knKeyList) {
|
|
// String content =
|
|
// String content =
|
|
- pool.getKnowledgeFromMilvus(s, mkey, false);
|
|
|
|
|
|
+ pool.getKnowledgeFromMilvus(s, mkey, false);
|
|
// module.highLog(String.format("Recall info from answer(%s) : %s", mkey, content));
|
|
// module.highLog(String.format("Recall info from answer(%s) : %s", mkey, content));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -967,18 +966,10 @@ public class JedgeBotGptService extends JMgbusService {
|
|
for(String k : keys) {
|
|
for(String k : keys) {
|
|
JedgeServiceSession s = sessions_.get(k);
|
|
JedgeServiceSession s = sessions_.get(k);
|
|
s.ttl ++;
|
|
s.ttl ++;
|
|
- if(s.ttl > JEJBotLLMConst.ival_timeout_session_ ) { //会话3分钟没有任何返回,清空
|
|
|
|
|
|
+ if(s.ttl > JedgeBotConst.ival_timeout_session_ ) { //会话3分钟没有任何返回,清空
|
|
toDel.add(k);
|
|
toDel.add(k);
|
|
module.highLog(String.format("Timeout talk session %s from %s", k, s.srcMod));
|
|
module.highLog(String.format("Timeout talk session %s from %s", k, s.srcMod));
|
|
// returnMgbusMessage(s.srcMod, s.cbUri, "会话超时", true, true);
|
|
// returnMgbusMessage(s.srcMod, s.cbUri, "会话超时", true, true);
|
|
- } else {
|
|
|
|
- //会话池心跳
|
|
|
|
- try {
|
|
|
|
- if(s.ttl % 5 ==0 )
|
|
|
|
- s.getGdtSession().knItemPoolHeartBeat();
|
|
|
|
- }catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for(String key : toDel)
|
|
for(String key : toDel)
|
|
@@ -1008,15 +999,10 @@ public class JedgeBotGptService extends JMgbusService {
|
|
d.put("nb",true);
|
|
d.put("nb",true);
|
|
msg.put("~c", d);
|
|
msg.put("~c", d);
|
|
postServiceMessage(srcMod, cbUri, msg);
|
|
postServiceMessage(srcMod, cbUri, msg);
|
|
-
|
|
|
|
- QIData msg2 = new QData();
|
|
|
|
- msg2.putString("msg", txtMessage);
|
|
|
|
- msg2.putBoolean("end", isEndMsg);
|
|
|
|
- postServiceMessage("tvmufis", "/tvmfs/glmmsg", msg2);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public void returnMgbusMessageWithKey(String key, String jeMod, String jeUriCb, String txtMessage,
|
|
public void returnMgbusMessageWithKey(String key, String jeMod, String jeUriCb, String txtMessage,
|
|
- boolean isEndMsg , boolean isErr ) {
|
|
|
|
|
|
+ boolean isEndMsg , boolean isErr ) {
|
|
module.exMarkLog(String.format("Corpus More >>%s",txtMessage));
|
|
module.exMarkLog(String.format("Corpus More >>%s",txtMessage));
|
|
QIData msg = new QData();
|
|
QIData msg = new QData();
|
|
msg.setKey(key);
|
|
msg.setKey(key);
|
|
@@ -1114,7 +1100,7 @@ public class JedgeBotGptService extends JMgbusService {
|
|
|
|
|
|
public String makeRealGptChatCall(JedgeServiceSession s, String realPrompt) {
|
|
public String makeRealGptChatCall(JedgeServiceSession s, String realPrompt) {
|
|
QIData gr = new QData();
|
|
QIData gr = new QData();
|
|
- gr.put("history", s.history_);
|
|
|
|
|
|
+// gr.put("history", s.history_);
|
|
gr.put("query", realPrompt);
|
|
gr.put("query", realPrompt);
|
|
gr.put("cb", "/gpt/LocalStream");//本地stream返回
|
|
gr.put("cb", "/gpt/LocalStream");//本地stream返回
|
|
gr.put("sid", s.sid);
|
|
gr.put("sid", s.sid);
|
|
@@ -1341,7 +1327,7 @@ public class JedgeBotGptService extends JMgbusService {
|
|
private void knTalkLog(String prompt, boolean succ) {
|
|
private void knTalkLog(String prompt, boolean succ) {
|
|
String log = "["+ TimeUtils.getNowShortStrWithMillis()+"] ["+(succ?"SUC":"FAL")+"]>>" + prompt;
|
|
String log = "["+ TimeUtils.getNowShortStrWithMillis()+"] ["+(succ?"SUC":"FAL")+"]>>" + prompt;
|
|
log = log.replaceAll( "\n","");
|
|
log = log.replaceAll( "\n","");
|
|
-// FileUtils.appendLine(kn_talk_log_file, log+"\n", "UTF-8");
|
|
|
|
|
|
+ FileUtils.appendLine(kn_talk_log_file, log+"\n", "UTF-8");
|
|
}
|
|
}
|
|
|
|
|
|
protected JedgeServiceSession findSession(QIData req, String prompt) {
|
|
protected JedgeServiceSession findSession(QIData req, String prompt) {
|