TASK:LLM;

This commit is contained in:
shahaibo
2025-05-20 10:29:22 +08:00
parent 738144ad22
commit 7f5a1615b3
4 changed files with 94 additions and 25 deletions

View File

@@ -31,5 +31,7 @@ public class ChatMessage implements Serializable {
private Long accountId;
private Integer isImage;
private LocalDateTime createTime;
}

View File

@@ -9,4 +9,5 @@ public class ReceiveCollectionElement {
private String level2Type;
private String rgb;
private String hsv;
private String minioUrl;
}

View File

@@ -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 {
}

View File

@@ -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<ChatMessage> getChatHistory(ChatHistoryDTO chatHistoryDTO) {
QueryWrapper<ChatMessage> qw = new QueryWrapper<>();
qw.lambda().eq(ChatMessage::getProjectId, chatHistoryDTO);
qw.lambda().eq(ChatMessage::getProjectId, chatHistoryDTO.getProjectId());
qw.lambda().orderByDesc(ChatMessage::getSeq);
Page<ChatMessage> chatMessagePage = chatMessageMapper.selectPage(new Page<>(chatHistoryDTO.getPage(), chatHistoryDTO.getSize()), qw);
for (ChatMessage record : chatMessagePage.getRecords()) {
if (record.getIsImage() == 1) {
String content = record.getContent();
List<ReceiveCollectionElement> 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);
}