news 2026/2/3 8:26:36

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/2/2 12:02:47

华为网络设备基本配置命令

1、恢复出出厂设置<Huawei>reset saved-configuration This will delete the configuration in the flash memory.The device configuratio ns will be erased to reconfigure.Are you sure? (y/n)[n]:yClear the configuration in the device successfully. <Huawe…

作者头像 李华
网站建设 2026/1/30 23:00:45

志同道合交友网站毕业论文+PPT(附源代码+演示视频)

文章目录志同道合交友网站一、项目简介&#xff08;源代码在文末&#xff09;1.运行视频2.&#x1f680; 项目技术栈3.✅ 环境要求说明4.包含的文件列表&#xff08;含论文&#xff09;数据库结构与测试用例系统功能结构后台运行截图项目部署源码下载志同道合交友网站 如需其他…

作者头像 李华
网站建设 2026/2/2 10:35:58

【Java 25 LTS六大核心特性】

Java 25 LTS 深度拆解&#xff1a;改变开发范式的六大核心特性 基本类型模式匹配&#xff08;JEP 507&#xff09; 模式匹配简化了类型检查和转换&#xff0c;减少冗余代码。例如&#xff1a; if (obj instanceof String s) { System.out.println(s.toLowerCase()); } 基…

作者头像 李华
网站建设 2026/2/1 13:36:13

Langchain-Chatchat助力医疗文档智能检索与问答

Langchain-Chatchat助力医疗文档智能检索与问答 在一家三甲医院的早交班会议上&#xff0c;一位年轻医生急切地翻找《KDIGO慢性肾病临床实践指南》第47页的内容——关于三期患者使用ACEI类药物的禁忌证。他花了七分钟才从PDF目录中定位到相关章节。而就在同一时刻&#xff0c;…

作者头像 李华
网站建设 2026/1/29 22:54:06

Langchain-Chatchat如何实现文档相似度比对?查重与去重依据

Langchain-Chatchat 如何实现文档相似度比对&#xff1f;查重与去重依据 在企业知识库日益膨胀的今天&#xff0c;一个看似简单却影响深远的问题浮出水面&#xff1a;为什么我上传了十份几乎一模一样的项目报告&#xff0c;系统还在一遍遍地索引、存储、检索&#xff1f; 这不仅…

作者头像 李华
网站建设 2026/2/1 16:41:52

java学习--String和StringBuffer互转

在 Java 中&#xff0c;String 是不可变字符串&#xff0c;StringBuffer 是可变字符串&#xff08;线程安全&#xff09;&#xff0c;两者的相互转换是开发中常见操作&#xff0c;以下是具体实现方式、示例及注意事项&#xff1a;一、String 转 StringBuffer有两种核心方式&…

作者头像 李华