feat: 实现伪流式接口,实时展示 LLM 执行步骤
- 新增 StreamingStepAdvisor 捕获每个执行步骤(工具调用、响应等) - 在 ChatService 中实现 chatStreamWithTools 方法 - 使用 SSE 实时推送执行步骤到前端 - 新增 /api/chat/stream-with-tools 接口 - 创建测试页面 stream-test.html 展示实时步骤 功能特性: - 实时显示用户消息 - 实时显示工具调用(工具名称、参数) - 实时显示工具响应 - 实时显示 AI 文本响应 - 支持错误处理和显示 访问地址:http://localhost:8080/stream-test.html Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
85b3130514
commit
0c5655af5b
4 files changed
+635
No files matched your search
@@ -0,0 +1,138 @@
|
||||
package org.springaicommunity.skills;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClientRequest;
|
||||
import org.springframework.ai.chat.client.ChatClientResponse;
|
||||
import org.springframework.ai.chat.client.advisor.api.AdvisorChain;
|
||||
import org.springframework.ai.chat.client.advisor.api.BaseAdvisor;
|
||||
import org.springframework.ai.chat.messages.AssistantMessage;
|
||||
|
||||
import reactor.core.publisher.Sinks;
|
||||
|
||||
/**
|
||||
* 流式步骤 Advisor - 捕获 LLM 执行的每个步骤并通过 SSE 发送
|
||||
*/
|
||||
public class StreamingStepAdvisor implements BaseAdvisor {
|
||||
|
||||
private final int order;
|
||||
private final Sinks.Many<Map<String, Object>> sink;
|
||||
|
||||
public StreamingStepAdvisor(int order, Sinks.Many<Map<String, Object>> sink) {
|
||||
this.order = order;
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return this.order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChain advisorChain) {
|
||||
// 发送用户消息事件
|
||||
var lastMessage = chatClientRequest.prompt().getInstructions()
|
||||
.get(chatClientRequest.prompt().getInstructions().size() - 1);
|
||||
|
||||
if (lastMessage.getMessageType() == org.springframework.ai.chat.messages.MessageType.USER) {
|
||||
Map<String, Object> event = new HashMap<>();
|
||||
event.put("type", "user_message");
|
||||
event.put("content", lastMessage.getText());
|
||||
emitEvent(event);
|
||||
}
|
||||
|
||||
return chatClientRequest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) {
|
||||
if (chatClientResponse.chatResponse() == null || chatClientResponse.chatResponse().getResults() == null) {
|
||||
return chatClientResponse;
|
||||
}
|
||||
|
||||
for (var generation : chatClientResponse.chatResponse().getResults()) {
|
||||
var message = generation.getOutput();
|
||||
|
||||
// 捕获工具调用
|
||||
if (message.getToolCalls() != null && !message.getToolCalls().isEmpty()) {
|
||||
for (var toolCall : message.getToolCalls()) {
|
||||
Map<String, Object> event = new HashMap<>();
|
||||
event.put("type", "tool_call");
|
||||
event.put("tool_name", toolCall.name());
|
||||
event.put("tool_id", toolCall.id());
|
||||
event.put("arguments", toolCall.arguments());
|
||||
emitEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
// 捕获工具响应
|
||||
if (message.getMessageType() == org.springframework.ai.chat.messages.MessageType.TOOL) {
|
||||
var toolResponseMessage = (org.springframework.ai.chat.messages.ToolResponseMessage) message;
|
||||
for (var toolResponse : toolResponseMessage.getResponses()) {
|
||||
Map<String, Object> event = new HashMap<>();
|
||||
event.put("type", "tool_response");
|
||||
event.put("tool_name", toolResponse.name());
|
||||
event.put("tool_id", toolResponse.id());
|
||||
event.put("response", truncate(toolResponse.responseData(), 500));
|
||||
emitEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
// 捕获 AI 文本响应
|
||||
if (message instanceof AssistantMessage && message.getText() != null && !message.getText().isEmpty()) {
|
||||
// 只在没有工具调用时发送文本(避免重复)
|
||||
if (message.getToolCalls() == null || message.getToolCalls().isEmpty()) {
|
||||
Map<String, Object> event = new HashMap<>();
|
||||
event.put("type", "assistant_message");
|
||||
event.put("content", message.getText());
|
||||
emitEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return chatClientResponse;
|
||||
}
|
||||
|
||||
private void emitEvent(Map<String, Object> event) {
|
||||
try {
|
||||
sink.tryEmitNext(event);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to emit event: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String truncate(String text, int maxLength) {
|
||||
if (text == null) return "";
|
||||
if (text.length() <= maxLength) return text;
|
||||
return text.substring(0, maxLength) + "...";
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private int order = 0;
|
||||
private Sinks.Many<Map<String, Object>> sink;
|
||||
|
||||
public Builder order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sink(Sinks.Many<Map<String, Object>> sink) {
|
||||
this.sink = sink;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StreamingStepAdvisor build() {
|
||||
if (sink == null) {
|
||||
throw new IllegalStateException("Sink must be provided");
|
||||
}
|
||||
return new StreamingStepAdvisor(this.order, this.sink);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user