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

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

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

xml 复制代码
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-flow</artifactId>
    <version>最新版本</version>
</dependency>

主要特点有:

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

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

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

yaml 复制代码
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

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

java 复制代码
@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、比较原始的风格(能表达内在的大概过程):

yaml 复制代码
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 组件参与。可以像低代码一样(或可执行程序一样),直接运行配置文件。

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;
    }
}
相关推荐
葫芦和十三5 小时前
图解 MongoDB 21|选举与 failover:Primary 是怎么选出来的
后端·mongodb·agent
GetcharZp6 小时前
26k Star 开源内网穿透神器 NetBird,一分钟实现全球设备互联!
后端
考虑考虑6 小时前
Mybatis实现批量插入
java·后端·mybatis
咖啡八杯7 小时前
GoF设计模式——中介者模式
java·后端·spring·设计模式
lizhongxuan9 小时前
多Agent之间的区别
后端
青石路11 小时前
记一次多JDK版本问题的排查,一坑套一坑,差点没爬上来
java
杨充11 小时前
1.面向对象设计思想
后端
IT_陈寒12 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
systemPro12 小时前
2.6亿条设备数据,历史查询从超时到50ms,我做了什么
后端
要阿尔卑斯吗12 小时前
提示词优化启示:为什么“按顺序输出“比“关键度评分“更有效
后端