refactor(yudao-module-llm): 调整模型补全请求的默认参数并优化数据处理
- 将 max_tokens 参数从 4000调整为 4096 - 将 temperature 参数从0.7 调整为 0.2 - 新增 top_p 参数,默认值为0.9 - 优化了数据处理逻辑,移除了不必要的注释代码 - 调整了 SseEmitter 的心跳检测机制 - 优化了数据解析过程,移除了多余的换行符
This commit is contained in:
parent
e0851f472a
commit
0340b24f78
@ -148,6 +148,9 @@ public class ModelService {
|
||||
*/
|
||||
public ModelCompletionsRespVO modelCompletionsStream (String url, ModelCompletionsReqVO req, SseEmitter emitter, String uuid) {
|
||||
req.setStream(true);
|
||||
req.setTemperature(0.2);
|
||||
req.setTop_p(0.9);
|
||||
req.setMax_tokens(4096);
|
||||
log.info("开始处理模型补全请求");
|
||||
|
||||
// 检查模型是否为空,若为空则设置默认模型
|
||||
@ -175,6 +178,38 @@ public class ModelService {
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// // 解析响应内容
|
||||
// log.info("正在解析响应内容...");
|
||||
// ChatCompletion chatCompletion = JSON.parseObject(result, ChatCompletion.class);
|
||||
//
|
||||
// // 检查响应内容是否包含错误信息
|
||||
// if (StringUtils.isBlank(chatCompletion.getDetail())) {
|
||||
// log.info("响应内容无错误信息,提取回答内容...");
|
||||
//
|
||||
// // 提取回答内容
|
||||
// String respContent = chatCompletion.getChoices().get(0).getMessage().getContent();
|
||||
// String patternString = "(<think>.*?</think>)";
|
||||
// Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL);
|
||||
// Matcher matcher = pattern.matcher(respContent);
|
||||
// String answerContent = matcher.replaceAll("");
|
||||
//
|
||||
// // 构建返回对象
|
||||
// ModelCompletionsRespVO respVO = new ModelCompletionsRespVO();
|
||||
// respVO.setSystem("助手");
|
||||
// respVO.setQuestion(req.getMessages().get(req.getMessages().size() - 1).getContent());
|
||||
// respVO.setAnswer(answer);
|
||||
//
|
||||
// log.info("模型补全请求处理成功。返回结果: {}", JSON.toJSONString(respVO));
|
||||
// return respVO;
|
||||
//
|
||||
// } else {
|
||||
// log.warn("响应内容包含错误信息,返回 null");
|
||||
// return null;
|
||||
// }
|
||||
// return null;
|
||||
|
||||
// 构建返回对象
|
||||
ModelCompletionsRespVO respVO = new ModelCompletionsRespVO();
|
||||
respVO.setSystem("助手");
|
||||
respVO.setQuestion(req.getMessages().get(req.getMessages().size() - 1).getContent());
|
||||
@ -249,6 +284,7 @@ public class ModelService {
|
||||
}
|
||||
|
||||
log.info("接收到的响应行数据: {}", line);
|
||||
line = line.replaceAll("\n", " ");
|
||||
String content = parseStreamLine(line, uuid);
|
||||
if (content != null) {
|
||||
emitter.send(
|
||||
@ -263,8 +299,9 @@ public class ModelService {
|
||||
}
|
||||
|
||||
// // 心跳检测
|
||||
// if (System.currentTimeMillis() - lastSendTime > MAX_IDLE_TIME && !result.toString().isEmpty()) {
|
||||
// emitter.send("[HEARTBEAT]");
|
||||
// if (System.currentTimeMillis() - lastSendTime > 15_000) {
|
||||
// emitter.send(SseEmitter.event().comment("heartbeat"));
|
||||
// lastSendTime = System.currentTimeMillis();
|
||||
// }
|
||||
}
|
||||
emitter.complete();
|
||||
@ -285,7 +322,7 @@ public class ModelService {
|
||||
if (StringUtils.isNotBlank(line)) {
|
||||
if (line.startsWith("data: ")) {
|
||||
String dataString = extractJsonFromDataString(line);
|
||||
if (!dataString.contains("[DONE]")||!dataString.contains("</think>")) {
|
||||
if (!dataString.contains("[DONE]")) {
|
||||
JSONObject jsonObject = JSON.parseObject(dataString);
|
||||
// 获取 choices 数组
|
||||
JSONArray choicesArray = jsonObject.getJSONArray("choices");
|
||||
|
@ -15,12 +15,10 @@ public class ModelCompletionsReqVO {
|
||||
|
||||
private String model;
|
||||
private List<ModelCompletionsMessage> messages;
|
||||
private Integer max_tokens = 4000;
|
||||
private Double temperature = 0.7;
|
||||
// private Integer max_tokens;
|
||||
// private Double temperature;
|
||||
private Integer max_tokens = 4096;
|
||||
private Double temperature = 0.2;
|
||||
private Double top_p = 0.9;
|
||||
private Boolean stream;
|
||||
// private Integer max_length = 120000;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
|
Loading…
x
Reference in New Issue
Block a user