|
|
@@ -25,7 +25,6 @@ import javax.validation.constraints.NotNull;
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -88,6 +87,18 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
public static final String END_SIGN = "】";
|
|
|
public static final String SYMBOL = "【示意图序号";
|
|
|
|
|
|
+ public static final String MODEL = "glm-4-plus";
|
|
|
+
|
|
|
+ public static final String DATA_STR = "\n\n---\n\n答案引用自文档:";
|
|
|
+
|
|
|
+ public static final String QUESTION = "总结三个建议,二十个字内,固定格式";
|
|
|
+
|
|
|
+ public static final String USER_STR = "user";
|
|
|
+
|
|
|
+ public static final String TODAY = "今天";
|
|
|
+
|
|
|
+ public static final String BEFOR = "天前";
|
|
|
+
|
|
|
@Override
|
|
|
public SseEmitter sseInvoke(SseParams sseParams) {
|
|
|
SseEmitter sseEmitter = new SseEmitter(0L);
|
|
|
@@ -143,12 +154,24 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
// threadPoolTaskExecutor.execute(() -> send(sseEmitter,json));
|
|
|
}
|
|
|
} else {
|
|
|
+ if(com.takai.common.utils.StringUtils.isNotEmpty(type) && "finish".equals(type)){
|
|
|
+ String sliceInfo = getSliceInfo(id);
|
|
|
+ if(com.takai.common.utils.StringUtils.isNotEmpty(sliceInfo)){
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("id", id);
|
|
|
+ json.put("event", "add");
|
|
|
+ json.put("data", sliceInfo);
|
|
|
+ log.info("智谱返回切片消息:" + sliceInfo);
|
|
|
+ send(sseEmitter, json);
|
|
|
+ }
|
|
|
+ }
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("id", id);
|
|
|
json.put("event", type);
|
|
|
json.put("data", newData);
|
|
|
log.info("智谱返回信息:" + json);
|
|
|
send(sseEmitter,json);
|
|
|
+
|
|
|
// threadPoolTaskExecutor.execute(() -> send(sseEmitter,json));
|
|
|
}
|
|
|
//sseEmitter.send(json);
|
|
|
@@ -421,7 +444,7 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
.temperature("0.05")
|
|
|
.knowledgeIds("["+uuid+"]")
|
|
|
.sliceCount(4)
|
|
|
- .model("glm-4-plus")
|
|
|
+ .model(MODEL)
|
|
|
.iconColor("blue")
|
|
|
.iconType("robot_assist")
|
|
|
.build();
|
|
|
@@ -561,11 +584,11 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
m.put("key", System.currentTimeMillis());
|
|
|
m.put("type", "group");
|
|
|
if(daysBetween == 0l){
|
|
|
- m.put("label", "今天");
|
|
|
+ m.put("label", TODAY);
|
|
|
m.put("children", setValue(entry.getValue()));
|
|
|
obj.add(m);
|
|
|
}else{
|
|
|
- m.put("label", -daysBetween+"天前");
|
|
|
+ m.put("label", -daysBetween+BEFOR);
|
|
|
m.put("children", setValue(entry.getValue()));
|
|
|
obj.add(m);
|
|
|
}
|
|
|
@@ -587,7 +610,7 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
SseEmitter sseEmitter = new SseEmitter(0L);
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("model",params.getModel());
|
|
|
- json.put("messages",params.getMessages());
|
|
|
+ json.put("messages", params.getMessages());
|
|
|
json.put("stream", params.getStream());
|
|
|
json.put("request_id", params.getRequest_id());
|
|
|
// json.put("tools", params.getTools());
|
|
|
@@ -662,8 +685,7 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Map<String, Object>> getSliceInfo(String requestId) {
|
|
|
- List<Map<String, Object>> list = new ArrayList();
|
|
|
+ public String getSliceInfo(String requestId) {
|
|
|
String url = bigModelConfig.getBaseurl() + bigModelConfig.getSliceInfo().replace("{request_id}",requestId);
|
|
|
Request request = buildGetRequest(url);
|
|
|
OkHttpClient client = buildOkHttpClient();
|
|
|
@@ -673,73 +695,123 @@ public class BigModelServiceImpl implements IBigModelService
|
|
|
String body = response.body().string();
|
|
|
boolean status = isJsonObject(body);
|
|
|
if(!status){
|
|
|
- return list;
|
|
|
+ return null;
|
|
|
}else{
|
|
|
JSONObject jsonObj = JSON.parseObject(body);
|
|
|
JSONObject jb = jsonObj.getJSONObject("data");
|
|
|
JSONArray array = jb.getJSONArray("document_slices");
|
|
|
if(array != null){
|
|
|
+ String data = DATA_STR;
|
|
|
for(int i=0; i<array.size(); i++){
|
|
|
Map<String, Object> map = new HashMap();
|
|
|
JSONObject detailObj = array.getJSONObject(i);
|
|
|
JSONObject docObj = detailObj.getJSONObject("document");
|
|
|
String name = String.valueOf(docObj.get("name"));
|
|
|
String urlStr = String.valueOf(docObj.get("url"));
|
|
|
- map.put("name", name);
|
|
|
- map.put("url", urlStr);
|
|
|
- list.add(map);
|
|
|
+ data += "["+name+"]"+"("+urlStr +"),";
|
|
|
}
|
|
|
+ data = data.substring(0, data.length()-1);
|
|
|
+ return data;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
- return list;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public AsyncCompletions getAsyncCompletions(CompletionsParams params) {
|
|
|
+ public List<String> getAsyncCompletions(CompletionsParams params) {
|
|
|
+ log.info("---question---", params.getMessages());
|
|
|
+ PromptObject vo = new PromptObject();
|
|
|
+ vo.setContent(QUESTION);
|
|
|
+ vo.setRole(USER_STR);
|
|
|
+ params.getMessages().add(vo);
|
|
|
String url = bigModelConfig.getAsyncCompletions();
|
|
|
JSONObject json = new JSONObject();
|
|
|
- json.put("model",params.getModel());
|
|
|
+ json.put("model", MODEL);
|
|
|
json.put("messages",params.getMessages());
|
|
|
+ json.put("stream", true);
|
|
|
RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , json.toJSONString());
|
|
|
|
|
|
Request request = buildPostRequest(url,requestBody);
|
|
|
OkHttpClient client = buildOkHttpClient();
|
|
|
- AsyncCompletions result = null;
|
|
|
+ List<String> resultList = new ArrayList();
|
|
|
try {
|
|
|
Response response = client.newCall(request).execute();
|
|
|
if(response.isSuccessful()){
|
|
|
String body = response.body().string();
|
|
|
- JSONObject jsonObj = JSON.parseObject(body);
|
|
|
- result = JSONObject.parseObject(String.valueOf(jsonObj), AsyncCompletions.class);
|
|
|
+ boolean status = isJsonObject(body);
|
|
|
+ if(status){
|
|
|
+ JSONObject jsonObj = JSON.parseObject(body);
|
|
|
+ AsyncCompletions result = JSONObject.parseObject(String.valueOf(jsonObj), AsyncCompletions.class);
|
|
|
+ if(result != null){
|
|
|
+ for (int i=0; i<5; i++){
|
|
|
+ try {
|
|
|
+ Thread.sleep(i*1000); // 时间递增调用
|
|
|
+ // 用异步返回的数据
|
|
|
+ JSONObject obj = getAsyncResult(result.getId());
|
|
|
+ if(obj != null){
|
|
|
+ JSONArray array = obj.getJSONArray("choices");
|
|
|
+ if(array != null){
|
|
|
+ for(int j=0; j<array.size(); j++){
|
|
|
+ JSONObject megObj = array.getJSONObject(j);
|
|
|
+ JSONObject message = megObj.getJSONObject("message");
|
|
|
+ String content = String.valueOf(message.get("content"));
|
|
|
+ if(com.takai.common.utils.StringUtils.isNotEmpty(content)){
|
|
|
+ content = content.replace("*", "").replace("1.", "")
|
|
|
+ .replace("2.", "").replace("3.", "");
|
|
|
+ if(content.contains("\n")){
|
|
|
+ String[] strings = content.split("\n");
|
|
|
+ List<String> listFromArray = new ArrayList<>(Arrays.asList(strings));
|
|
|
+ resultList.addAll(listFromArray);
|
|
|
+ }else if(content.contains("、")){
|
|
|
+ String[] strings = content.split("、");
|
|
|
+ List<String> listFromArray = new ArrayList<>(Arrays.asList(strings));
|
|
|
+ resultList.addAll(listFromArray);
|
|
|
+ }else if(content.contains(",")){
|
|
|
+ String[] strings = content.split(",");
|
|
|
+ List<String> listFromArray = new ArrayList<>(Arrays.asList(strings));
|
|
|
+ resultList.addAll(listFromArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
- return result;
|
|
|
+ return resultList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public AsyncResult getAsyncResult(String id) {
|
|
|
+ public JSONObject getAsyncResult(String id) {
|
|
|
String url = bigModelConfig.getAsyncResult().replace("{id}", id);
|
|
|
Request request = buildGetRequest(url);
|
|
|
OkHttpClient client = buildOkHttpClient();
|
|
|
- AsyncResult vo = null;
|
|
|
try {
|
|
|
Response response = client.newCall(request).execute();
|
|
|
if(response.isSuccessful()){
|
|
|
String body = response.body().string();
|
|
|
- JSONObject jsonObj = JSON.parseObject(body);
|
|
|
- vo = JSONObject.parseObject(String.valueOf(jsonObj), AsyncResult.class);
|
|
|
+ if(isJsonObject(body)){
|
|
|
+ JSONObject jsonObj = JSON.parseObject(body);
|
|
|
+ return jsonObj;
|
|
|
+ //vo = JSONObject.parseObject(String.valueOf(jsonObj), AsyncResult.class);
|
|
|
+ }
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
-
|
|
|
- return vo;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
@Override
|