13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
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 ;
22
17
23
18
import com .alibaba .cloud .ai .graph .CompiledGraph ;
24
19
import com .alibaba .cloud .ai .graph .OverAllState ;
25
20
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 ;
27
24
import com .alibaba .cloud .ai .graph .exception .GraphRunnerException ;
28
25
import com .alibaba .cloud .ai .graph .exception .GraphStateException ;
29
-
30
26
import org .springframework .ai .chat .model .ChatModel ;
31
27
32
- import static com .alibaba .cloud .ai .graph .StateGraph .END ;
28
+ import java .util .Map ;
29
+ import java .util .Optional ;
33
30
34
31
public class LlmRoutingAgent extends FlowAgent {
35
32
@@ -48,51 +45,10 @@ public Optional<OverAllState> invoke(Map<String, Object> input) throws GraphStat
48
45
return compiledGraph .invoke (input );
49
46
}
50
47
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
- */
72
48
@ 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 );
96
52
}
97
53
98
54
public static LlmRoutingAgentBuilder builder () {
0 commit comments