|
@@ -17,24 +17,30 @@ import java.util.regex.Pattern;
|
|
|
public class QizhenAssistant {
|
|
|
|
|
|
public static String noAnswer = "未找到相关知识";
|
|
|
- public static Map<String,String> getChatResponse(String userInput, String conversationId,String appId) {
|
|
|
+
|
|
|
+ public static Map<String, String> getChatResponse(String userInput, String conversationId, String appId, boolean tuili) {
|
|
|
int retry = 0;
|
|
|
try {
|
|
|
- return getAnswer(userInput, conversationId,appId);
|
|
|
- }catch(Exception e){
|
|
|
+ return getAnswer(userInput, conversationId, appId, tuili);
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
retry++;
|
|
|
- if(retry>=2){
|
|
|
- Map<String,String> map = new HashMap<>();
|
|
|
- map.put("answer","failed");
|
|
|
+ if (retry >= 2) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("answer", "failed");
|
|
|
return map;
|
|
|
}
|
|
|
- return getAnswer(userInput, conversationId,appId);
|
|
|
+ return getAnswer(userInput, conversationId, appId, tuili);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static Map<String, String> getChatResponse(String userInput, String conversationId, String appId) {
|
|
|
+ return getChatResponse(userInput, conversationId, appId, false);
|
|
|
+ }
|
|
|
+
|
|
|
public static final String domain = "https://console.bce.baidu.com/ai_apaas/personalSpace/knowledgeBase/document/detail/";
|
|
|
|
|
|
- private static boolean contants(String content, String regex){
|
|
|
+ private static boolean contants(String content, String regex) {
|
|
|
// 编译正则表达式
|
|
|
Pattern pattern = Pattern.compile(regex);
|
|
|
|
|
@@ -43,8 +49,8 @@ public class QizhenAssistant {
|
|
|
return matcher.find();
|
|
|
}
|
|
|
|
|
|
- private static Map<String,String> getAnswer(String userInput, String conversationId,String appId) {
|
|
|
- Map<String,String> map = new HashMap<>();
|
|
|
+ private static Map<String, String> getAnswer(String userInput, String conversationId, String appId, boolean tuili) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("app_id", appId);
|
|
|
json.put("conversation_id", conversationId);
|
|
@@ -54,30 +60,30 @@ public class QizhenAssistant {
|
|
|
HttpRequest request = HttpUtil.createRequest(Method.POST, "https://qianfan.baidubce.com/v2/app/conversation/runs")
|
|
|
.header("X-Appbuilder-Authorization", "Bearer bce-v3/ALTAK-MyGbNEA18oT3boS2nOga1/d8b5057f7842f59b2c64971d8d077fe724d0aed5")
|
|
|
.header("Content-Type", "application/json")
|
|
|
- .body(json.toJSONString()).timeout(60*1000);
|
|
|
+ .body(json.toJSONString()).timeout(60 * 1000);
|
|
|
;
|
|
|
long l = System.currentTimeMillis();
|
|
|
String resposne = request.execute().body();
|
|
|
String answer = JSONObject.parseObject(resposne).getString("answer");
|
|
|
- if(answer.contains(noAnswer)){
|
|
|
- map.put("answer",noAnswer);
|
|
|
+ if (answer.contains(noAnswer)) {
|
|
|
+ map.put("answer", noAnswer);
|
|
|
return map;
|
|
|
}
|
|
|
- map.put("answer",answer == null ? noAnswer : answer);
|
|
|
+ map.put("answer", answer == null ? noAnswer : answer);
|
|
|
JSONArray contents = JSONObject.parseObject(resposne).getJSONArray("content");
|
|
|
JSONArray docs = new JSONArray();
|
|
|
List<String> defaultReferences = new ArrayList<>();
|
|
|
- for(int i=0;i<contents.size();i++){
|
|
|
+ for (int i = 0; i < contents.size(); i++) {
|
|
|
JSONObject content = contents.getJSONObject(i);
|
|
|
- if("rag".equals(content.getString("content_type"))){
|
|
|
+ if ("rag".equals(content.getString("content_type"))) {
|
|
|
JSONObject outputs = content.getJSONObject("outputs");
|
|
|
- if(outputs!=null) {
|
|
|
+ if (outputs != null) {
|
|
|
JSONArray references = outputs.getJSONArray("references");
|
|
|
if (references != null && references.size() > 0) {
|
|
|
for (int j = 0; j < references.size(); j++) {
|
|
|
JSONObject reference = new JSONObject();
|
|
|
String id = references.getJSONObject(j).getString("id");
|
|
|
- if(!contants(answer,"\\^(\\[\\d+\\])*\\["+id+"\\](\\[\\d+\\])*\\^")){
|
|
|
+ if (!contants(answer, "\\^(\\[\\d+\\])*\\[" + id + "\\](\\[\\d+\\])*\\^")) {
|
|
|
continue;
|
|
|
}
|
|
|
reference.put("index", id);
|
|
@@ -93,15 +99,25 @@ public class QizhenAssistant {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if(docs==null || docs.size()==0){
|
|
|
- map.put("answer",noAnswer);
|
|
|
- return map;
|
|
|
+
|
|
|
+ boolean notMatch = (docs == null || docs.size() == 0);
|
|
|
+ if (!tuili) {//不允许推理
|
|
|
+ if(notMatch) {
|
|
|
+ map.put("answer", noAnswer);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ map.put("references", docs.toJSONString());
|
|
|
+ map.put("defaultReferences", JSON.toJSONString(defaultReferences));
|
|
|
+ }else if(!notMatch) {
|
|
|
+ map.put("references", docs.toJSONString());
|
|
|
+ }else{
|
|
|
+ map.put("references", "[]");
|
|
|
}
|
|
|
- map.put("references", docs.toJSONString());
|
|
|
- map.put("defaultReferences", JSON.toJSONString(defaultReferences));
|
|
|
+
|
|
|
System.out.println((System.currentTimeMillis() - l) / 1000 + userInput);
|
|
|
return map;
|
|
|
}
|
|
|
+
|
|
|
public static String getConversationId(String appId) {
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("app_id", appId);
|