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

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

  • https://www.toutiao.com/video/7455114080131482152/ Solon Flow 是一个通用流编排引擎。可用于计算(或任务)的编排场景; 可用于业务规则和决策处理型的编排场景; 可用于办公审批型(有状态、可中断,人员参与)的编排场景; 可用于长时间流程(结合自动前进,等待介入)的编排场景。同时支持:java8,java11,java17,java21,java24。
<groupid>org.noear</groupid>
    <artifactid>solon-flow</artifactid>
    <version>最新版本</version>

主要特点有:

  • 使用 yaml 格式做编排

  • 表达式与脚本自由

  • 元信息配置,为扩展提供了无限空间(每个流程,相当于自带了元数据库)

  • 事件广播与回调支持

  • 支持“无状态”、“有状态”两种需求分类

  • 驱动定制(是像 JDBC 有 MySql, PostgreSQL,还可能有 Elasticsearch) 下面提供两种处编排风格以可供参考

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

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

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

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

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

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

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

@Controller
public class DemoController {
    @Mapping(&#34;demo&#34;)
    public Object input(UploadedFile attachment, String message) throws Throwable {
        FlowEngine flowEngine = FlowEngine.newInstance();
        flowEngine.load(&#34;classpath:flow/demo1.chain.yml&#34;);
FlowContext ctx  = new FlowContext();
        ctx.put(&#34;file&#34;, attachment);
flowEngine.eval(&#34;demo1&#34;);
return context.result;
    }
}