本例参考 dify 的 chatFlow 的效果,模拟实现视频内容:
- https://www.toutiao.com/video/7455114080131482152/
Solon Flow 是一个通用流编排引擎。可用于计算(或任务)的编排场景; 可用于业务规则和决策处理型的编排场景; 可用于办公审批型(有状态、可中断,人员参与)的编排场景; 可用于长时间流程(结合自动前进,等待介入)的编排场景。同时支持:java8,java11,java17,java21,java24。- <dependency>
- <groupId>org.noear</groupId>
- 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;
- }
- }
复制代码 来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |