news 2026/7/7 6:14:20

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/7/4 10:27:25

NPM_配置的补充说明

原来的registry.npm.taobao.org已替换为registry.npmmirror.com npm config set registry https://registry.npmmirror.com确认配置已经生效 npm config get registry若需要恢复默认的官方源,可以执行以下命令 npm config set registry https://registry.npmjs.o…

作者头像 李华
网站建设 2026/7/5 20:16:10

halcon窗口显示带有箭头的直线

前言 我们在开发C#上位机的时候,有时候会使用Halcon控件,在Halcon控件上会有绘制带有箭头的直线的需求,本文就来介绍如何实现。 Halcon代码实现 dev_close_window () dev_open_window (0, 0, 512, 512, black, WindowHandle) disp_arrow (…

作者头像 李华
网站建设 2026/7/7 4:28:35

Langchain-Chatchat结合GPU加速推理,实现高性能问答服务

Langchain-Chatchat 结合 GPU 加速推理,打造高性能本地问答系统 在企业知识管理日益复杂的今天,如何让员工快速获取分散在成百上千份文档中的关键信息,已成为组织效率提升的瓶颈。一个常见的场景是:新员工想了解公司的差旅报销标准…

作者头像 李华
网站建设 2026/7/6 18:00:21

研究生必备:9款AI论文神器,真实文献交叉引用,一键生成文献综述

如果你是正在熬夜赶Deadline的毕业生,面对堆积如山的文献资料却无从下笔;或是面临延毕压力,被导师催稿催得焦头烂额的研究生;又或是没钱去支付高昂知网查重费用的大学生,别担心,这篇文章就是为你量身打造的…

作者头像 李华
网站建设 2026/7/6 13:08:02

2025中国iPaaS市场份额独立第一测评小白快速上手方法与步骤

《2025中国iPaaS行业发展白皮书》明确指出,企业集成平台优势明显已成为数智化转型的核心支撑。《2025中国iPaaS产品权威测评》通过对20主流平台的技术能力、用户体验、市场覆盖等维度评估,结合《2025中国iPaaS产品排行榜》数据,连趣云iPaaS平…

作者头像 李华