支持 新版本 ollama 的深度思考输出
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||||
* No deletion without permission, or be held responsible to law.
|
* No deletion without permission, or be held responsible to law.
|
||||||
*/
|
*/
|
||||||
package com.jeesite.modules.cms.ai.config;
|
package com.jeesite.modules.ai.cms.config;
|
||||||
|
|
||||||
import com.jeesite.common.lang.StringUtils;
|
import com.jeesite.common.lang.StringUtils;
|
||||||
import com.jeesite.common.mapper.JsonMapper;
|
import com.jeesite.common.mapper.JsonMapper;
|
||||||
@@ -38,6 +38,7 @@ public class WebClientThinkConfig {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public WebClientCustomizer webClientCustomizerThink() {
|
public WebClientCustomizer webClientCustomizerThink() {
|
||||||
return webClientBuilder -> {
|
return webClientBuilder -> {
|
||||||
ExchangeFilterFunction requestFilter = ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
|
ExchangeFilterFunction requestFilter = ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
|
||||||
@@ -59,11 +60,12 @@ public class WebClientThinkConfig {
|
|||||||
List<String> lines = new ArrayList<>();
|
List<String> lines = new ArrayList<>();
|
||||||
String[] list = eventString.split("\\n", -1);
|
String[] list = eventString.split("\\n", -1);
|
||||||
for (String line : list) {
|
for (String line : list) {
|
||||||
if (!line.startsWith("data: ")) {
|
String jsonPart = line;
|
||||||
lines.add(line);
|
boolean dataPrefix = false;
|
||||||
continue;
|
if (line.startsWith("data: ")) {
|
||||||
|
jsonPart = line.substring("data: ".length()).trim();
|
||||||
|
dataPrefix = true;
|
||||||
}
|
}
|
||||||
String jsonPart = line.substring("data: ".length()).trim();
|
|
||||||
if (!(StringUtils.startsWith(jsonPart, "{")
|
if (!(StringUtils.startsWith(jsonPart, "{")
|
||||||
&& StringUtils.endsWith(jsonPart, "}")
|
&& StringUtils.endsWith(jsonPart, "}")
|
||||||
&& !"data: [DONE]".equals(line))) {
|
&& !"data: [DONE]".equals(line))) {
|
||||||
@@ -76,38 +78,54 @@ public class WebClientThinkConfig {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 修改内容字段
|
// 修改内容字段
|
||||||
List<Object> choices = (List<Object>)map.get("choices");
|
boolean ollamaEvent = false;
|
||||||
|
List<Object> choices = (List<Object>) map.get("choices");
|
||||||
if (choices == null) {
|
if (choices == null) {
|
||||||
lines.add(line);
|
Map<String, Object> message = (Map<String, Object>) map.get("message");
|
||||||
continue;
|
if (message == null) {
|
||||||
|
lines.add(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
choices = List.of(message);
|
||||||
|
ollamaEvent = true;
|
||||||
}
|
}
|
||||||
for (Object o : choices) {
|
for (Object o : choices) {
|
||||||
Map<String, Object> choice = (Map<String, Object>) o;
|
Map<String, Object> choice = (Map<String, Object>) o;
|
||||||
if (choice == null) {
|
if (choice == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
String content;
|
||||||
|
String reasoningContent;
|
||||||
Map<String, Object> delta = (Map<String, Object>) choice.get("delta");
|
Map<String, Object> delta = (Map<String, Object>) choice.get("delta");
|
||||||
if (delta == null) {
|
if (delta != null) {
|
||||||
continue;
|
content = (String) delta.get("content");
|
||||||
|
reasoningContent = (String) delta.get("reasoning_content");
|
||||||
|
} else {
|
||||||
|
content = (String) choice.get("content");
|
||||||
|
reasoningContent = (String) choice.get("thinking");
|
||||||
}
|
}
|
||||||
String reasoningContent = (String) delta.get("reasoning_content");
|
|
||||||
String content = (String) delta.get("content");
|
|
||||||
if (StringUtils.isNotEmpty(reasoningContent) && StringUtils.isEmpty(content)) {
|
if (StringUtils.isNotEmpty(reasoningContent) && StringUtils.isEmpty(content)) {
|
||||||
if (!thinkingFlag.get()) {
|
if (!thinkingFlag.get()) {
|
||||||
thinkingFlag.set(true);
|
thinkingFlag.set(true);
|
||||||
delta.put("content", "<think>\n" + reasoningContent);
|
content = "<think>\n" + reasoningContent;
|
||||||
} else {
|
} else {
|
||||||
delta.put("content", reasoningContent);
|
content = reasoningContent;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (thinkingFlag.get()) {
|
if (thinkingFlag.get()) {
|
||||||
thinkingFlag.set(false);
|
thinkingFlag.set(false);
|
||||||
delta.put("content", "</think>\n" + (content == null ? "" : content));
|
content = "</think>\n" + (content == null ? "" : content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (ollamaEvent) {
|
||||||
|
choice.put("content", content);
|
||||||
|
map.put("message", choice);
|
||||||
|
} else if (delta != null) {
|
||||||
|
delta.put("content", content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 重新生成事件字符串
|
// 重新生成事件字符串
|
||||||
lines.add("data: " + JsonMapper.toJson(map));
|
lines.add((dataPrefix ? "data: " : "") + JsonMapper.toJson(map));
|
||||||
}
|
}
|
||||||
String finalLine = StringUtils.join(lines, "\n");
|
String finalLine = StringUtils.join(lines, "\n");
|
||||||
logger.trace("Modified response: ==> {}", finalLine);
|
logger.trace("Modified response: ==> {}", finalLine);
|
||||||
Reference in New Issue
Block a user