Skip to main content
Note: The former Spring Boot example module agentscope-examples/multiagent-patterns/ was removed during the 2.0 package refactor. Use the code snippets on this page as the reference implementation. For other runnable samples, see agentscope-examples/documentation/.
The pipeline example uses Spring AI Alibaba flow agents (SequentialAgent, ParallelAgent, LoopAgent) with AgentScopeAgent sub-agents and AgentScope DashScopeChatModel (Model). Each pipeline is built from ReActAgent-based AgentScopeAgents and invoked via PipelineService.

Prerequisites

  • JDK 17+
  • Maven 3.6+
  • DashScope API key: export AI_DASHSCOPE_API_KEY=your-key or set spring.ai.dashscope.api-key in application.yml

Model configuration

The example uses a single Model bean (DashScopeChatModel) shared by all pipeline sub-agents:

1. SequentialAgent: natural language → SQL → score

Scenario: User describes a query in natural language. The pipeline (1) SQL Generator converts it to MySQL SQL, (2) SQL Rater scores how well the SQL matches user intent (0–1). Sub-agents run in sequence; each output feeds the next. Example input: “List all orders from the last 30 days with total amount greater than 500.”

Implementation

  • SQL Generator: instruction("{input}"), outputKey("sql") — receives user input, writes generated SQL into state key sql.
  • SQL Rater: instruction("... {sql} ... {input} ..."), outputKey("score") — receives previous sql and original input, writes score into state key score.

2. ParallelAgent: multi-angle research

Scenario: User provides a topic; the pipeline researches it from three angles in parallel: technology, finance/business, and market/industry. Results are merged into a single report (research_report). Example input: “Research the current state of large language models.” (Demo uses “AI agents in enterprise software”.)

Implementation

  • Each sub-agent has instruction("Research the following topic: {input}.") and its own outputKey (tech_analysis, finance_analysis, market_analysis).
  • DefaultMergeStrategy merges sub-agent outputs into one; merged result is written to mergeOutputKey research_report.

3. LoopAgent: SQL refinement until quality threshold

Scenario: Generate SQL from natural language and iteratively refine until the quality score exceeds 0.5. Each iteration runs an inner SequentialAgent: SQL Generator → SQL Rater. Loop continues until score > 0.5 or max iterations. Example input: “Find customers who placed more than 3 orders in 2024.”

Implementation

LoopPipelineConfig builds the same SQL Generator and SQL Rater AgentScopeAgents as SequentialPipelineConfig (same prompts, instruction, outputKey). It then wraps them in a SequentialAgent and that in a LoopAgent with a condition-based loop strategy:
  • LoopAgent wraps a single SequentialAgent (sql_agent) that runs SQL Generator then SQL Rater each iteration.
  • loopStrategy: LoopMode.condition(...) — receives the last turn’s messages, reads the last message (rater output), parses it as a number; returns true when score > 0.5 to stop the loop.

Invoking pipelines: PipelineService

PipelineService is wired with the three agents and exposes runSequential, runParallel, and runLoop. Each method invokes the corresponding agent with a string input and returns a result record.
Result types: Service implementation (extract of how results are read from state):

Optional demo runner

When pipeline.runner.enabled=true, PipelineCommandRunner runs a demo for each pipeline on startup with sample inputs and logs the results:

Configuration

  • Routing - Classify and route to specialist agents
  • MsgHub - Message broadcasting for multi-agent conversations
  • Handoffs - State-driven routing and transfer between agents
  • Multi-Agent Debate - Debate workflow pattern