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;
    }
}
相关推荐
有什么东东1 分钟前
山东大学软件学院创新项目实训开发日志(20)之中医知识问答自动生成对话标题bug修改
java·vue·bug·springboot
问道财经9 分钟前
拆解华为Pura X新发现:“仿生”散热与钛合金“骨架”
后端·restful
打死不学Java代码10 分钟前
PaginationInnerInterceptor使用(Mybatis-plus分页)
android·java·mybatis
南客先生12 分钟前
海量聊天消息处理:ShardingJDBC分库分表、ClickHouse冷热数据分离、ES复合查询方案、Flink实时计算与SpringCloud集成
java·clickhouse·elasticsearch·flink·springcloud·shardingjdbc
Asthenia041213 分钟前
TCP粘包问题及其解决方案(Java实现)
后端
w236173460119 分钟前
Tomcat:从零理解Java Web应用的“心脏”
java·前端·tomcat
yuren_xia24 分钟前
示例:Spring JDBC编程式事务
java·后端·spring
uhakadotcom29 分钟前
企业智能体网络(Agent Mesh)入门指南:基础知识与实用示例
后端·面试·github
用户6133467165332 分钟前
开发体育赛事直播系统:用户管理机制与内容审核技术实现方案
后端
陈大大陈39 分钟前
基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
java·linux·开发语言·数据结构·c++·算法·缓存