feat(llm): 为模型补全请求添加分组 ID 参数- 在 modelCompletionsStream 方法中添加 groupId 参数
- 将 groupId 参数传递给 sendPostRequest 和 handleResponseEntity 方法 - 在 parseStreamLine 方法中为 ChatReqVO 对象添加 groupId 字段 - 优化了代码格式,调整了部分缩进和空格
This commit is contained in:
parent
35e19396e1
commit
1138058d74
@ -146,7 +146,7 @@ public class ModelService {
|
||||
* @param url 模型服务的 URL
|
||||
* @param req 模型补全请求对象
|
||||
*/
|
||||
public ModelCompletionsRespVO modelCompletionsStream (String url, ModelCompletionsReqVO req, SseEmitter emitter, String uuid) {
|
||||
public ModelCompletionsRespVO modelCompletionsStream (String url, ModelCompletionsReqVO req, SseEmitter emitter, String uuid, String groupId) {
|
||||
req.setStream(true);
|
||||
req.setTemperature(0.2);
|
||||
req.setTop_p(0.9);
|
||||
@ -168,11 +168,11 @@ public class ModelService {
|
||||
log.info("使用指定URL: {}", url);
|
||||
}
|
||||
|
||||
String answer="";
|
||||
String answer = "";
|
||||
try {
|
||||
String jsonString = JSON.toJSONString(req);
|
||||
log.info("开始处理模型补全请求,参数3: {}", jsonString);
|
||||
answer= sendPostRequest(url, jsonString, emitter, uuid);
|
||||
answer = sendPostRequest(url, jsonString, emitter, uuid, groupId);
|
||||
} catch (Exception e) {
|
||||
emitter.completeWithError(e);
|
||||
|
||||
@ -225,7 +225,7 @@ public class ModelService {
|
||||
* @param requestBody 请求体内容
|
||||
* @throws IOException 发送请求或处理响应时可能抛出的 IO 异常
|
||||
*/
|
||||
private String sendPostRequest (String apiUrl, String requestBody, SseEmitter emitter, String uuid) throws IOException {
|
||||
private String sendPostRequest (String apiUrl, String requestBody, SseEmitter emitter, String uuid, String groupId) throws IOException {
|
||||
// 创建 HttpClient 实例
|
||||
HttpClient httpClient = HttpClients.createDefault();
|
||||
|
||||
@ -239,7 +239,7 @@ public class ModelService {
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
|
||||
// 处理响应实体
|
||||
return handleResponseEntity(response, emitter, uuid);
|
||||
return handleResponseEntity(response, emitter, uuid, groupId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -269,7 +269,7 @@ public class ModelService {
|
||||
*
|
||||
* @param response HttpResponse 对象
|
||||
*/
|
||||
private String handleResponseEntity (HttpResponse response, SseEmitter emitter, String uuid) {
|
||||
private String handleResponseEntity (HttpResponse response, SseEmitter emitter, String uuid, String groupId) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
// 获取响应实体
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
@ -285,7 +285,7 @@ public class ModelService {
|
||||
|
||||
log.info("接收到的响应行数据: {}", line);
|
||||
line = line.replaceAll("\n", " ");
|
||||
String content = parseStreamLine(line, uuid);
|
||||
String content = parseStreamLine(line, uuid, groupId);
|
||||
if (content != null) {
|
||||
emitter.send(
|
||||
SseEmitter.event()
|
||||
@ -294,15 +294,15 @@ public class ModelService {
|
||||
log.info("已发送数据:{}", content);
|
||||
}
|
||||
ChatReqVO chatReqVO = JSONObject.parseObject(content, ChatReqVO.class);
|
||||
if (content!=null){
|
||||
if (content != null) {
|
||||
result.append(chatReqVO.getContent());
|
||||
}
|
||||
|
||||
// // 心跳检测
|
||||
// if (System.currentTimeMillis() - lastSendTime > 15_000) {
|
||||
// emitter.send(SseEmitter.event().comment("heartbeat"));
|
||||
// lastSendTime = System.currentTimeMillis();
|
||||
// }
|
||||
// // 心跳检测
|
||||
// if (System.currentTimeMillis() - lastSendTime > 15_000) {
|
||||
// emitter.send(SseEmitter.event().comment("heartbeat"));
|
||||
// lastSendTime = System.currentTimeMillis();
|
||||
// }
|
||||
}
|
||||
emitter.complete();
|
||||
} catch (IOException e) {
|
||||
@ -318,7 +318,7 @@ public class ModelService {
|
||||
* @param line 流式响应中的单行JSON数据
|
||||
* @return 处理后的文本内容(若无有效内容返回null)
|
||||
*/
|
||||
private String parseStreamLine (String line, String uuid) {
|
||||
private String parseStreamLine (String line, String uuid, String groupId) {
|
||||
if (StringUtils.isNotBlank(line)) {
|
||||
if (line.startsWith("data: ")) {
|
||||
String dataString = extractJsonFromDataString(line);
|
||||
@ -337,6 +337,7 @@ public class ModelService {
|
||||
content = content.replaceAll("\n", " ");
|
||||
ChatReqVO chatReqVO = new ChatReqVO();
|
||||
chatReqVO.setUuid("");
|
||||
chatReqVO.setGroupId("");
|
||||
chatReqVO.setContent(content);
|
||||
chatReqVO.setFinish_reason(false);
|
||||
return JSON.toJSONString(chatReqVO);
|
||||
@ -346,6 +347,7 @@ public class ModelService {
|
||||
|
||||
ChatReqVO chatReqVO = new ChatReqVO();
|
||||
chatReqVO.setUuid(uuid);
|
||||
chatReqVO.setGroupId(groupId);
|
||||
chatReqVO.setContent("");
|
||||
chatReqVO.setFinish_reason(true);
|
||||
return JSON.toJSONString(chatReqVO);
|
||||
|
Loading…
x
Reference in New Issue
Block a user