新增chat Robot 接口服务
This commit is contained in:
@@ -8,17 +8,12 @@ import com.ai.da.common.utils.LocalCacheUtils;
|
|||||||
import com.ai.da.common.utils.MultiReadHttpServletRequest;
|
import com.ai.da.common.utils.MultiReadHttpServletRequest;
|
||||||
import com.ai.da.common.utils.MultiReadHttpServletResponse;
|
import com.ai.da.common.utils.MultiReadHttpServletResponse;
|
||||||
import com.ai.da.model.vo.AuthPrincipalVo;
|
import com.ai.da.model.vo.AuthPrincipalVo;
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Required;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.util.StopWatch;
|
import org.springframework.util.StopWatch;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -28,7 +23,6 @@ import javax.servlet.ServletException;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -50,7 +44,8 @@ public class AuthenticationFilter extends OncePerRequestFilter {
|
|||||||
Arrays.asList("/favicon.ico","/doc.html","api/account/login","api/account/preLogin","api/account/sendEmail",
|
Arrays.asList("/favicon.ico","/doc.html","api/account/login","api/account/preLogin","api/account/sendEmail",
|
||||||
"/webjars/","/swagger-resources","/v2/api-docs","api/account/resetPwd",
|
"/webjars/","/swagger-resources","/v2/api-docs","api/account/resetPwd",
|
||||||
"/api/python/saveGeneratePicture", "/api/python/getLibraryByUserId",
|
"/api/python/saveGeneratePicture", "/api/python/getLibraryByUserId",
|
||||||
"/api/third/party/addUser","/api/third/party/editUser","/api/element/initDefaultSysFile");
|
"/api/third/party/addUser","/api/third/party/editUser","/api/element/initDefaultSysFile",
|
||||||
|
"/api/python/chatStream","/api/python/flush");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest httpServletRequest, @NonNull HttpServletResponse httpServletResponse, @NonNull FilterChain filterChain) throws ServletException, IOException {
|
protected void doFilterInternal(HttpServletRequest httpServletRequest, @NonNull HttpServletResponse httpServletResponse, @NonNull FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
|||||||
@@ -3,23 +3,23 @@ package com.ai.da.controller;
|
|||||||
import com.ai.da.common.enums.CollectionLevel1TypeEnum;
|
import com.ai.da.common.enums.CollectionLevel1TypeEnum;
|
||||||
import com.ai.da.common.response.Response;
|
import com.ai.da.common.response.Response;
|
||||||
import com.ai.da.common.utils.CopyUtil;
|
import com.ai.da.common.utils.CopyUtil;
|
||||||
import com.ai.da.common.utils.MD5Utils;
|
import com.ai.da.model.dto.ChatFlushDTO;
|
||||||
import com.ai.da.model.dto.CollectionDeleteFileDTO;
|
import com.ai.da.model.dto.ChatSendDTO;
|
||||||
import com.ai.da.model.dto.CollectionElementUploadDTO;
|
import com.ai.da.model.vo.PythonLibraryVo;
|
||||||
import com.ai.da.model.dto.CollectionGeneratePrintDTO;
|
import com.ai.da.model.vo.SysFileVO;
|
||||||
import com.ai.da.model.dto.CollectionSavePrintDTO;
|
|
||||||
import com.ai.da.model.vo.*;
|
|
||||||
import com.ai.da.python.PythonService;
|
import com.ai.da.python.PythonService;
|
||||||
import com.ai.da.service.*;
|
import com.ai.da.service.ChatRobotService;
|
||||||
|
import com.ai.da.service.LibraryService;
|
||||||
|
import com.ai.da.service.SysFileService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.validation.Valid;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -39,13 +39,16 @@ public class PythonController {
|
|||||||
@Resource
|
@Resource
|
||||||
private LibraryService libraryService;
|
private LibraryService libraryService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ChatRobotService chatRobotService;
|
||||||
|
|
||||||
@ApiOperation(value = "python服务保存图片到java服务")
|
@ApiOperation(value = "python服务保存图片到java服务")
|
||||||
@PostMapping("/saveGeneratePicture")
|
@PostMapping("/saveGeneratePicture")
|
||||||
public Response<String> upload(@RequestParam("file") MultipartFile file,
|
public Response<String> upload(@RequestParam("file") MultipartFile file,
|
||||||
@ApiParam("操作类型 generatePrint ->生成印花 " +
|
@ApiParam("操作类型 generatePrint ->生成印花 " +
|
||||||
"designCollection ->设计collection generateAdvancedDesign ->生成高级design")
|
"designCollection ->设计collection generateAdvancedDesign ->生成高级design")
|
||||||
@RequestParam(value = "operateType") String operateType) {
|
@RequestParam(value = "operateType") String operateType) {
|
||||||
return Response.success(pythonService.upload(file,operateType));
|
return Response.success(pythonService.upload(file, operateType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "通过文件类型获取系统文件")
|
@ApiOperation(value = "通过文件类型获取系统文件")
|
||||||
@@ -56,12 +59,25 @@ public class PythonController {
|
|||||||
|
|
||||||
@ApiOperation(value = "通过用户id获取library")
|
@ApiOperation(value = "通过用户id获取library")
|
||||||
@GetMapping("/getLibraryByUserId")
|
@GetMapping("/getLibraryByUserId")
|
||||||
public Response<Map<String,List<String>>> getLibraryByUserId(@RequestParam(value = "userId") Long userId) {
|
public Response<Map<String, List<String>>> getLibraryByUserId(@RequestParam(value = "userId") Long userId) {
|
||||||
List<PythonLibraryVo> response = CopyUtil.copyList(libraryService.selectByAccountIdAnd1TypeList(userId,
|
List<PythonLibraryVo> response = CopyUtil.copyList(libraryService.selectByAccountIdAnd1TypeList(userId,
|
||||||
Collections.singletonList(CollectionLevel1TypeEnum.SKETCH_BOARD.getRealName())),PythonLibraryVo.class);
|
Collections.singletonList(CollectionLevel1TypeEnum.SKETCH_BOARD.getRealName())), PythonLibraryVo.class);
|
||||||
//key转小写 统一
|
//key转小写 统一
|
||||||
return Response.success(response.stream().collect(Collectors.groupingBy(v ->v.getLevel2Type().toLowerCase(),
|
return Response.success(response.stream().collect(Collectors.groupingBy(v -> v.getLevel2Type().toLowerCase(),
|
||||||
Collectors.mapping(PythonLibraryVo::getUrl,Collectors.toList()))));
|
Collectors.mapping(PythonLibraryVo::getUrl, Collectors.toList()))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "发送用户输入消息")
|
||||||
|
@PostMapping("/chatStream")
|
||||||
|
public SseEmitter MessageToPythonChatStream(@RequestBody ChatSendDTO chatSendDTO) {
|
||||||
|
log.info(chatSendDTO.toString());
|
||||||
|
return chatRobotService.sendMessageToChatRobot(chatSendDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "刷新会话缓存")
|
||||||
|
@PostMapping("/flush")
|
||||||
|
public Response<String> ChatBufferFlush(@RequestBody ChatFlushDTO chatFlushDTO) {
|
||||||
|
return Response.success(chatRobotService.chatBufferFlush(chatFlushDTO));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/main/java/com/ai/da/model/dto/ChatFlushDTO.java
Normal file
27
src/main/java/com/ai/da/model/dto/ChatFlushDTO.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.ai.da.model.dto;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aida
|
||||||
|
* @version 1.0
|
||||||
|
* @project aida_back
|
||||||
|
* @description 刷新缓存DTO
|
||||||
|
* @date 2023/7/25 16:51:16
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ChatFlushDTO {
|
||||||
|
@NotNull(message = "userId cannot be empty!")
|
||||||
|
@ApiModelProperty("用户id")
|
||||||
|
private String user_id;
|
||||||
|
|
||||||
|
@NotBlank(message = "sessionId cannot be empty!")
|
||||||
|
@ApiModelProperty("会话ID")
|
||||||
|
private String session_id;
|
||||||
|
|
||||||
|
}
|
||||||
34
src/main/java/com/ai/da/model/dto/ChatSendDTO.java
Normal file
34
src/main/java/com/ai/da/model/dto/ChatSendDTO.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package com.ai.da.model.dto;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aida
|
||||||
|
* @version 1.0
|
||||||
|
* @project aida_back
|
||||||
|
* @description 机器人对话DTO
|
||||||
|
* @date 2023/7/25 16:35:55
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("chatRobot 对话")
|
||||||
|
public class ChatSendDTO {
|
||||||
|
|
||||||
|
@NotNull(message = "userId cannot be empty!")
|
||||||
|
@ApiModelProperty("用户id")
|
||||||
|
private String user_id;
|
||||||
|
|
||||||
|
@NotBlank(message = "sessionId cannot be empty!")
|
||||||
|
@ApiModelProperty("会话ID")
|
||||||
|
private String session_id;
|
||||||
|
|
||||||
|
@NotBlank(message = "Please input the message !")
|
||||||
|
@ApiModelProperty("消息")
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
}
|
||||||
18
src/main/java/com/ai/da/service/ChatRobotService.java
Normal file
18
src/main/java/com/ai/da/service/ChatRobotService.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.ai.da.service;
|
||||||
|
|
||||||
|
import com.ai.da.model.dto.ChatFlushDTO;
|
||||||
|
import com.ai.da.model.dto.ChatSendDTO;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aida
|
||||||
|
* @version 1.0
|
||||||
|
* @project aida_back
|
||||||
|
* @description 对话机器人服务接口
|
||||||
|
* @date 2023/7/25 16:42:18
|
||||||
|
*/
|
||||||
|
public interface ChatRobotService {
|
||||||
|
SseEmitter sendMessageToChatRobot(ChatSendDTO chatSendDTO);
|
||||||
|
|
||||||
|
String chatBufferFlush(ChatFlushDTO chatFlushDTO);
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package com.ai.da.service.impl;
|
||||||
|
|
||||||
|
import com.ai.da.model.dto.ChatFlushDTO;
|
||||||
|
import com.ai.da.model.dto.ChatSendDTO;
|
||||||
|
import com.ai.da.service.ChatRobotService;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aida
|
||||||
|
* @version 1.0
|
||||||
|
* @project ChatRobot
|
||||||
|
* @description 请求python Chat Stream 接口服务实现类
|
||||||
|
* @date 2023/7/10 10:41:45
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class ChatRobotServiceImpl implements ChatRobotService {
|
||||||
|
|
||||||
|
// @Value("")
|
||||||
|
String chatStreamUrl = "http://127.0.0.1:6789/api/chat_stream";
|
||||||
|
String chatBufferFlushUrl = "http://127.0.0.1:6789/api/chat_flush";
|
||||||
|
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
|
||||||
|
Gson gson = new GsonBuilder().create();
|
||||||
|
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||||
|
Integer timeout = 9999999;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SseEmitter sendMessageToChatRobot(ChatSendDTO chatSendDTO) {
|
||||||
|
SseEmitter emitter = new SseEmitter();
|
||||||
|
String requestBody = gson.toJson(chatSendDTO);
|
||||||
|
executorService.execute(() -> {
|
||||||
|
try {
|
||||||
|
// 这里根据你的业务逻辑,从服务获取数据
|
||||||
|
// 示例:从服务获取数据并逐条发送给客户端
|
||||||
|
URL urlObj = new URL(chatStreamUrl);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
|
||||||
|
connection.setConnectTimeout(timeout);
|
||||||
|
connection.setReadTimeout(timeout);
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
OutputStream outputStream = connection.getOutputStream();
|
||||||
|
outputStream.write(requestBody.getBytes());
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
log.info(line);
|
||||||
|
emitter.send(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
} else {
|
||||||
|
System.out.println("Request failed with status code: " + responseCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.disconnect();
|
||||||
|
emitter.complete();
|
||||||
|
} catch (Exception e) {
|
||||||
|
emitter.completeWithError(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return emitter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String chatBufferFlush(ChatFlushDTO chatFlushDTO) {
|
||||||
|
log.info(chatBufferFlushUrl);
|
||||||
|
return String.valueOf(restTemplate.postForEntity(chatBufferFlushUrl, chatFlushDTO, String.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@ server.port=5567
|
|||||||
#datasource
|
#datasource
|
||||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
spring.datasource.url=jdbc:mysql://18.167.251.121:33006/aida?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
spring.datasource.url=jdbc:mysql://18.167.251.121:33006/aida?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||||
spring.datasource.username=nacos
|
spring.datasource.username=root
|
||||||
spring.datasource.password=nacos
|
spring.datasource.password=root
|
||||||
|
|
||||||
#security
|
#security
|
||||||
spring.security.jwtSecret=JWTSECRET
|
spring.security.jwtSecret=JWTSECRET
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ server.port=5567
|
|||||||
#datasource
|
#datasource
|
||||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
spring.datasource.url=jdbc:mysql://18.167.251.121:33006/aida?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
spring.datasource.url=jdbc:mysql://18.167.251.121:33006/aida?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||||
spring.datasource.username=nacos
|
spring.datasource.username=root
|
||||||
spring.datasource.password=nacos
|
spring.datasource.password=root
|
||||||
|
|
||||||
#security
|
#security
|
||||||
spring.security.jwtSecret=JWTSECRET
|
spring.security.jwtSecret=JWTSECRET
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user