package com.custom.webui.service.impl;
|
|
import com.custom.webui.config.props.LlmProperties;
|
import com.custom.webui.service.LlmService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.stereotype.Service;
|
|
import java.util.List;
|
|
/**
|
* 模拟 LLM 实现,用于在没有 Ollama 的环境下验证端到端链路。
|
*
|
* <p>启用方式:{@code webui.llm.provider=mock}(smoke profile 默认开启)。
|
*
|
* <p>它不是「假数据占位」,而是一个有价值的测试工具:
|
* <ul>
|
* <li>可以在 CI 里跑通「SSE 推送 → 消息落库 → done=1」的完整回归</li>
|
* <li>不依赖模型下载,新同学 clone 下来就能看到打字机效果</li>
|
* <li>输出中包含上下文摘要,便于肉眼确认 RAG 片段确实被拼进了 prompt</li>
|
* </ul>
|
*/
|
@Slf4j
|
@Service
|
@ConditionalOnProperty(name = "webui.llm.provider", havingValue = "mock")
|
public class MockLlmService implements LlmService {
|
|
private final LlmProperties properties;
|
|
public MockLlmService(LlmProperties properties) {
|
this.properties = properties;
|
log.warn("LLM 实现: Mock —— 当前不会真正调用大模型,仅用于链路验证。"
|
+ "如需真实对话请设置 webui.llm.provider=ollama");
|
}
|
|
@Override
|
public void streamChat(List<LlmMessage> messages, LlmOptions options, TokenConsumer consumer) {
|
String userInput = messages.stream()
|
.filter(m -> "user".equals(m.role()))
|
.reduce((first, second) -> second)
|
.map(LlmMessage::content)
|
// 截断用户输入:重新生成时历史会带上整段上文,
|
// 若不截断,每次重新生成回复都会比上次更长,长度呈平方级增长,
|
// 让本地测试变得非常慢。
|
.map(content -> abbreviate(content, 200))
|
.orElse("(空输入)");
|
|
boolean hasRagContext = messages.stream()
|
.anyMatch(m -> "system".equals(m.role()) && m.content().contains("参考资料"));
|
|
String reply = """
|
【Mock 模式回复】当前后端未连接真实大模型,这条内容由本地模拟器生成,用于验证 \
|
SSE 流式链路、消息落库与消息树渲染是否正常。
|
|
收到你的问题:%s
|
|
当前上下文统计:共 %d 条消息,模型参数 model=%s, temperature=%s, topP=%s。
|
%s
|
|
要接入真实模型,请执行:
|
1. 安装 Ollama 并执行 OLLAMA_HOST=0.0.0.0 ollama serve
|
2. ollama pull qwen2.5:7b
|
3. 把配置 webui.llm.provider 改为 ollama 后重启后端
|
""".formatted(
|
userInput,
|
messages.size(),
|
options != null && options.modelName() != null ? options.modelName() : defaultModel(),
|
options != null && options.temperature() != null ? options.temperature() : "默认",
|
options != null && options.topP() != null ? options.topP() : "默认",
|
hasRagContext
|
? "已检测到 RAG 参考资料注入(说明知识库检索链路生效)。"
|
: "本次未注入 RAG 参考资料。");
|
|
// 按字符切块,模拟真实模型逐 token 输出的节奏
|
int chunkSize = 2;
|
for (int i = 0; i < reply.length(); i += chunkSize) {
|
String chunk = reply.substring(i, Math.min(i + chunkSize, reply.length()));
|
consumer.accept(chunk, false);
|
try {
|
// 25ms/块 → 约 80 字符/秒,接近真实模型的打字观感
|
Thread.sleep(25);
|
} catch (InterruptedException e) {
|
// 前端断开连接会中断本线程,属于正常流程,恢复中断标记后退出
|
Thread.currentThread().interrupt();
|
log.debug("Mock 流式输出被中断(客户端可能已断开)");
|
return;
|
}
|
}
|
}
|
|
@Override
|
public String complete(List<LlmMessage> messages, LlmOptions options) {
|
// 非流式同样返回可识别内容,方便测试标题生成
|
String userInput = messages.stream()
|
.filter(m -> "user".equals(m.role()))
|
.reduce((first, second) -> second)
|
.map(LlmMessage::content)
|
.orElse("新对话");
|
return abbreviate(userInput.replaceAll("\\s+", " ").trim(), 30);
|
}
|
|
@Override
|
public String defaultModel() {
|
return "mock-model";
|
}
|
|
private String abbreviate(String s, int max) {
|
if (s == null) {
|
return "";
|
}
|
return s.length() <= max ? s : s.substring(0, max) + "...";
|
}
|
}
|