news 2026/5/14 0:44:42

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/5/10 0:06:14

Meta的 Mango「芒果」模型:看来AI视频生成要变天了

📌 目录扎克伯格的「芒果」要炸场!Meta神秘AI视频模型Mango曝光:4K/60帧秒出片,好莱坞都要慌了一、Meta的野心:不止超越Sora,要把AI视频搬进专业片场(一)Mango vs 现有AI视频工具&am…

作者头像 李华
网站建设 2026/5/13 9:23:24

moodycamel::ConcurrentQueue 清空队列的方法论

方法1&#xff1a;循环弹出元素 #include <concurrentqueue.h>// 如果存储的是指针类型 moodycamel::ConcurrentQueue<int*> queue; int* item nullptr; while (queue.try_dequeue(item)) {if (item) {delete item; // 如果需要释放内存item nullptr;} }// 如果…

作者头像 李华
网站建设 2026/5/10 22:13:57

halcon窗口显示文字

前言 我们在开发C#上位机的时候&#xff0c;有时候会使用Halcon控件&#xff0c;在Halcon控件上会有绘制文字&#xff0c;本文就来介绍如何实现。 Halcon代码实现 dev_close_window () dev_open_window (0, 0, 512, 512, black, WindowHandle) set_font (WindowHandle, 宋体…

作者头像 李华
网站建设 2026/5/13 20:15:09

Langchain-Chatchat错误排查手册:常见问题与解决方案

Langchain-Chatchat 错误排查手册&#xff1a;常见问题与解决方案 在企业级 AI 应用日益强调数据隐私和本地化部署的今天&#xff0c;基于大型语言模型&#xff08;LLM&#xff09;的知识库系统正从“云端调用”转向“私有可控”。Langchain-Chatchat 作为一款开源、可离线运行…

作者头像 李华
网站建设 2026/5/9 21:05:36

AtCoder Beginner Contest竞赛题解 | 洛谷 AT_abc436_b Magic Square

​欢迎大家订阅我的专栏&#xff1a;算法题解&#xff1a;C与Python实现&#xff01; 本专栏旨在帮助大家从基础到进阶 &#xff0c;逐步提升编程能力&#xff0c;助力信息学竞赛备战&#xff01; 专栏特色 1.经典算法练习&#xff1a;根据信息学竞赛大纲&#xff0c;精心挑选…

作者头像 李华