Agent Executor¶
In [1]:
Copied!
var userHomeDir = System.getProperty("user.home");
var localRespoUrl = "file://" + userHomeDir + "/.m2/repository/";
var langchain4jVersion = "1.9.1";
var langchain4jbeta = "1.9.1-beta17";
var langgraph4jVersion = "1.7-SNAPSHOT";
var userHomeDir = System.getProperty("user.home");
var localRespoUrl = "file://" + userHomeDir + "/.m2/repository/";
var langchain4jVersion = "1.9.1";
var langchain4jbeta = "1.9.1-beta17";
var langgraph4jVersion = "1.7-SNAPSHOT";
Remove installed package from Jupiter cache
In [2]:
Copied!
%%bash
rm -rf \{userHomeDir}/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j
%%bash
rm -rf \{userHomeDir}/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j
add local maven repo and install dependencies
In [3]:
Copied!
%dependency /add-repo local \{localRespoUrl} release|never snapshot|always
// %dependency /list-repos
%dependency /add org.slf4j:slf4j-jdk14:2.0.9
%dependency /add org.bsc.langgraph4j:langgraph4j-core:\{langgraph4jVersion}
%dependency /add org.bsc.langgraph4j:langgraph4j-langchain4j:\{langgraph4jVersion}
%dependency /add org.bsc.langgraph4j:langgraph4j-agent-executor:\{langgraph4jVersion}
%dependency /add dev.langchain4j:langchain4j:\{langchain4jVersion}
%dependency /add dev.langchain4j:langchain4j-open-ai:\{langchain4jVersion}
%dependency /resolve
%dependency /add-repo local \{localRespoUrl} release|never snapshot|always
// %dependency /list-repos
%dependency /add org.slf4j:slf4j-jdk14:2.0.9
%dependency /add org.bsc.langgraph4j:langgraph4j-core:\{langgraph4jVersion}
%dependency /add org.bsc.langgraph4j:langgraph4j-langchain4j:\{langgraph4jVersion}
%dependency /add org.bsc.langgraph4j:langgraph4j-agent-executor:\{langgraph4jVersion}
%dependency /add dev.langchain4j:langchain4j:\{langchain4jVersion}
%dependency /add dev.langchain4j:langchain4j-open-ai:\{langchain4jVersion}
%dependency /resolve
Repository local url: file:///Users/bsorrentino/.m2/repository/ added. Adding dependency org.slf4j:slf4j-jdk14:2.0.9 Adding dependency org.bsc.langgraph4j:langgraph4j-core:1.7-SNAPSHOT Adding dependency org.bsc.langgraph4j:langgraph4j-langchain4j:1.7-SNAPSHOT Adding dependency org.bsc.langgraph4j:langgraph4j-agent-executor:1.7-SNAPSHOT Adding dependency dev.langchain4j:langchain4j:1.9.1 Adding dependency dev.langchain4j:langchain4j-open-ai:1.9.1 Solving dependencies Resolved artifacts count: 78 Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/slf4j/slf4j-jdk14/2.0.9/slf4j-jdk14-2.0.9.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j/langgraph4j-core/1.7-SNAPSHOT/langgraph4j-core-1.7-SNAPSHOT.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/async/async-generator/4.0.0-beta2/async-generator-4.0.0-beta2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j/langgraph4j-langchain4j/1.7-SNAPSHOT/langgraph4j-langchain4j-1.7-SNAPSHOT.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j/langgraph4j-agent-executor/1.7-SNAPSHOT/langgraph4j-agent-executor-1.7-SNAPSHOT.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-github-models/1.9.1-beta17/langchain4j-github-models-1.9.1-beta17.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-ai-inference/1.0.0-beta.5/azure-ai-inference-1.0.0-beta.5.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-json/1.5.0/azure-json-1.5.0.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-core/1.55.3/azure-core-1.55.3.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-xml/1.2.0/azure-xml-1.2.0.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.17.2/jackson-datatype-jsr310-2.17.2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/projectreactor/reactor-core/3.4.41/reactor-core-3.4.41.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/reactivestreams/reactive-streams/1.0.4/reactive-streams-1.0.4.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-core-http-netty/1.15.11/azure-core-http-netty-1.15.11.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-handler-proxy/4.1.118.Final/netty-handler-proxy-4.1.118.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-codec-socks/4.1.118.Final/netty-codec-socks-4.1.118.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-transport-native-unix-common/4.1.118.Final/netty-transport-native-unix-common-4.1.118.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-transport-native-epoll/4.1.118.Final/netty-transport-native-epoll-4.1.118.Final-linux-x86_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-transport-classes-epoll/4.1.118.Final/netty-transport-classes-epoll-4.1.118.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-transport-native-kqueue/4.1.118.Final/netty-transport-native-kqueue-4.1.118.Final-osx-x86_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-transport-classes-kqueue/4.1.118.Final/netty-transport-classes-kqueue-4.1.118.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-tcnative-boringssl-static/2.0.70.Final/netty-tcnative-boringssl-static-2.0.70.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-tcnative-classes/2.0.70.Final/netty-tcnative-classes-2.0.70.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-tcnative-boringssl-static/2.0.70.Final/netty-tcnative-boringssl-static-2.0.70.Final-linux-x86_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-tcnative-boringssl-static/2.0.70.Final/netty-tcnative-boringssl-static-2.0.70.Final-linux-aarch_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-tcnative-boringssl-static/2.0.70.Final/netty-tcnative-boringssl-static-2.0.70.Final-osx-x86_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-tcnative-boringssl-static/2.0.70.Final/netty-tcnative-boringssl-static-2.0.70.Final-osx-aarch_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-tcnative-boringssl-static/2.0.70.Final/netty-tcnative-boringssl-static-2.0.70.Final-windows-x86_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/projectreactor/netty/reactor-netty-http/1.0.48/reactor-netty-http-1.0.48.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-resolver-dns/4.1.112.Final/netty-resolver-dns-4.1.112.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-codec-dns/4.1.112.Final/netty-codec-dns-4.1.112.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-resolver-dns-native-macos/4.1.112.Final/netty-resolver-dns-native-macos-4.1.112.Final-osx-x86_64.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-resolver-dns-classes-macos/4.1.112.Final/netty-resolver-dns-classes-macos-4.1.112.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/projectreactor/netty/reactor-netty-core/1.0.48/reactor-netty-core-1.0.48.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-core-test/1.27.0-beta.8/azure-core-test-1.27.0-beta.8.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-core-http-jdk-httpclient/1.0.3/azure-core-http-jdk-httpclient-1.0.3.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-core-http-okhttp/1.12.10/azure-core-http-okhttp-1.12.10.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okio/okio-jvm/3.9.1/okio-jvm-3.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib/1.9.25/kotlin-stdlib-1.9.25.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/annotations/13.0/annotations-13.0.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okhttp3/okhttp/4.12.0/okhttp-4.12.0.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.21/kotlin-stdlib-jdk8-1.8.21.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.21/kotlin-stdlib-jdk7-1.8.21.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-core-http-vertx/1.0.3/azure-core-http-vertx-1.0.3.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/vertx/vertx-core/4.5.13/vertx-core-4.5.13.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/projectreactor/reactor-test/3.4.41/reactor-test-3.4.41.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/junit/jupiter/junit-jupiter-api/5.11.2/junit-jupiter-api-5.11.2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/opentest4j/opentest4j/1.3.0/opentest4j-1.3.0.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/junit/platform/junit-platform-commons/1.11.2/junit-platform-commons-1.11.2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/junit/jupiter/junit-jupiter-engine/5.11.2/junit-jupiter-engine-5.11.2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/junit/platform/junit-platform-engine/1.11.2/junit-platform-engine-1.11.2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/junit/jupiter/junit-jupiter-params/5.11.2/junit-jupiter-params-5.11.2.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/apache/ant/ant/1.10.14/ant-1.10.14.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/apache/ant/ant-launcher/1.10.14/ant-launcher-1.10.14.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-azure-open-ai/1.9.1/langchain4j-azure-open-ai-1.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/azure/azure-ai-openai/1.0.0-beta.16/azure-ai-openai-1.0.0-beta.16.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-codec-http2/4.1.128.Final/netty-codec-http2-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-common/4.1.128.Final/netty-common-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-buffer/4.1.128.Final/netty-buffer-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-transport/4.1.128.Final/netty-transport-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-resolver/4.1.128.Final/netty-resolver-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-codec/4.1.128.Final/netty-codec-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-handler/4.1.128.Final/netty-handler-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/io/netty/netty-codec-http/4.1.128.Final/netty-codec-http-4.1.128.Final.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-ollama/1.9.1/langchain4j-ollama-1.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j/1.9.1/langchain4j-1.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-core/1.9.1/langchain4j-core-1.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jspecify/jspecify/1.0.0/jspecify-1.0.0.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-annotations/2.20/jackson-annotations-2.20.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-core/2.20.1/jackson-core-2.20.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-databind/2.20.1/jackson-databind-2.20.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/apache/opennlp/opennlp-tools/2.5.4/opennlp-tools-2.5.4.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-open-ai/1.9.1/langchain4j-open-ai-1.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-http-client/1.9.1/langchain4j-http-client-1.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-http-client-jdk/1.9.1/langchain4j-http-client-jdk-1.9.1.jar Add to classpath: /Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/knuddels/jtokkit/1.1.0/jtokkit-1.1.0.jar
Initialize Logger
In [4]:
Copied!
try( var file = new java.io.FileInputStream("./logging.properties")) {
java.util.logging.LogManager.getLogManager().readConfiguration( file );
}
var log = org.slf4j.LoggerFactory.getLogger("AgentExecutor");
try( var file = new java.io.FileInputStream("./logging.properties")) {
java.util.logging.LogManager.getLogManager().readConfiguration( file );
}
var log = org.slf4j.LoggerFactory.getLogger("AgentExecutor");
Create Tools
In [5]:
Copied!
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import java.util.Optional;
import static java.lang.String.format;
public class TestTool {
private String lastResult;
Optional<String> lastResult() {
return Optional.ofNullable(lastResult);
}
@Tool("tool for test AI agent executor")
String execTest(@P("test message") String message) {
lastResult = format( "test tool executed: %s", message);
return lastResult;
}
}
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import java.util.Optional;
import static java.lang.String.format;
public class TestTool {
private String lastResult;
Optional lastResult() {
return Optional.ofNullable(lastResult);
}
@Tool("tool for test AI agent executor")
String execTest(@P("test message") String message) {
lastResult = format( "test tool executed: %s", message);
return lastResult;
}
}
In [6]:
Copied!
import static org.bsc.langgraph4j.StateGraph.END;
import static org.bsc.langgraph4j.StateGraph.START;
import org.bsc.langgraph4j.CompileConfig;
import org.bsc.langgraph4j.RunnableConfig;
import org.bsc.langgraph4j.checkpoint.BaseCheckpointSaver;
import org.bsc.langgraph4j.checkpoint.MemorySaver;
import org.bsc.langgraph4j.state.AgentState;
import org.bsc.langgraph4j.serializer.StateSerializer;
import org.bsc.langgraph4j.agentexecutor.AgentExecutor;
import dev.langchain4j.model.openai.OpenAiChatModel;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
var chatModel = OpenAiChatModel.builder()
.apiKey( System.getenv("OPENAI_API_KEY") )
//.modelName( "gpt-3.5-turbo-0125" )
.modelName( "gpt-4o-mini" )
.logResponses(true)
.maxRetries(2)
.temperature(0.0)
.maxTokens(2000)
.build();
var stateGraph = AgentExecutor.builder()
.chatModel(chatModel)
.toolsFromObject( new TestTool() )
.build();
import static org.bsc.langgraph4j.StateGraph.END;
import static org.bsc.langgraph4j.StateGraph.START;
import org.bsc.langgraph4j.CompileConfig;
import org.bsc.langgraph4j.RunnableConfig;
import org.bsc.langgraph4j.checkpoint.BaseCheckpointSaver;
import org.bsc.langgraph4j.checkpoint.MemorySaver;
import org.bsc.langgraph4j.state.AgentState;
import org.bsc.langgraph4j.serializer.StateSerializer;
import org.bsc.langgraph4j.agentexecutor.AgentExecutor;
import dev.langchain4j.model.openai.OpenAiChatModel;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
var chatModel = OpenAiChatModel.builder()
.apiKey( System.getenv("OPENAI_API_KEY") )
//.modelName( "gpt-3.5-turbo-0125" )
.modelName( "gpt-4o-mini" )
.logResponses(true)
.maxRetries(2)
.temperature(0.0)
.maxTokens(2000)
.build();
var stateGraph = AgentExecutor.builder()
.chatModel(chatModel)
.toolsFromObject( new TestTool() )
.build();
Test 1¶
Update State replacing the 'input'
In [7]:
Copied!
import dev.langchain4j.data.message.UserMessage;
var saver = new MemorySaver();
CompileConfig compileConfig = CompileConfig.builder()
.checkpointSaver( saver )
.build();
var graph = stateGraph.compile( compileConfig );
var config = RunnableConfig.builder()
.threadId("test1")
.build();
var iterator = graph.streamSnapshots( Map.of( "messages", UserMessage.from("perform test once")), config );
for( var step : iterator ) {
log.info( "STEP: {}", step );
}
import dev.langchain4j.data.message.UserMessage;
var saver = new MemorySaver();
CompileConfig compileConfig = CompileConfig.builder()
.checkpointSaver( saver )
.build();
var graph = stateGraph.compile( compileConfig );
var config = RunnableConfig.builder()
.threadId("test1")
.build();
var iterator = graph.streamSnapshots( Map.of( "messages", UserMessage.from("perform test once")), config );
for( var step : iterator ) {
log.info( "STEP: {}", step );
}
START
callAgent
STEP: StateSnapshot{node=__START__, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=0b799e1c-2563-498c-902e-55ce7a3fce88, nextNode=agent, streamMode=SNAPSHOTS }}
executeTools
execute: [execTest]
STEP: StateSnapshot{node=agent, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=f411fa43-6c35-47ae-946f-6a35a56c1d1a, nextNode=action, streamMode=SNAPSHOTS }}
callAgent
STEP: StateSnapshot{node=action, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo" toolName = "execTest" text = "test tool executed: perform test once" }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=eab1229d-e127-42ff-b9c8-d73434b1d065, nextNode=agent, streamMode=SNAPSHOTS }}
executeTools
STEP: StateSnapshot{node=agent, state={
agent_response=The test has been executed with the message: "perform test once".
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=dd3b83fb-0ded-4d32-affb-ad7fdf5bba5d, nextNode=action, streamMode=SNAPSHOTS }}
STEP: StateSnapshot{node=action, state={
agent_response=no tool execution request found!
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=8cf9d417-73a2-4ef6-b50b-2bbc0d0adfcb, nextNode=__END__, streamMode=SNAPSHOTS }}
STEP: NodeOutput{node=__END__, state={
agent_response=no tool execution request found!
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}}
In [8]:
Copied!
import org.bsc.langgraph4j.state.RemoveByHash;
var history = graph.getStateHistory(config).stream().collect( Collectors.toList() );
for( var snapshot : history ) {
log.info( "{}", snapshot );
}
// var state2 = history.get(2);
// var updatedConfig = graph.updateState( state2.config(),
// Map.of( "messages", UserMessage.from("perform test twice")), null);
// var iterator = graph.streamSnapshots( null, updatedConfig );
// for( var step : iterator ) {
// log.info( "STEP:\n {}", step );
// }
import org.bsc.langgraph4j.state.RemoveByHash;
var history = graph.getStateHistory(config).stream().collect( Collectors.toList() );
for( var snapshot : history ) {
log.info( "{}", snapshot );
}
// var state2 = history.get(2);
// var updatedConfig = graph.updateState( state2.config(),
// Map.of( "messages", UserMessage.from("perform test twice")), null);
// var iterator = graph.streamSnapshots( null, updatedConfig );
// for( var step : iterator ) {
// log.info( "STEP:\n {}", step );
// }
StateSnapshot{node=action, state={
agent_response=no tool execution request found!
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=8cf9d417-73a2-4ef6-b50b-2bbc0d0adfcb, nextNode=__END__, streamMode=VALUES }}
StateSnapshot{node=agent, state={
agent_response=The test has been executed with the message: "perform test once".
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=dd3b83fb-0ded-4d32-affb-ad7fdf5bba5d, nextNode=action, streamMode=VALUES }}
StateSnapshot{node=action, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo" toolName = "execTest" text = "test tool executed: perform test once" }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=eab1229d-e127-42ff-b9c8-d73434b1d065, nextNode=agent, streamMode=VALUES }}
StateSnapshot{node=agent, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_KGhXbzdA3z5NNZU0ux2Mwzwo", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=f411fa43-6c35-47ae-946f-6a35a56c1d1a, nextNode=action, streamMode=VALUES }}
StateSnapshot{node=__START__, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
]
}, config=RunnableConfig{ threadId=test1, checkPointId=0b799e1c-2563-498c-902e-55ce7a3fce88, nextNode=agent, streamMode=VALUES }}
Test 2¶
Update State replacing the 'input' using a cloned state
In [9]:
Copied!
var config = RunnableConfig.builder()
.threadId("test2")
.build();
var iterator = graph.streamSnapshots( Map.of( "messages", UserMessage.from("perform test once") ), config );
for( var step : iterator ) {
log.info( "STEP: {}", step );
}
var config = RunnableConfig.builder()
.threadId("test2")
.build();
var iterator = graph.streamSnapshots( Map.of( "messages", UserMessage.from("perform test once") ), config );
for( var step : iterator ) {
log.info( "STEP: {}", step );
}
START
callAgent
STEP: StateSnapshot{node=__START__, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
]
}, config=RunnableConfig{ threadId=test2, checkPointId=285b5856-031f-465d-8136-66dfb902733c, nextNode=agent, streamMode=SNAPSHOTS }}
executeTools
execute: [execTest]
STEP: StateSnapshot{node=agent, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
]
}, config=RunnableConfig{ threadId=test2, checkPointId=61a1df1a-cca3-4391-83a7-980f6c409c3c, nextNode=action, streamMode=SNAPSHOTS }}
callAgent
STEP: StateSnapshot{node=action, state={
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k" toolName = "execTest" text = "test tool executed: perform test once" }
]
}, config=RunnableConfig{ threadId=test2, checkPointId=8beef50b-65a5-4521-b919-fc75ed0b4796, nextNode=agent, streamMode=SNAPSHOTS }}
executeTools
STEP: StateSnapshot{node=agent, state={
agent_response=The test has been executed with the message: "perform test once".
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}, config=RunnableConfig{ threadId=test2, checkPointId=be089f38-6365-4ce7-a4bc-ef2dba8abc05, nextNode=action, streamMode=SNAPSHOTS }}
STEP: StateSnapshot{node=action, state={
agent_response=no tool execution request found!
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}, config=RunnableConfig{ threadId=test2, checkPointId=a2d5c295-2eec-4cf9-8f9b-5737d38a0d64, nextNode=__END__, streamMode=SNAPSHOTS }}
STEP: NodeOutput{node=__END__, state={
agent_response=no tool execution request found!
messages=[
UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }
AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }
ToolExecutionResultMessage { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k" toolName = "execTest" text = "test tool executed: perform test once" }
AiMessage { text = "The test has been executed with the message: "perform test once".", thinking = null, toolExecutionRequests = [], attributes = {} }
]
}}
In [10]:
Copied!
var history = graph.getStateHistory(config).stream().collect( Collectors.toList() );
var state2 = history.get(2);
var updatedState = new HashMap<String,Object>();
updatedState.putAll(state2.state().data());
updatedState.put( "messages", UserMessage.from("perform test twice") );
// var updatedConfig = graph.updateState( state2.config(), updatedState );
// log.info( "UPDATED CONFIG: {}", updatedConfig );
// var iterator = graph.streamSnapshots( null, updatedConfig );
// for( var step : iterator ) {
// log.info( "STEP:\n {}", step );
// }
var history = graph.getStateHistory(config).stream().collect( Collectors.toList() );
var state2 = history.get(2);
var updatedState = new HashMap();
updatedState.putAll(state2.state().data());
updatedState.put( "messages", UserMessage.from("perform test twice") );
// var updatedConfig = graph.updateState( state2.config(), updatedState );
// log.info( "UPDATED CONFIG: {}", updatedConfig );
// var iterator = graph.streamSnapshots( null, updatedConfig );
// for( var step : iterator ) {
// log.info( "STEP:\n {}", step );
// }
Out[10]:
[UserMessage { name = null, contents = [TextContent { text = "perform test once" }], attributes = {} }, AiMessage { text = null, thinking = null, toolExecutionRequests = [ToolExecutionRequest { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k", name = "execTest", arguments = "{"message":"perform test once"}" }], attributes = {} }, ToolExecutionResultMessage { id = "call_hq3RECYLLcBGkVxTuRdGPQ1k" toolName = "execTest" text = "test tool executed: perform test once" }]