|
@@ -5,7 +5,9 @@ import cn.cslg.pas.common.model.dify.ChatMessageDTO;
|
|
|
import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
|
|
|
import cn.cslg.pas.common.model.dify.DifyHistoryMessageDTO;
|
|
|
|
|
|
+import cn.cslg.pas.common.utils.CacheUtils;
|
|
|
import cn.cslg.pas.common.utils.DataUtils;
|
|
|
+import cn.cslg.pas.common.utils.LoginUtils;
|
|
|
import cn.cslg.pas.domain.business.TechnicalCase;
|
|
|
import cn.cslg.pas.domain.report.AssoProjectConfession;
|
|
|
import cn.cslg.pas.service.business.TechnicalCaseService;
|
|
@@ -20,6 +22,7 @@ import com.google.gson.GsonBuilder;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -56,6 +59,10 @@ public class DifyService {
|
|
|
private final DifySessionService difySessionService;
|
|
|
private final TechnicalCaseService technicalCaseService;
|
|
|
private final AssoProjectConfessionService assoProjectConfessionService;
|
|
|
+ @Autowired
|
|
|
+ private CacheUtils cacheUtils;
|
|
|
+ @Autowired
|
|
|
+ private LoginUtils loginUtils;
|
|
|
|
|
|
/**
|
|
|
* 调用文件系统删除文件接口
|
|
@@ -81,7 +88,7 @@ public class DifyService {
|
|
|
* 调用文件系统删除文件接口
|
|
|
*/
|
|
|
public String queryHistoryMessage(DifyHistoryMessageDTO difyChatMessageDTO) throws IOException {
|
|
|
- difyChatMessageDTO.setUser("1");
|
|
|
+ difyChatMessageDTO.setUser(loginUtils.getId().toString());
|
|
|
OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
|
|
.connectTimeout(600, TimeUnit.SECONDS)
|
|
|
.writeTimeout(600, TimeUnit.SECONDS)
|
|
@@ -99,37 +106,11 @@ public class DifyService {
|
|
|
return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
|
|
|
}
|
|
|
|
|
|
- public Flux<String> chatMessageStream(DifyChatMessageDTO difyChatMessageDTO) {
|
|
|
- Gson gson = new GsonBuilder()
|
|
|
- .disableHtmlEscaping() // 禁用 HTML 转义
|
|
|
- .setPrettyPrinting()
|
|
|
- .create();
|
|
|
-
|
|
|
- return WebClient.create().post().uri(url + "chat-messages").header("Authorization", "Bearer " + apiKey).header(HttpHeaders.CONTENT_TYPE, "application/json").bodyValue(gson.toJson(difyChatMessageDTO))
|
|
|
- .retrieve().bodyToFlux(String.class) // 接收原始字节
|
|
|
- .map(string -> {
|
|
|
- String chinese = "";
|
|
|
- try {
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
- JsonNode node = mapper.readTree(string);
|
|
|
- String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
|
|
|
-
|
|
|
- chinese = prettyJson;
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
- }
|
|
|
- return chinese;
|
|
|
- })// 这里指定编码
|
|
|
- .doOnNext(System.out::println).doFinally(data -> {
|
|
|
- System.out.println("数据接收完毕,可以在这里执行后续操作");
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
public Flux<String> getOkhttp(ChatMessageDTO chatMessageDTO) {
|
|
|
Integer projectId = chatMessageDTO.getProjectId();
|
|
|
String conversationId = chatMessageDTO.getConversationId();
|
|
|
- String userId = "1";
|
|
|
+ String userId = loginUtils.getId().toString();
|
|
|
String query = chatMessageDTO.getQuery();
|
|
|
if (conversationId == null) {
|
|
|
conversationId = difySessionService.getSessionId(projectId, userId);
|
|
@@ -260,7 +241,7 @@ public class DifyService {
|
|
|
public Map<String, Object> generateInventionPoint(ChatMessageDTO chatMessageDTO) throws IOException {
|
|
|
Integer projectId = chatMessageDTO.getProjectId();
|
|
|
String query = "生成发明点";
|
|
|
- String userId = "1";
|
|
|
+ String userId = loginUtils.getId().toString();
|
|
|
String conversationId = chatMessageDTO.getConversationId();
|
|
|
DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
@@ -286,10 +267,10 @@ public class DifyService {
|
|
|
JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
String inventionPoint = jsonObject.get("answer").toString();
|
|
|
inventionPoint = DataUtils.getMarkDownText(inventionPoint);
|
|
|
- if (conversationId == null) {
|
|
|
- conversationId = jsonObject.get("conversation_id").toString();
|
|
|
- difySessionService.addDifySession(projectId, userId, conversationId);
|
|
|
- }
|
|
|
+// if (conversationId == null) {
|
|
|
+// conversationId = jsonObject.get("conversation_id").toString();
|
|
|
+// difySessionService.addDifySession(projectId, userId, conversationId);
|
|
|
+// }
|
|
|
Map<String, Object> map1 = new HashMap<>();
|
|
|
map1.put("invention_point", inventionPoint);
|
|
|
technicalCaseService.updateInventionPoint(projectId, inventionPoint);
|