refactor(module-llm):优化模型补全请求的日志输出和请求处理

- 修改了日志输出格式,为不同的日志添加了特定的标识
-优化了模型补全请求的处理流程,使用 ContentType 设置请求体内容类型
- 移除了重复设置 Content-Type 头的代码
This commit is contained in:
Liuyang 2025-03-02 11:14:46 +08:00
parent e95e42da1d
commit af97393316
2 changed files with 13 additions and 6 deletions

View File

@ -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);

View File

@ -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");