Solon v3.3.1

aiflow - 编排应用示例

</> markdown

编排语法为 solon-flow 语法。支持使用 solon-flow 编辑器:

1、聊天应用编排

智应用编排

id: chat1
layout:
  - task: "@WebInput"
  - task: "@ChatModel"
    meta:
      systemPrompt: "你是个聊天助手"
      stream: false
      chatConfig: # "@type": "org.noear.solon.ai.chat.ChatConfig"
        provider: "ollama"
        model: "qwen2.5:1.5b"
        apiUrl: "http://127.0.0.1:11434/api/chat"
  - task: "@WebOutput"

对接 Web 程序

@Controller
public class DemoController {
    @Inject
    StatefulFlowEngine flowEngine;

    Map<String, ChatSession> chatSessionMap = new ConcurrentHashMap<>();

    @Produces(MimeType.TEXT_EVENT_STREAM_VALUE)
    @Mapping("chat")
    public void chat(Context ctx) throws Exception {
        FlowContext flowContext = new FlowContext();

        //保存会话记录
        ChatSession chatSession = chatSessionMap.computeIfAbsent(ctx.sessionId(), k -> new ChatSessionDefault(ctx.sessionId()));
        flowContext.put(Attrs.CTX_CHAT_SESSION, chatSession);

        flowEngine.eval("chat1", flowContext);
    }
}

2、使用 MCP 应用编排

智应用编排

id: mcp1
layout:
  - task: "@VarInput"
    meta:
      message: "杭州今天天气怎么样?"
  - task: "@ChatModel"
    meta:
      systemPrompt: "你是个天气预报员"
      stream: false
      chatConfig: # "@type": "org.noear.solon.ai.chat.ChatConfig"
        provider: "ollama"
        model: "qwen2.5:1.5b"
        apiUrl: "http://127.0.0.1:11434/api/chat"
      mcpServers:
        weather:
          url: "http://127.0.0.1:8080/mcp/sse"
  - task: "@ConsoleOutput"

单元测试

@SolonTest(DemoApp.class) //会启动一个 mcpServer 服务
public class McpTest extends HttpTester {
    @Inject
    FlowEngine flowEngine;

    @Test
    public void test() {
        flowEngine.eval("mcp1");
    }
}

3、 RAG 智能体应用编排

智应用编排

id: rag1
layout:
  - task: "@VarInput"
    meta:
      message: "Solon 是谁开发的?"
  - task: "@EmbeddingModel"
    meta:
      embeddingConfig: # "@type": "org.noear.solon.ai.embedding.EmbeddingConfig"
        provider: "ollama"
        model: "bge-m3"
        apiUrl: "http://127.0.0.1:11434/api/embed"
  - task: "@InMemoryRepository"
    meta:
      documentSources:
        - "https://solon.noear.org/article/about?format=md"
      splitPipeline:
        - "org.noear.solon.ai.rag.splitter.RegexTextSplitter"
        - "org.noear.solon.ai.rag.splitter.TokenSizeTextSplitter"
  - task: "@ChatModel"
    meta:
      systemPrompt: "你是个知识库"
      stream: false
      chatConfig: # "@type": "org.noear.solon.ai.chat.ChatConfig"
        provider: "ollama"
        model: "qwen2.5:1.5b"
        apiUrl: "http://127.0.0.1:11434/api/chat"
  - task: "@ConsoleOutput"

单元测试

@SolonTest(DemoApp.class) //会启动一个 mcpServer 服务
public class McpTest extends HttpTester {
    @Inject
    FlowEngine flowEngine;

    @Test
    public void test() {
        flowEngine.eval("rag1");
    }
}

3、多智能体对话应用编排(讲相声)

智应用编排

id: vs1
layout:
  - type: "start"
  - task: "@VarInput"
    meta:
      message: "你好"
  - task: "@ChatModel"
    id: model_a
    meta:
      systemPrompt: "你是一个智能体名字叫“阿飞”。将跟另一个叫“阿紫”的智能体,表演相声式吵架。每句话不要超过50个字"
      stream: false
      chatSession: "A"
      chatConfig: # "@type": "org.noear.solon.ai.chat.ChatConfig"
        provider: "ollama"
        model: "qwen2.5:1.5b"
        apiUrl: "http://127.0.0.1:11434/api/chat"
  - task: "@ConsoleOutput"
    meta:
      format: "阿飞:#{message}"
  - task: "@ChatModel"
    id: model_b
    meta:
      systemPrompt: "你是一个智能体名字叫“阿紫”。将跟另一个叫“阿飞”的智能体,表演相声式吵架。每句话不要超过50个字"
      stream: false
      chatSession: "B"
      chatConfig: # "@type": "org.noear.solon.ai.chat.ChatConfig"
        provider: "ollama"
        model: "qwen2.5:1.5b"
        apiUrl: "http://127.0.0.1:11434/api/chat"
  - task: "@ConsoleOutput"
    meta:
      format: "阿紫:#{message}"
  - type: "exclusive"
    link:
      - nextId: model_a
        condition: 'context.counter().incr("demo") < 5'

单元测试

@SolonTest(DemoApp.class) 
public class McpTest extends HttpTester {
    @Inject
    FlowEngine flowEngine;

    @Test
    public void test() {
        flowEngine.eval("vs1");
    }
}