Spring AI Alibaba Graph 中的 LLM 流式输出
使用流式 ChatClient
Spring AI Alibaba 支持通过 ChatClient 进行流式输出。
使用流式 ChatClient查看完整代码
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import reactor.core.publisher.Flux;
// 使用流式输出
Flux<ChatResponse> flux = chatClient.prompt()
.user("tell me a joke")
.stream()
.chatResponse();
// 订阅流式响应
flux.subscribe(
response -> {
String content = response.getResult().getOutput().getText();
System.out.print(content);
},
error -> System.err.println("Error: " + error.getMessage()),
() -> System.out.println("
Stream completed")
);