25
25
import static org .bsc .langgraph4j .action .AsyncEdgeAction .edge_async ;
26
26
import static org .bsc .langgraph4j .action .AsyncNodeAction .node_async ;
27
27
28
+ /**
29
+ * Interface representing an Agent Executor.
30
+ */
28
31
public interface AgentExecutor {
29
32
33
+ /**
34
+ * Represents the state of an agent.
35
+ */
30
36
class State extends AgentState {
31
37
static Map <String , Channel <?>> SCHEMA = Map .of (
32
38
"intermediate_steps" , AppenderChannel .<IntermediateStep >of (ArrayList ::new )
33
39
);
34
40
41
+ /**
42
+ * Constructs a new State with the given initialization data.
43
+ *
44
+ * @param initData the initialization data
45
+ */
35
46
public State (Map <String , Object > initData ) {
36
47
super (initData );
37
48
}
38
49
50
+ /**
51
+ * Retrieves the input value.
52
+ *
53
+ * @return an Optional containing the input value if present
54
+ */
39
55
public Optional <String > input () {
40
56
return value ("input" );
41
57
}
58
+
59
+ /**
60
+ * Retrieves the agent outcome.
61
+ *
62
+ * @return an Optional containing the agent outcome if present
63
+ */
42
64
public Optional <AgentOutcome > agentOutcome () {
43
65
return value ("agent_outcome" );
44
66
}
67
+
68
+ /**
69
+ * Retrieves the list of intermediate steps.
70
+ *
71
+ * @return a list of intermediate steps
72
+ */
45
73
public List <IntermediateStep > intermediateSteps () {
46
74
return this .<List <IntermediateStep >>value ("intermediate_steps" ).orElseGet (ArrayList ::new );
47
75
}
48
76
}
49
77
78
+ /**
79
+ * Enum representing different serializers for the agent state.
80
+ */
50
81
enum Serializers {
51
82
52
- STD ( new STDStateSerializer () ),
53
- JSON ( new JSONStateSerializer () );
83
+ STD (new STDStateSerializer ()),
84
+ JSON (new JSONStateSerializer ());
54
85
55
86
private final StateSerializer <AgentExecutor .State > serializer ;
56
87
88
+ /**
89
+ * Constructs a new Serializers enum with the specified serializer.
90
+ *
91
+ * @param serializer the state serializer
92
+ */
57
93
Serializers (StateSerializer <AgentExecutor .State > serializer ) {
58
94
this .serializer = serializer ;
59
95
}
60
96
97
+ /**
98
+ * Retrieves the state serializer.
99
+ *
100
+ * @return the state serializer
101
+ */
61
102
public StateSerializer <AgentExecutor .State > object () {
62
103
return serializer ;
63
104
}
64
-
65
105
}
66
106
107
+ /**
108
+ * Builder class for constructing a graph of agent execution.
109
+ */
67
110
class GraphBuilder {
68
111
private StreamingChatLanguageModel streamingChatLanguageModel ;
69
112
private ChatLanguageModel chatLanguageModel ;
70
113
private final ToolNode .Builder toolNodeBuilder = ToolNode .builder ();
71
114
private StateSerializer <State > stateSerializer ;
72
115
116
+ /**
117
+ * Sets the chat language model for the graph builder.
118
+ *
119
+ * @param chatLanguageModel the chat language model
120
+ * @return the updated GraphBuilder instance
121
+ */
73
122
public GraphBuilder chatLanguageModel (ChatLanguageModel chatLanguageModel ) {
74
123
this .chatLanguageModel = chatLanguageModel ;
75
124
return this ;
76
125
}
126
+
127
+ /**
128
+ * Sets the streaming chat language model for the graph builder.
129
+ *
130
+ * @param streamingChatLanguageModel the streaming chat language model
131
+ * @return the updated GraphBuilder instance
132
+ */
77
133
public GraphBuilder chatLanguageModel (StreamingChatLanguageModel streamingChatLanguageModel ) {
78
134
this .streamingChatLanguageModel = streamingChatLanguageModel ;
79
135
return this ;
80
136
}
137
+
138
+ /**
139
+ * Sets the objects with tools for the graph builder (deprecated).
140
+ *
141
+ * @param objectsWithTools the list of objects with tools
142
+ * @return the updated GraphBuilder instance
143
+ */
81
144
@ Deprecated
82
145
public GraphBuilder objectsWithTools (List <Object > objectsWithTools ) {
83
146
objectsWithTools .forEach (toolNodeBuilder ::specification );
84
147
return this ;
85
148
}
149
+
150
+ /**
151
+ * Sets the tool specification for the graph builder.
152
+ *
153
+ * @param objectsWithTool the tool specification
154
+ * @return the updated GraphBuilder instance
155
+ */
86
156
public GraphBuilder toolSpecification (Object objectsWithTool ) {
87
- toolNodeBuilder .specification ( objectsWithTool );
157
+ toolNodeBuilder .specification (objectsWithTool );
88
158
return this ;
89
159
}
160
+
161
+ /**
162
+ * Sets the tool specification with executor for the graph builder.
163
+ *
164
+ * @param spec the tool specification
165
+ * @param executor the tool executor
166
+ * @return the updated GraphBuilder instance
167
+ */
90
168
public GraphBuilder toolSpecification (ToolSpecification spec , ToolExecutor executor ) {
91
- toolNodeBuilder .specification ( spec , executor );
169
+ toolNodeBuilder .specification (spec , executor );
92
170
return this ;
93
171
}
172
+
173
+ /**
174
+ * Sets the tool specification for the graph builder.
175
+ *
176
+ * @param toolSpecifications the tool specifications
177
+ * @return the updated GraphBuilder instance
178
+ */
94
179
public GraphBuilder toolSpecification (ToolNode .Specification toolSpecifications ) {
95
- toolNodeBuilder .specification ( toolSpecifications );
180
+ toolNodeBuilder .specification (toolSpecifications );
96
181
return this ;
97
182
}
98
183
184
+ /**
185
+ * Sets the state serializer for the graph builder.
186
+ *
187
+ * @param stateSerializer the state serializer
188
+ * @return the updated GraphBuilder instance
189
+ */
99
190
public GraphBuilder stateSerializer (StateSerializer <State > stateSerializer ) {
100
191
this .stateSerializer = stateSerializer ;
101
192
return this ;
102
193
}
103
194
195
+ /**
196
+ * Builds the state graph.
197
+ *
198
+ * @return the constructed StateGraph
199
+ * @throws GraphStateException if there is an error in the graph state
200
+ */
104
201
public StateGraph <State > build () throws GraphStateException {
105
202
106
- if ( streamingChatLanguageModel != null && chatLanguageModel != null ) {
203
+ if ( streamingChatLanguageModel != null && chatLanguageModel != null ) {
107
204
throw new IllegalArgumentException ("chatLanguageModel and streamingChatLanguageModel are mutually exclusive!" );
108
205
}
109
- if ( streamingChatLanguageModel == null && chatLanguageModel == null ) {
206
+ if ( streamingChatLanguageModel == null && chatLanguageModel == null ) {
110
207
throw new IllegalArgumentException ("a chatLanguageModel or streamingChatLanguageModel is required!" );
111
208
}
112
209
@@ -115,38 +212,39 @@ public StateGraph<State> build() throws GraphStateException {
115
212
var agent = Agent .builder ()
116
213
.chatLanguageModel (chatLanguageModel )
117
214
.streamingChatLanguageModel (streamingChatLanguageModel )
118
- .tools ( toolNode .toolSpecifications () )
215
+ .tools (toolNode .toolSpecifications ())
119
216
.build ();
120
217
121
- if ( stateSerializer == null ) {
218
+ if ( stateSerializer == null ) {
122
219
stateSerializer = Serializers .STD .object ();
123
220
}
124
221
125
- final var callAgent = new CallAgent ( agent );
126
- final var executeTools = new ExecuteTools ( agent , toolNode );
127
- final EdgeAction <State > shouldContinue = (state ) ->
222
+ final var callAgent = new CallAgent (agent );
223
+ final var executeTools = new ExecuteTools (agent , toolNode );
224
+ final EdgeAction <State > shouldContinue = (state ) ->
128
225
state .agentOutcome ()
129
- .map (AgentOutcome ::finish )
130
- .map ( finish -> "end" )
131
- .orElse ("continue" );
226
+ .map (AgentOutcome ::finish )
227
+ .map (finish -> "end" )
228
+ .orElse ("continue" );
132
229
133
230
return new StateGraph <>(State .SCHEMA , stateSerializer )
134
- .addNode ( "agent" , node_async ( callAgent ) )
135
- .addNode ( "action" , node_async ( executeTools ) )
136
- .addEdge (START ,"agent" )
231
+ .addNode ("agent" , node_async (callAgent ) )
232
+ .addNode ("action" , node_async (executeTools ) )
233
+ .addEdge (START , "agent" )
137
234
.addConditionalEdges ("agent" ,
138
- edge_async ( shouldContinue ),
235
+ edge_async (shouldContinue ),
139
236
Map .of ("continue" , "action" , "end" , END )
140
237
)
141
- .addEdge ("action" , "agent" )
142
- ;
238
+ .addEdge ("action" , "agent" );
143
239
}
144
240
}
145
241
242
+ /**
243
+ * Creates a new GraphBuilder instance.
244
+ *
245
+ * @return a new GraphBuilder
246
+ */
146
247
static GraphBuilder graphBuilder () {
147
248
return new GraphBuilder ();
148
249
}
149
-
150
250
}
151
-
152
-
0 commit comments