refactor(yudao-module-llm): 重构模型补全请求的处理逻辑

- 将 sendPostRequest 方法从私有改为受保护的
- 优化了请求体的处理,使用 JSON.toJSONString 方法序列化请求对象
- 重构了 SseEmitter 的使用方式,提高了代码的可读性和可维护性
- 删除了冗余的私有方法,简化了代码结构
This commit is contained in:
Liuyang 2025-03-02 10:35:07 +08:00
parent 071516df0c
commit 6370cb223e

View File

@ -23,9 +23,6 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -150,12 +147,77 @@ public class ModelService {
log.info("开始处理模型补全请求,参数: {}", req);
try {
log.info("开始处理模型补全请求,参数: {}", JSON.toJSONString(req));
sendPostRequest(url, "{\"max_tokens\":4000,\"messages\":[{\"content\":\"<content></content>\",\"role\":\"system\"},{\"content\":\"介绍一下天津\",\"role\":\"user\"}],\"model\":\"Qwen2.5-0.5B-Instruct\",\"temperature\":0.7,\"stream\":true}", emitter);
sendPostRequest(url, JSON.toJSONString(req), emitter);
} catch (Exception e) {
emitter.completeWithError(e);
}
}
/**
* 发送 POST 请求并处理响应
*
* @param apiUrl 目标 API URL
* @param requestBody 请求体内容
* @throws IOException 发送请求或处理响应时可能抛出的 IO 异常
*/
private void sendPostRequest (String apiUrl, String requestBody, SseEmitter emitter) throws IOException {
// 创建 HttpClient 实例
HttpClient httpClient = HttpClients.createDefault();
// 创建 HttpPost 请求对象
HttpPost httpPost = new HttpPost(apiUrl);
// 设置请求体和请求头
setupRequest(httpPost, requestBody);
// 执行 POST 请求并获取响应
HttpResponse response = httpClient.execute(httpPost);
// 处理响应实体
handleResponseEntity(response, emitter);
}
/**
* 设置请求体和请求头
*
* @param httpPost HttpPost 请求对象
* @param requestBody 请求体内容
* @throws IOException 创建 StringEntity 时可能抛出的 IO 异常
*/
private void setupRequest (HttpPost httpPost, String requestBody) throws IOException {
// 创建 StringEntity 对象用于封装请求体
StringEntity entity = new StringEntity(requestBody);
// 设置请求体
httpPost.setEntity(entity);
// 设置请求头指定请求体的内容类型为 JSON
httpPost.setHeader("Content-Type", "application/json");
}
/**
* 处理响应实体
*
* @param response HttpResponse 对象
* @throws IOException 读取响应实体输入流时可能抛出的 IO 异常
*/
private void handleResponseEntity (HttpResponse response, SseEmitter emitter) throws IOException {
// 获取响应实体
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 使用 try-with-resources 语句自动关闭输入流和 BufferedReader
try (InputStream inputStream = responseEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
// 逐行读取响应内容并处理
while ((line = reader.readLine()) != null) {
System.out.println("接收到的响应行数据: " + line);
String content = parseStreamLine(line);
if (content != null) {
emitter.send(SseEmitter.event().data(content));
}
}
}
}
}
/**
* 解析流式返回的单行数据提取有效内容并清理特定标记
*
@ -321,69 +383,4 @@ public class ModelService {
return null;
}
/**
* 发送 POST 请求并处理响应
*
* @param apiUrl 目标 API URL
* @param requestBody 请求体内容
* @throws IOException 发送请求或处理响应时可能抛出的 IO 异常
*/
private void sendPostRequest (String apiUrl, String requestBody, SseEmitter emitter) throws IOException {
// 创建 HttpClient 实例
HttpClient httpClient = HttpClients.createDefault();
// 创建 HttpPost 请求对象
HttpPost httpPost = new HttpPost(apiUrl);
// 设置请求体和请求头
setupRequest(httpPost, requestBody);
// 执行 POST 请求并获取响应
HttpResponse response = httpClient.execute(httpPost);
// 处理响应实体
handleResponseEntity(response, emitter);
}
/**
* 设置请求体和请求头
*
* @param httpPost HttpPost 请求对象
* @param requestBody 请求体内容
* @throws IOException 创建 StringEntity 时可能抛出的 IO 异常
*/
private void setupRequest (HttpPost httpPost, String requestBody) throws IOException {
// 创建 StringEntity 对象用于封装请求体
StringEntity entity = new StringEntity(requestBody);
// 设置请求体
httpPost.setEntity(entity);
// 设置请求头指定请求体的内容类型为 JSON
httpPost.setHeader("Content-Type", "application/json");
}
/**
* 处理响应实体
*
* @param response HttpResponse 对象
* @throws IOException 读取响应实体输入流时可能抛出的 IO 异常
*/
private void handleResponseEntity (HttpResponse response, SseEmitter emitter) throws IOException {
// 获取响应实体
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 使用 try-with-resources 语句自动关闭输入流和 BufferedReader
try (InputStream inputStream = responseEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
// 逐行读取响应内容并处理
while ((line = reader.readLine()) != null) {
System.out.println("接收到的响应行数据: " + line);
String content = parseStreamLine(line);
if (content != null) {
emitter.send(SseEmitter.event().data(content));
}
}
}
}
}
}