4
4
import dev .langchain4j .data .message .AiMessage ;
5
5
import dev .langchain4j .model .chat .StreamingChatLanguageModel ;
6
6
import dev .langchain4j .model .output .Response ;
7
+ import dev .langchain4j .service .tool .ToolExecutor ;
7
8
import lombok .extern .slf4j .Slf4j ;
8
9
import dev .langchain4j .data .message .ToolExecutionResultMessage ;
9
10
import dev .langchain4j .model .chat .ChatLanguageModel ;
@@ -49,7 +50,7 @@ public StateSerializer<AgentExecutor.State> object() {
49
50
public class GraphBuilder {
50
51
private StreamingChatLanguageModel streamingChatLanguageModel ;
51
52
private ChatLanguageModel chatLanguageModel ;
52
- private List < Object > objectsWithTools ;
53
+ private ToolNode . Builder toolNodeBuilder = ToolNode . builder () ;
53
54
private StateSerializer <State > stateSerializer ;
54
55
55
56
public GraphBuilder chatLanguageModel (ChatLanguageModel chatLanguageModel ) {
@@ -60,8 +61,21 @@ public GraphBuilder chatLanguageModel(StreamingChatLanguageModel streamingChatLa
60
61
this .streamingChatLanguageModel = streamingChatLanguageModel ;
61
62
return this ;
62
63
}
64
+ @ Deprecated
63
65
public GraphBuilder objectsWithTools (List <Object > objectsWithTools ) {
64
- this .objectsWithTools = objectsWithTools ;
66
+ objectsWithTools .forEach ( o -> toolNodeBuilder .specification ( o ) );
67
+ return this ;
68
+ }
69
+ public GraphBuilder toolSpecification (Object objectsWithTool ) {
70
+ toolNodeBuilder .specification ( objectsWithTool );
71
+ return this ;
72
+ }
73
+ public GraphBuilder toolSpecification (ToolSpecification spec , ToolExecutor executor ) {
74
+ toolNodeBuilder .specification ( spec , executor );
75
+ return this ;
76
+ }
77
+ public GraphBuilder toolSpecification (ToolNode .Specification toolSpecifications ) {
78
+ toolNodeBuilder .specification ( toolSpecifications );
65
79
return this ;
66
80
}
67
81
@@ -71,22 +85,20 @@ public GraphBuilder stateSerializer( StateSerializer<State> stateSerializer) {
71
85
}
72
86
73
87
public StateGraph <State > build () throws GraphStateException {
74
- Objects . requireNonNull ( objectsWithTools , "objectsWithTools is required!" );
88
+
75
89
if ( streamingChatLanguageModel != null && chatLanguageModel != null ) {
76
90
throw new IllegalArgumentException ("chatLanguageModel and streamingChatLanguageModel are mutually exclusive!" );
77
91
}
78
92
if ( streamingChatLanguageModel == null && chatLanguageModel == null ) {
79
93
throw new IllegalArgumentException ("a chatLanguageModel or streamingChatLanguageModel is required!" );
80
94
}
81
95
82
- var toolNode = ToolNode .of ( objectsWithTools );
83
-
84
- final List <ToolSpecification > toolSpecifications = toolNode .toolSpecifications ();
96
+ final var toolNode = toolNodeBuilder .build ();
85
97
86
98
var agentRunnable = Agent .builder ()
87
99
.chatLanguageModel (chatLanguageModel )
88
100
.streamingChatLanguageModel (streamingChatLanguageModel )
89
- .tools ( toolSpecifications )
101
+ .tools ( toolNode . toolSpecifications () )
90
102
.build ();
91
103
92
104
if ( stateSerializer == null ) {
0 commit comments