新增chat Robot 接口服务
This commit is contained in:
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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user