您现在的位置是:首页 > 文章详情

🔥 Java AI(智能体)编排开发就用 Solon Flow

日期:2025-04-25点击:5

本例参考 dify 的 chatFlow 的效果,模拟实现视频内容:

Solon Flow 是一个通用流编排引擎。可用于计算(或任务)的编排场景; 可用于业务规则和决策处理型的编排场景; 可用于办公审批型(有状态、可中断,人员参与)的编排场景; 可用于长时间流程(结合自动前进,等待介入)的编排场景。同时支持:java8,java11,java17,java21,java24。

 <dependency> <groupId>org.noear</groupId> <artifactId>solon-flow</artifactId> <version>最新版本</version> </dependency> 

主要特点有:

  • 使用 yaml 格式做编排
  • 表达式与脚本自由
  • 元信息配置,为扩展提供了无限空间(每个流程,相当于自带了元数据库)
  • 事件广播与回调支持
  • 支持“无状态”、“有状态”两种需求分类
  • 驱动定制(是像 JDBC 有 MySql, PostgreSQL,还可能有 Elasticsearch)

下面提供两种处编排风格以可供参考

1、使用 “元信息” + 任务组件“ 风格 (更利于可视界面配置)

 id: demo1 layout: - title: "开始" type: start - title: "文件提取" meta.input: "file" # 可视界面的配置(通过元信息表示) meta.output: "fileTxt" task: @FileLoaderCom - title: "LLM" meta.model: "Qwen/Qwen2.5-72B-Instruct" # 可视界面的配置(通过元信息表示) meta.input: "fileTxt" meta.messages: - role: system content: "#角色\n你是一个数据专家,删除数据的格式整理和转换\n\n#上下文\n${fileTxt}\n\n#任务\n提取csv格式的字符串" task: @ChatModelCom - title: "参数提取器" meta.model: "Qwen/Qwen2.5-72B-Instruct" # 可视界面的配置(通过元信息表示) meta.output: "csvData" task: @ParamExtractionCom - title: "执行代码" meta.input: "csvData" task: | import com.demo.DataUtils;  String json = DataUtils.csvToJson(node.meta().get("meta.input")); //转为 json 数据 String echatCode = DataUtils.jsonAsEchatCode(json); //转为 echat 图表代码 context.result = echatCode; //做为结果返回 - title: "结束" type: end 

这种风格,更适合可视界面的编译。设计是,可以预选设计好很多组件,经过管理配置后,可提供界面选择。

 @Component("FileLoaderCom") public class FileLoaderCom implements TaskComponent { @Override public void run(FlowContext context, Node node) throws Throwable { ... } } @Component("ChatModelCom") public class ChatModelCom implements TaskComponent { @Override public void run(FlowContext context, Node node) throws Throwable { ... } } @Component("ParamExtractionCom") public class ParamExtractionCom implements TaskComponent { @Override public void run(FlowContext context, Node node) throws Throwable { ... } } @Controller public class DemoController { @Mapping("demo") public Object input(UploadedFile attachment, String message) throws Throwable { FlowEngine flowEngine = FlowEngine.newInstance(); flowEngine.load("classpath:flow/demo1.chain.yml"); FlowContext ctx = new FlowContext(); ctx.put("file", attachment); flowEngine.eval("demo1"); return context.result; } } 

2、比较原始的风格(能表达内在的大概过程):

 id: demo1 layout: - title: "开始" type: start - title: "文件提取" meta.input: "file" # 可视界面的配置(通过元信息表示) meta.output: "fileTxt" task: | import org.noear.solon.ai.loader.*;  var loader = FileLoader.of(file); var fileTxt = loader.load(); context.put(node.meta().get("meta.output"), fileTxt); //推入上下文(后续节点可用) - title: "LLM" meta.model: "Qwen/Qwen2.5-72B-Instruct" # 可视界面的配置(通过元信息表示) meta.input: "fileTxt" meta.messages: - role: system content: "#角色\n你是一个数据专家,删除数据的格式整理和转换\n\n#上下文\n${fileTxt}\n\n#任务\n提取csv格式的字符串" task: | import com.demo.ModelUtils; //根据业务封装,可快速获取配置的模型 import com.demo.MessageUtils; //根据业务封装,可快速构建消息 var chatModel = ModelUtils.get(node.meta().get("model")); var chatMessages = MessageUtils.get(node.meta().get("messages"), context); var resp = chatModel.prompt(chatMessages).call(); context.put("resp", resp);  - title: "参数提取器" meta.model: "Qwen/Qwen2.5-72B-Instruct" # 可视界面的配置(通过元信息表示) meta.output: "csvData" task: | context.put(node.meta().get("meta.output"), resp.getMessage().getContent());  - title: "执行代码" meta.input: "csvData" task: | import com.demo.DataUtils;  String json = DataUtils.csvToJson(node.meta().get("meta.input")); //转为 json 数据 String echatCode = DataUtils.jsonAsEchatCode(json); //转为 echat 图表代码 context.result = echatCode; //做为结果返回 - title: "结束" type: end 

这个风格比较原始,不过不需要 java 组件参与。可以像低代码一样(或可执行程序一样),直接运行配置文件。

 @Controller public class DemoController { @Mapping("demo") public Object input(UploadedFile attachment, String message) throws Throwable { FlowEngine flowEngine = FlowEngine.newInstance(); flowEngine.load("classpath:flow/demo1.chain.yml"); FlowContext ctx = new FlowContext(); ctx.put("file", attachment); flowEngine.eval("demo1"); return context.result; } }
原文链接:https://www.oschina.net/news/346475
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章