Skip to content

Commit 667fe5f

Browse files
Aias00chenjingyangANTA
authored andcommitted
feat(graph): refactor agent api (alibaba#2234)
1 parent 60d2135 commit 667fe5f

26 files changed

+1961
-565
lines changed

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/BuilderExample.java

Lines changed: 0 additions & 131 deletions
This file was deleted.
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.alibaba.cloud.ai.graph.agent.flow;
16+
package com.alibaba.cloud.ai.graph.agent.flow.agent;
1717

1818
import java.util.List;
1919

@@ -24,9 +24,9 @@
2424
import com.alibaba.cloud.ai.graph.action.AsyncNodeAction;
2525
import com.alibaba.cloud.ai.graph.agent.BaseAgent;
2626
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
27+
import com.alibaba.cloud.ai.graph.agent.flow.builder.FlowGraphBuilder;
2728
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
2829

29-
import static com.alibaba.cloud.ai.graph.StateGraph.START;
3030
import static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;
3131

3232
public abstract class FlowAgent extends BaseAgent {
@@ -54,20 +54,26 @@ protected FlowAgent(String name, String description, String outputKey, String in
5454
}
5555

5656
protected StateGraph initGraph() throws GraphStateException {
57-
StateGraph graph = new StateGraph(this.name(), keyStrategyFactory);
58-
59-
// add root agent
60-
graph.addNode(this.name(), node_async(new TransparentNode(this.outputKey, this.inputKey)));
61-
62-
// add starting edge
63-
graph.addEdge(START, this.name());
64-
// Use recursive method to add all sub-agents
65-
processSubAgents(graph, this, this.subAgents());
66-
67-
return graph;
57+
// Use FlowGraphBuilder to construct the graph
58+
FlowGraphBuilder.FlowGraphConfig config = FlowGraphBuilder.FlowGraphConfig.builder()
59+
.name(this.name())
60+
.keyStrategyFactory(keyStrategyFactory)
61+
.rootAgent(this)
62+
.subAgents(this.subAgents());
63+
64+
// Delegate to specific graph builder based on agent type
65+
return buildSpecificGraph(config);
6866
}
6967

70-
protected abstract void processSubAgents(StateGraph graph, BaseAgent parentAgent, List<BaseAgent> subAgents)
68+
/**
69+
* Abstract method for subclasses to specify their graph building strategy. This
70+
* method should be implemented by concrete FlowAgent subclasses to define how their
71+
* specific graph structure should be built.
72+
* @param config the graph configuration
73+
* @return the constructed StateGraph
74+
* @throws GraphStateException if graph construction fails
75+
*/
76+
protected abstract StateGraph buildSpecificGraph(FlowGraphBuilder.FlowGraphConfig config)
7177
throws GraphStateException;
7278

7379
@Override
Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.alibaba.cloud.ai.graph.agent.flow;
17-
18-
import java.util.HashMap;
19-
import java.util.List;
20-
import java.util.Map;
21-
import java.util.Optional;
16+
package com.alibaba.cloud.ai.graph.agent.flow.agent;
2217

2318
import com.alibaba.cloud.ai.graph.CompiledGraph;
2419
import com.alibaba.cloud.ai.graph.OverAllState;
2520
import com.alibaba.cloud.ai.graph.StateGraph;
26-
import com.alibaba.cloud.ai.graph.agent.BaseAgent;
21+
import com.alibaba.cloud.ai.graph.agent.flow.builder.FlowAgentBuilder;
22+
import com.alibaba.cloud.ai.graph.agent.flow.builder.FlowGraphBuilder;
23+
import com.alibaba.cloud.ai.graph.agent.flow.enums.FlowAgentEnum;
2724
import com.alibaba.cloud.ai.graph.exception.GraphRunnerException;
2825
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
29-
3026
import org.springframework.ai.chat.model.ChatModel;
3127

32-
import static com.alibaba.cloud.ai.graph.StateGraph.END;
28+
import java.util.Map;
29+
import java.util.Optional;
3330

3431
public class LlmRoutingAgent extends FlowAgent {
3532

@@ -48,51 +45,10 @@ public Optional<OverAllState> invoke(Map<String, Object> input) throws GraphStat
4845
return compiledGraph.invoke(input);
4946
}
5047

51-
// protected StateGraph initGraph() throws GraphStateException {
52-
// StateGraph graph = new StateGraph(this.name(), keyStrategyFactory);
53-
//
54-
// // add root agent
55-
// graph.addNode(this.name(), node_async(LlmNode.builder().(this.outputKey,
56-
// this.inputKey)));
57-
//
58-
// // add starting edge
59-
// graph.addEdge(START, this.name());
60-
// // Use recursive method to add all sub-agents
61-
// processSubAgents(graph, this, this.subAgents());
62-
//
63-
// return graph;
64-
// }
65-
66-
/**
67-
* Recursively adds sub-agents and their nested sub-agents to the graph
68-
* @param graph the StateGraph to add nodes and edges to
69-
* @param parentAgent the name of the parent node
70-
* @param subAgents the list of sub-agents to process
71-
*/
7248
@Override
73-
protected void processSubAgents(StateGraph graph, BaseAgent parentAgent, List<BaseAgent> subAgents)
74-
throws GraphStateException {
75-
Map<String, String> edgeRoutingMap = new HashMap<>();
76-
for (BaseAgent subAgent : subAgents) {
77-
// Add the current sub-agent as a node
78-
graph.addNode(subAgent.name(), subAgent.asAsyncNodeAction(parentAgent.outputKey(), subAgent.outputKey()));
79-
// graph.addEdge(parentAgent.name(), subAgent.name());
80-
edgeRoutingMap.put(subAgent.name(), subAgent.name());
81-
82-
// Recursively process this sub-agent's sub-agents if they exist
83-
if (subAgent instanceof FlowAgent subFlowAgent) {
84-
if (subFlowAgent.subAgents() == null || subFlowAgent.subAgents().isEmpty()) {
85-
graph.addEdge(subAgent.name(), END);
86-
}
87-
}
88-
else {
89-
graph.addEdge(subAgent.name(), END);
90-
}
91-
}
92-
93-
// Connect parent to this sub-agent
94-
graph.addConditionalEdges(parentAgent.name(), new RoutingEdgeAction(chatModel, this, subAgents),
95-
edgeRoutingMap);
49+
protected StateGraph buildSpecificGraph(FlowGraphBuilder.FlowGraphConfig config) throws GraphStateException {
50+
config.setChatModel(this.chatModel);
51+
return FlowGraphBuilder.buildGraph(FlowAgentEnum.ROUTING.getType(), config);
9652
}
9753

9854
public static LlmRoutingAgentBuilder builder() {

0 commit comments

Comments
 (0)