Skip to Content
企业级Java开发中的应用人工智能 (Chinese Edition)
book

企业级Java开发中的应用人工智能 (Chinese Edition)

by Alex Soto Bueno, Markus Eisele, Natale Vinto
November 2025
Beginner to intermediate
430 pages
5h 12m
Chinese
O'Reilly Media, Inc.
Content preview from 企业级Java开发中的应用人工智能 (Chinese Edition)

第10章. LangGraph4j

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在的前两章中,您已掌握如何在多种场景下运用LangChain4j项目——例如构建聊天机器人、文本分类以及开发RAG系统。此外,我们还带您探索了向量嵌入领域,通过LangChain4j的特性开发了聚类等各类应用场景。然而您也注意到,面对某些高级场景时,仅靠LangChain4j尚显不足。

当构建复杂的多智能体应用时,你将涉及众多组件,包括大量prompt、工具、内容检索器或RAG系统。让所有组件协同运作固然令人欣喜,但正如前章所述,这种方法存在若干挑战:

  • 排除冗余令牌——例如未使用的工具或无关文档——这些元素可能拖慢模型运行速度、增加幻觉风险(模型提供错误响应)并推高成本

  • 管理各类代理的状态

  • 协调智能体

  • 处理更复杂的测试场景

  • 实现复杂行为以支持人机交互或流程并行化

LangGraph4j通过循环图(为简化起见,本书统称其为图)来协调和执行一个或多个智能体,从而解决了这些问题。在本章中,您将学习如何使用LangGraph4j来实现需要多个智能体或需要人工干预才能继续执行的高级AI应用程序。尽管本章使用的是LangGraph4j,但任何其他图框架同样适用。

理解LangGraph4j中的图结构

LangGraph4j( )是一个用于构建有状态多智能体应用程序的框架,主要用于配合LangChain4j和Spring AI使用。

LangGraph4j提供API用于构建循环图,定义节点、边或边间条件,并支持遍历图结构执行各节点定义的逻辑。

除图定义与执行外,LangGraph4j还提供以下特性:

  • 有状态图

  • 人机交互

  • 中断点(暂停与恢复)

  • 检查点

  • 并行执行

  • 子图

  • 时间旅行

在深入探讨LangGraph4j的使用方法前,让我们先回顾一些核心图概念。 图是一种非线性数据结构,由节点(顶点)及其间的连接边构成。 图用于表示实体间的关系或网络。

让我们从 LangGraph4j 的视角审视构成图的所有要素。

节点

节点代表 一个实体,该实体调用函数并将结果返回给后续节点。例如,节点可以是一个从第三方服务获取天气或汇率的函数,或通过LangChain4j向LLM提出问题的函数。

大多数情况下,图结构包含多个节点。其中两个特殊节点通常不执行任何操作:起始节点与终止节点仅用于标识执行流程的起止位置。

图10-1展示了一个包含多个节点的图示例。

A diagram of a graph illustrating multiple nodes connected by edges, with a designated start and end point.
图10-1. 包含起始/终止节点的多节点

除节点外,图结构还包含边。

在图中,边连接两个节点以建立关联关系。例如,可能需要连接两个节点:第一个节点获取天气信息,另一个节点生成对应天气的图标。

边可为有向(单向)或无向(双向),并可能带有条件。 非条件边情况下节点执行流程直线推进。而条件边连接的节点仅在满足边条件时才会执行。

图10-2展示了一个带边图的示例。

Diagram of a graph showing nodes connected by conditional and nonconditional edges, labeled with arrows and a condition "i > 18".
图10-2. 含条件边与非条件边的

图中的最后一个重要概念是状态。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

技术主管的进阶之路 (Chinese Edition)

技术主管的进阶之路 (Chinese Edition)

Anemari Fiser

Publisher Resources

ISBN: 0642572279905