Logo
首页
前沿
AI热点新闻
36氦头条
博客
开源
极简简历
极简智库
瞬间
相册
友链
知识库
创作者
搜索文章 Ctrl K

Spring AI Alibaba

返回 0
  • 快速开始
  • 版本说明
  • 概览
  • 框架
    • Agent 框架
      • 进阶指南
        • 多智能体(Multi-agent)
        • 分布式智能体(A2A Agent)
        • 工作流(Workflow)
        • 记忆管理(Memory)
        • 检索增强生成(RAG)
        • 人工介入(Human-in-the-Loop)
        • 沙箱运行时(Sandbox)
        • 上下文工程(Context Engineering)
        • 智能体作为工具(Agent Tool)
      • 教程
        • Agents
        • Hooks 和 Interceptors
        • Memory 短期记忆
        • Messages 消息
        • Models 模型
        • Skills 技能
        • Structured Output 结构化输出
        • Tools 工具
    • Studio
      • 快速开始
    • Graph Core
      • Graph 工作流编排指南
      • 核心概念
        • 持久化
        • 核心概念
        • 流式输出
        • 内存管理
      • 示例
        • 并行节点定义
        • 并行流式输出
        • 持久化执行
        • 多智能体监督者模式
        • 分配MCP工具给指定节点
        • 人类反馈
        • 时光旅行 - 状态历史回溯
        • 为图添加持久化(记忆)
        • 子图 Subgraphs
        • 子图作为 CompiledGraph
        • 子图作为 StateGraph
        • 子图作为节点操作
        • Graph 执行取消
        • PlantUML 图表可视化
        • Redis 检查点持久化
        • Spring AI Alibaba LLM 流式集成

Spring AI Alibaba Graph 中的 LLM 流式输出

使用流式 ChatClient

Spring AI Alibaba 支持通过 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("\nStream completed")

);

使用 Reactor 的阻塞式处理

Flux<ChatResponse> flux = chatClient.prompt()

        .user("tell me a joke")

        .stream()

        .chatResponse();



// 使用 Reactor 的阻塞式处理

flux.collectList().block().forEach(response -> {

    System.out.println("Received: " + response.getResult().getOutput().getText());

});

输出示例:


Sure, here's a joke for you:



Why don't scientists trust atoms?



Because they make up everything!

Stream completed

在 Graph 节点中使用流式输出

创建带流式输出的 Graph 节点

参考 节点流式输出文档 获取完整示例。

import com.alibaba.cloud.ai.graph.OverAllState;

import com.alibaba.cloud.ai.graph.action.NodeAction;

import org.springframework.ai.chat.client.ChatClient;

import reactor.core.publisher.Flux;



import java.util.Map;



public class StreamingAgentNode implements NodeAction {



    private final ChatClient chatClient;



    public StreamingAgentNode(ChatClient.Builder builder) {

        this.chatClient = builder.build();

    }



    @Override

    public Map<String, Object> apply(OverAllState state) {

        String userMessage = (String) state.value("query").orElse("Hello");



        // 使用流式输出

        Flux<String> contentFlux = chatClient.prompt()

                .user(userMessage)

                .stream()

                .content();



        return Map.of("answer", contentFlux);

    }

}

配置和运行

import com.alibaba.cloud.ai.graph.StateGraph;

import com.alibaba.cloud.ai.graph.OverAllState;

import com.alibaba.cloud.ai.graph.CompiledGraph;

import org.springframework.ai.chat.client.ChatClient;



// 配置 Graph

StateGraph graph = new StateGraph(keyStrategyFactory)

    .addNode("agent", new StreamingAgentNode(chatClientBuilder))

    .addEdge(StateGraph.START, "agent")

    .addEdge("agent", StateGraph.END);



CompiledGraph compiledGraph = graph.compile();



// 执行

Map<String, Object> input = Map.of("query", "Hello");

OverAllState result = compiledGraph.invoke(input);



System.out.println("Final result: " + result.value("answer").orElse(""));

相关文档

  • 节点流式输出 - 完整的流式输出示例

  • 快速入门 - Graph 基础使用

  • Spring AI 文档 - Spring AI 官方文档

目录
    共 50 篇 Spring AI Alibaba / Spring AI Alibaba LLM 流式集成 0 字 0 行 cosolar
    创建 2026-09-13T09:33:00.135451224Z 更新 2026-11-27T02:05:09.477553812Z
    Logo

    以极简为笔,以求索为墨。 简单书写,深度思考,每一次记录,都是与内心的对话。

    30,291 访问量
    219 点赞数
    0 评论数
    162 文章数 13 分类数 243 标签数
    2026-06-06 建站时间
    中国·北京 博主地址

    订阅更新

    订阅我们的技术文章,每周精选内容送达你的邮箱

    隐私政策 | 使用条款 | 友情链接 | 关于我们
    本主题由 腾讯云 提供云服务
    © 极简笔记 版权所有 津ICP备2026156702号-1
    Powered by Halo Theme halo-theme-cosolar