From af97393316557ac7bfe0c439a9e281b46db004a1 Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Sun, 2 Mar 2025 11:14:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(module-llm):=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=A1=A5=E5=85=A8=E8=AF=B7=E6=B1=82=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=E5=92=8C=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改了日志输出格式,为不同的日志添加了特定的标识 -优化了模型补全请求的处理流程,使用 ContentType 设置请求体内容类型 - 移除了重复设置 Content-Type 头的代码 --- .../conversation/ConversationServiceImpl.java | 2 +- .../module/llm/service/http/ModelService.java | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/conversation/ConversationServiceImpl.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/conversation/ConversationServiceImpl.java index 0f38cf733..6af8deb6e 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/conversation/ConversationServiceImpl.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/conversation/ConversationServiceImpl.java @@ -416,7 +416,7 @@ public class ConversationServiceImpl implements ConversationService { ModelCompletionsReqVO modelCompletionsReqVO = new ModelCompletionsReqVO(); modelCompletionsReqVO.setMessages(messages); modelCompletionsReqVO.setModel(model); - log.info("构建模型补全请求对象,请求参数: {}", modelCompletionsReqVO); + log.info("构建模型补全请求对象,请求参数1: {}", modelCompletionsReqVO); // 调用模型服务进行流式处理 modelService.modelCompletionsStream(selfModelUrl, modelCompletionsReqVO, emitter); 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 e71fc3dd0..7ef401702 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 @@ -15,6 +15,7 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.springframework.http.MediaType; @@ -147,10 +148,11 @@ public class ModelService { */ public void modelCompletionsStream (String url, ModelCompletionsReqVO req, SseEmitter emitter) { req.setStream(true); - log.info("开始处理模型补全请求,参数: {}", req); + log.info("开始处理模型补全请求,参数2: {}", req); try { - log.info("开始处理模型补全请求,参数: {}", JSON.toJSONString(req)); - sendPostRequest(url, JSON.toJSONString(req), emitter); + String jsonString = JSON.toJSONString(req); + log.info("开始处理模型补全请求,参数3: {}", jsonString); + sendPostRequest(url, jsonString, emitter); } catch (Exception e) { emitter.completeWithError(e); } @@ -166,6 +168,7 @@ public class ModelService { private void sendPostRequest (String apiUrl, String requestBody, SseEmitter emitter) throws IOException { // 创建 HttpClient 实例 HttpClient httpClient = HttpClients.createDefault(); + // 创建 HttpPost 请求对象 HttpPost httpPost = new HttpPost(apiUrl); @@ -188,11 +191,15 @@ public class ModelService { */ private void setupRequest (HttpPost httpPost, String requestBody) throws IOException { // 创建 StringEntity 对象,用于封装请求体 - StringEntity entity = new StringEntity(requestBody); + StringEntity entity = new StringEntity( + requestBody, + // 自动设置Content-Type为application/json; charset=UTF-8 + ContentType.APPLICATION_JSON + ); // 设置请求体 httpPost.setEntity(entity); // 设置请求头,指定请求体的内容类型为 JSON - httpPost.setHeader("Content-Type", "application/json"); +// httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("Accept-Encoding", "gzip, deflate, br"); httpPost.setHeader("Accept", "*/*"); httpPost.setHeader("Connection", "keep-alive");