news 2026/4/16 14:18:30

AI大模型实用(六)Java快速实现智能体整理(LangChain4j-agentic)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
AI大模型实用(六)Java快速实现智能体整理(LangChain4j-agentic)

目录

一、使用langchain4j-agentic构建智能体应用

二、核心概念

1、 @Agent("Edits a story to better fit a given audience") 用于描述agent的设计目的,以及其他agent通过这些描述决定用不用、什么时候使用该agent.

2、Agent的名字,可以直接定义。也可以通过代理构建器的名称方法进行程序化

 未直接定义带有 @Agent 注释的方法名称

3、使用该 AgenticServices.agentBuilder() 方法构建该代理实例,指定接口和聊天模型。

4、AI service与 agent的区别

5、AgenticScope存储共享变量(多个agent直接交互)

6、工作流Workflow模式,这些模式可以组合起来,创造更复杂的工作流程。

7、循环工作流Loop

8、Parallel workflow  并行工作流

9、Conditional workflow  条件工作流

10、Asynchronous agents  异步代理

11、Error handling  错误处理

12、追踪Agent调用过程(可观测性)

13、以上内容都可以使用注解完成(即 声明式 API)


以下内容参考自:

https://docs.langchain4j.dev/tutorials/agents/

一、使用langchain4j-agentic构建智能体应用

智能体一般说是采用多个AI services来开发融入人工智能的应用程序.

智能系统架构可以分为两大类:

  • 工作流
  • 纯智能体

二、核心概念

1、 @Agent("Edits a story to better fit a given audience") 用于描述agent的设计目的,以及其他agent通过这些描述决定用不用、什么时候使用该agent.

public interface AudienceEditor { @UserMessage(""" You are a professional editor. Analyze and rewrite the following story to better align with the target audience of { {audience}}. Return only the story and nothing else. The story is "{ {story}}". """) @Agent("Edits a story to better fit a given audience") String editStory(@V("story") String story, @V("audience") String audience); }

2、Agent的名字,可以直接定义。也可以通过代理构建器的名称方法进行程序化

 未直接定义带有 @Agent 注释的方法名称

@Agent(name = "",description = "Edits a story to better fit a given audience",value = "",outputKey = "") String editStory(@V("story") String story, @V("audience") String audience);

name值为空,则Agent名字未editStory.

3、使用该 AgenticServices.agentBuilder() 方法构建该代理实例,指定接口和聊天模型。

CreativeWriter creativeWriter = AgenticServices .agentBuilder(CreativeWriter.class) .chatModel(myChatModel) .outputKey("story") .build();

4、AI service与 agent的区别

区别1: 本质上 agents = 多个AI services + 多个other agents

区别2: 用于指定共享变量的名称,代理调用的结果将存储在该变量中,以便同一代理系统内的其他代理使用。 输出名称也可以直接在 @Agent 注释中声明。比如:

@Agent(outputKey = "story", description = "Generates a story based on the given topic")

注:AgenticServices 类提供了一组静态工厂方法,用于创建和定义由 langchain4j-agentic 框架提供的各种代理。

5、AgenticScope存储共享变量(多个agent直接交互)

AgenticScope 还会自动注册其他相关信息,如所有代理的调用顺序及其响应。当调用代理系统的主智能体时,会自动创建,并在必要时通过回调程序提供

6、工作流Workflow模式,这些模式可以组合起来,创造更复杂的工作流程。

  • Sequential workflow  顺序工作流​

每个代理的输出作为输入传递给下一个代理。

适用场景: 当你需要按特定顺序完成一系列任务时

比如 : 针对特定受众编写一个故事

public interface AudienceEditor { @UserMessage(""" You are a professional editor. Analyze and rewrite the following story to better align with the target audience of { {audience}}. Return only the story and nothing else. The story is "{ {story}}". """) @Agent("Edits a story to better fit a given audience") String editStory(@V("story") String story, @V("audience") String audience); }

针对特定风格编写一个故事

public interface StyleEditor { @UserMessage(""" You are a professional editor. Analyze and rewrite the following story to better fit and be more coherent with the { {style}} style. Return only the
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 10:57:23

测试深度思考:从执行者到战略家的思维跃迁

在软件测试领域,我们常陷于日常的用例执行、缺陷跟踪和报告编写中,仿佛测试就是一场“找茬游戏”。然而,随着敏捷开发、DevOps和AI技术的普及,测试角色正从被动的质量检验者向主动的质量赋能者演变。深度思考,正是这一…

作者头像 李华
网站建设 2026/4/16 12:52:10

10 个AI论文工具,MBA学生高效写作必备!

10 个AI论文工具,MBA学生高效写作必备! AI 工具助力论文写作,MBA 学生的高效之选 在当今快节奏的 MBA 学习中,论文写作已成为一项重要任务。无论是案例分析、商业计划书还是研究论文,都需要学生具备高效的写作能力与…

作者头像 李华
网站建设 2026/4/16 11:05:36

Langchain-Chatchat与Istio服务网格集成:精细化流量治理

Langchain-Chatchat与Istio服务网格集成:精细化流量治理 在企业加速构建AI原生能力的今天,如何在保障数据主权的前提下,将大语言模型(LLM)系统稳定、可控地接入生产环境,成为技术团队面临的核心挑战。尤其在…

作者头像 李华
网站建设 2026/3/31 19:28:53

Langchain-Chatchat支持哪些文件格式?一文讲清输入源配置

Langchain-Chatchat 支持哪些文件格式?一文讲清输入源配置 在企业知识管理日益复杂的今天,如何让堆积如山的PDF、Word文档和内部手册“开口说话”,成了智能化落地的关键一步。很多团队尝试引入大模型来构建智能问答系统,却发现通用…

作者头像 李华
网站建设 2026/4/16 12:28:17

10 个AI论文工具,助你轻松搞定研究生论文!

10 个AI论文工具,助你轻松搞定研究生论文! AI 工具助力学术写作,轻松应对论文挑战 在研究生阶段,论文写作不仅是学术能力的体现,也是对时间与精力的巨大考验。随着人工智能技术的发展,越来越多的 AI 工具被…

作者头像 李华