Skip to content

Commit e463bfe

Browse files
committed
feat(AgentExecutor): allow to use dynamic tool definirion
1 parent 9e2c92a commit e463bfe

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

samples/agent-executor/src/main/java/org/bsc/langgraph4j/agentexecutor/AgentExecutor.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.langchain4j.data.message.AiMessage;
55
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
66
import dev.langchain4j.model.output.Response;
7+
import dev.langchain4j.service.tool.ToolExecutor;
78
import lombok.extern.slf4j.Slf4j;
89
import dev.langchain4j.data.message.ToolExecutionResultMessage;
910
import dev.langchain4j.model.chat.ChatLanguageModel;
@@ -49,7 +50,7 @@ public StateSerializer<AgentExecutor.State> object() {
4950
public class GraphBuilder {
5051
private StreamingChatLanguageModel streamingChatLanguageModel;
5152
private ChatLanguageModel chatLanguageModel;
52-
private List<Object> objectsWithTools;
53+
private ToolNode.Builder toolNodeBuilder = ToolNode.builder();
5354
private StateSerializer<State> stateSerializer;
5455

5556
public GraphBuilder chatLanguageModel(ChatLanguageModel chatLanguageModel) {
@@ -60,8 +61,21 @@ public GraphBuilder chatLanguageModel(StreamingChatLanguageModel streamingChatLa
6061
this.streamingChatLanguageModel = streamingChatLanguageModel;
6162
return this;
6263
}
64+
@Deprecated
6365
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 );
6579
return this;
6680
}
6781

@@ -71,22 +85,20 @@ public GraphBuilder stateSerializer( StateSerializer<State> stateSerializer) {
7185
}
7286

7387
public StateGraph<State> build() throws GraphStateException {
74-
Objects.requireNonNull(objectsWithTools, "objectsWithTools is required!");
88+
7589
if( streamingChatLanguageModel != null && chatLanguageModel != null ) {
7690
throw new IllegalArgumentException("chatLanguageModel and streamingChatLanguageModel are mutually exclusive!");
7791
}
7892
if( streamingChatLanguageModel == null && chatLanguageModel == null ) {
7993
throw new IllegalArgumentException("a chatLanguageModel or streamingChatLanguageModel is required!");
8094
}
8195

82-
var toolNode = ToolNode.of( objectsWithTools );
83-
84-
final List<ToolSpecification> toolSpecifications = toolNode.toolSpecifications();
96+
final var toolNode = toolNodeBuilder.build();
8597

8698
var agentRunnable = Agent.builder()
8799
.chatLanguageModel(chatLanguageModel)
88100
.streamingChatLanguageModel(streamingChatLanguageModel)
89-
.tools( toolSpecifications )
101+
.tools( toolNode.toolSpecifications() )
90102
.build();
91103

92104
if( stateSerializer == null ) {

samples/agent-executor/src/test/java/org/bsc/langgraph4j/agentexecutor/AgentExecutorStreamingTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private StateGraph<AgentExecutor.State> newGraph() throws Exception {
4646

4747
return agentExecutor.graphBuilder()
4848
.chatLanguageModel(chatLanguageModel)
49-
.objectsWithTools(List.of(new TestTool()))
49+
.toolSpecification(new TestTool())
5050
.build();
5151
}
5252

samples/agent-executor/src/test/java/org/bsc/langgraph4j/agentexecutor/AgentExecutorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private StateGraph<AgentExecutor.State> newGraph() throws Exception {
4646

4747
return agentExecutor.graphBuilder()
4848
.chatLanguageModel(chatLanguageModel)
49-
.objectsWithTools(List.of(new TestTool()))
49+
.toolSpecification(new TestTool())
5050
.build();
5151
}
5252

0 commit comments

Comments
 (0)