From 7f5a1615b34fd96e354dca4ce495b899a32a7a60 Mon Sep 17 00:00:00 2001 From: shahaibo <1023316923@qq.com> Date: Tue, 20 May 2025 10:29:22 +0800 Subject: [PATCH] =?UTF-8?q?TASK:LLM=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../da/mapper/primary/entity/ChatMessage.java | 2 + .../model/dto/ReceiveCollectionElement.java | 1 + .../com/ai/da/model/vo/ChatMessageVO.java | 8 ++ .../ai/da/service/impl/LLMServiceImpl.java | 108 ++++++++++++++---- 4 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/ai/da/model/vo/ChatMessageVO.java diff --git a/src/main/java/com/ai/da/mapper/primary/entity/ChatMessage.java b/src/main/java/com/ai/da/mapper/primary/entity/ChatMessage.java index b1886773..70859881 100644 --- a/src/main/java/com/ai/da/mapper/primary/entity/ChatMessage.java +++ b/src/main/java/com/ai/da/mapper/primary/entity/ChatMessage.java @@ -31,5 +31,7 @@ public class ChatMessage implements Serializable { private Long accountId; + private Integer isImage; + private LocalDateTime createTime; } diff --git a/src/main/java/com/ai/da/model/dto/ReceiveCollectionElement.java b/src/main/java/com/ai/da/model/dto/ReceiveCollectionElement.java index ab6f257b..3c980a3c 100644 --- a/src/main/java/com/ai/da/model/dto/ReceiveCollectionElement.java +++ b/src/main/java/com/ai/da/model/dto/ReceiveCollectionElement.java @@ -9,4 +9,5 @@ public class ReceiveCollectionElement { private String level2Type; private String rgb; private String hsv; + private String minioUrl; } diff --git a/src/main/java/com/ai/da/model/vo/ChatMessageVO.java b/src/main/java/com/ai/da/model/vo/ChatMessageVO.java new file mode 100644 index 00000000..db179a62 --- /dev/null +++ b/src/main/java/com/ai/da/model/vo/ChatMessageVO.java @@ -0,0 +1,8 @@ +package com.ai.da.model.vo; + +import com.ai.da.mapper.primary.entity.ChatMessage; +import lombok.Data; + +@Data +public class ChatMessageVO extends ChatMessage { +} diff --git a/src/main/java/com/ai/da/service/impl/LLMServiceImpl.java b/src/main/java/com/ai/da/service/impl/LLMServiceImpl.java index 68717832..dc8ef5c2 100644 --- a/src/main/java/com/ai/da/service/impl/LLMServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/LLMServiceImpl.java @@ -9,6 +9,7 @@ import com.ai.da.common.utils.MinioUtil; import com.ai.da.mapper.primary.*; import com.ai.da.mapper.primary.entity.*; import com.ai.da.model.dto.ChatHistoryDTO; +import com.ai.da.model.dto.ReceiveCollectionElement; import com.ai.da.model.dto.ReceiveDesignParam; import com.ai.da.model.enums.*; import com.ai.da.model.vo.AuthPrincipalVo; @@ -22,6 +23,7 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; @@ -77,16 +79,18 @@ public class LLMServiceImpl implements LLMService { AuthPrincipalVo principal = jwtTokenHelper.parserToUser(token); Long accountId = principal.getId(); int userSeq = getNextSeq(projectId); // 获取当前session下一条消息序号 - String url = "http://18.167.251.121:10002/chat-stream"; + String url = "http://18.167.251.121:2011/chat-stream"; +// String url = "http://10.1.1.240:1013/chat-stream"; HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json"); JSONObject jsonBodyObject = new JSONObject(); - jsonBodyObject.put("session_id", projectId.toString()); + jsonBodyObject.put("project_id", projectId.toString()); jsonBodyObject.put("role", "user"); - jsonBodyObject.put("image", imageUrlList); // 可扩展 +// jsonBodyObject.put("image", !CollectionUtils.isEmpty(imageUrlList) ? imageUrlList : ""); // 可扩展 + jsonBodyObject.put("image", ""); // 可扩展 jsonBodyObject.put("file", fileUrl != null ? fileUrl : ""); jsonBodyObject.put("message", prompt); jsonBodyObject.put("enable_thinking", false); @@ -107,7 +111,18 @@ public class LLMServiceImpl implements LLMService { } // 2. 流式接收并累积内容 - StringBuilder responseBuilder = new StringBuilder(); + // 3. 存储系统回复 + int systemSeq = getNextSeq(projectId); + ChatMessage systemMessage = new ChatMessage(); + systemMessage.setRole("system"); + systemMessage.setIsImage(0); + systemMessage.setProjectId(projectId); + systemMessage.setSeq(systemSeq); +// systemMessage.setCreateTime(LocalDateTime.now()); +// systemMessage.setContent(responseBuilder.toString()); + systemMessage.setAccountId(accountId); +// chatMessageMapper.insert(systemMessage); + StringBuilder responseContentBuilder = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { String line; while ((line = reader.readLine()) != null) { @@ -117,42 +132,65 @@ public class LLMServiceImpl implements LLMService { System.out.println(jsonStr); JSONObject json = JSON.parseObject(jsonStr); String status = json.getString("status"); - - if ("[DONE]".equals(status)) { - break; - } - if (!StringUtils.isEmpty(status)) { String content = json.getString("content"); - if (!status.equals("[RUNNING]") && !status.equals("[DESIGN_SIGNAL]")) { + // 结束标识 + if ("[DONE]".equals(status)) { + break; + }else if ("[RUNNING]".equals(status)) { + if (content.startsWith("\n[TOOL_CALL]") || content.startsWith("\n[TOOL_RESPONSE]")) { + // 不发送不存储 + }else { + responseContentBuilder.append(content); + emitter.send(json.toJSONString()); + } + }else { + if (responseContentBuilder.length() != 0) { + systemMessage.setCreateTime(LocalDateTime.now()); + systemMessage.setContent(responseContentBuilder.toString()); + chatMessageMapper.insert(systemMessage); + systemMessage.setId(null); + systemMessage.setSeq(getNextSeq(projectId)); + responseContentBuilder = new StringBuilder(); + } JSONObject toolsData = json.getJSONObject("tools_data"); ReceiveDesignParam receiveDesignParam = JSONObject.parseObject(JSONObject.toJSONString(toolsData), ReceiveDesignParam.class); receiveDesignParam.setProjectId(projectId); designService.receiveDesignParams(receiveDesignParam); - } - if (content != null) { - responseBuilder.append(content); + for (ReceiveCollectionElement receiveCollectionElement : receiveDesignParam.getReceiveCollectionElementList()) { + if (!StringUtils.isEmpty(receiveCollectionElement.getUrl())) { + receiveCollectionElement.setMinioUrl(minioUtil.getPreSignedUrl(receiveCollectionElement.getUrl(), 24 * 60)); + } + } + String jsonString = JSONObject.toJSONString(receiveDesignParam); + json.put("tools_data", jsonString); + + ChatMessage systemImage = new ChatMessage(); + systemImage.setRole("system"); + systemImage.setIsImage(1); + systemImage.setProjectId(projectId); + systemImage.setSeq(getNextSeq(projectId)); + systemImage.setCreateTime(LocalDateTime.now()); + systemImage.setContent(JSONObject.toJSONString(receiveDesignParam.getReceiveCollectionElementList())); + systemImage.setAccountId(accountId); + chatMessageMapper.insert(systemImage); + emitter.send(json.toJSONString()); } } } } + if (responseContentBuilder.length() != 0) { + systemMessage.setCreateTime(LocalDateTime.now()); + systemMessage.setContent(responseContentBuilder.toString()); + chatMessageMapper.insert(systemMessage); + } } - - // 3. 存储系统回复 - int systemSeq = getNextSeq(projectId); - ChatMessage systemMessage = new ChatMessage(); - systemMessage.setRole("user"); - systemMessage.setProjectId(projectId); - systemMessage.setSeq(systemSeq); - systemMessage.setCreateTime(LocalDateTime.now()); - systemMessage.setContent(responseBuilder.toString()); - systemMessage.setAccountId(accountId); - chatMessageMapper.insert(systemMessage); } emitter.complete(); } catch (Exception e) { + System.out.println("走进异常"); emitter.completeWithError(e); } }); @@ -274,9 +312,29 @@ public class LLMServiceImpl implements LLMService { @Override public PageBaseResponse getChatHistory(ChatHistoryDTO chatHistoryDTO) { QueryWrapper qw = new QueryWrapper<>(); - qw.lambda().eq(ChatMessage::getProjectId, chatHistoryDTO); + qw.lambda().eq(ChatMessage::getProjectId, chatHistoryDTO.getProjectId()); qw.lambda().orderByDesc(ChatMessage::getSeq); Page chatMessagePage = chatMessageMapper.selectPage(new Page<>(chatHistoryDTO.getPage(), chatHistoryDTO.getSize()), qw); + for (ChatMessage record : chatMessagePage.getRecords()) { + if (record.getIsImage() == 1) { + String content = record.getContent(); + List list = JSONObject.parseArray(content, ReceiveCollectionElement.class); + for (ReceiveCollectionElement receiveCollectionElement : list) { + if (StringUtils.isEmpty(receiveCollectionElement.getUrl())) { + receiveCollectionElement.setMinioUrl(minioUtil.getPreSignedUrl(receiveCollectionElement.getUrl(), 24 * 60)); + } + } + record.setContent(JSONObject.toJSONString(list)); + } + if (record.getRole().equals("user")) { + String content = record.getContent(); + JSONObject jsonObject = JSONObject.parseObject(content); + String file = jsonObject.getString("file"); + if (!StringUtils.isEmpty(file)) { + jsonObject.put("file", minioUtil.getPreSignedUrl(file, 24 * 60)); + } + } + } return PageBaseResponse.success(chatMessagePage); }