From 6370cb223ec59028477438b311708c353d3ada92 Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Sun, 2 Mar 2025 10:35:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(yudao-module-llm):=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E6=A8=A1=E5=9E=8B=E8=A1=A5=E5=85=A8=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 sendPostRequest 方法从私有改为受保护的 - 优化了请求体的处理,使用 JSON.toJSONString 方法序列化请求对象 - 重构了 SseEmitter 的使用方式,提高了代码的可读性和可维护性 - 删除了冗余的私有方法,简化了代码结构 --- .../module/llm/service/http/ModelService.java | 135 +++++++++--------- 1 file changed, 66 insertions(+), 69 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java index 11cc5cc27..f7d790d36 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/ModelService.java @@ -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\":\"\",\"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)); - } - } - } - } - } - }