Skip to content

Commit fe4ea4c

Browse files
committed
test(notebook): test notebook from @hide212131
1 parent 64e6ad5 commit fe4ea4c

File tree

1 file changed

+315
-0
lines changed

1 file changed

+315
-0
lines changed

how-tos/wait-user-input.ipynb

+315
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"String userHomeDir = System.getProperty(\"user.home\");\n",
10+
"String localRespoUrl = \"file://\" + userHomeDir + \"/.m2/repository/\";\n",
11+
"String langchain4jVersion = \"0.34.0\""
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 2,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"\u001b[0mRepository \u001b[1m\u001b[32mlocal\u001b[0m url: \u001b[1m\u001b[32mfile:///Users/bsorrentino/.m2/repository/\u001b[0m added.\n",
24+
"\u001b[0mRepositories count: 5\n",
25+
"\u001b[0mname: \u001b[1m\u001b[32mcentral \u001b[0murl: \u001b[1m\u001b[32mhttps://repo.maven.apache.org/maven2/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n",
26+
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32mjcenter \u001b[0murl: \u001b[1m\u001b[32mhttps://jcenter.bintray.com/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n",
27+
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32mjboss \u001b[0murl: \u001b[1m\u001b[32mhttps://repository.jboss.org/nexus/content/repositories/releases/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n",
28+
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32matlassian \u001b[0murl: \u001b[1m\u001b[32mhttps://packages.atlassian.com/maven/public \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mfalse \u001b[0mupdate:\u001b[32mnever \n",
29+
"\u001b[0m\u001b[0mname: \u001b[1m\u001b[32mlocal \u001b[0murl: \u001b[1m\u001b[32mfile:///Users/bsorrentino/.m2/repository/ \u001b[0mrelease:\u001b[32mtrue \u001b[0mupdate:\u001b[32mnever \u001b[0msnapshot:\u001b[32mtrue \u001b[0mupdate:\u001b[32malways \n",
30+
"\u001b[0m"
31+
]
32+
}
33+
],
34+
"source": [
35+
"%dependency /add-repo local \\{localRespoUrl} release|never snapshot|always\n",
36+
"%dependency /list-repos"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 3,
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"Adding dependency \u001b[0m\u001b[1m\u001b[32morg.bsc.langgraph4j:langgraph4j-core-jdk8:1.0-SNAPSHOT\n",
49+
"\u001b[0mAdding dependency \u001b[0m\u001b[1m\u001b[32mdev.langchain4j:langchain4j:0.34.0\n",
50+
"\u001b[0mAdding dependency \u001b[0m\u001b[1m\u001b[32mdev.langchain4j:langchain4j-open-ai:0.34.0\n",
51+
"\u001b[0mProposed dependencies count: 3\n",
52+
"\u001b[0m - \u001b[1m\u001b[32morg.bsc.langgraph4j:langgraph4j-core-jdk8:jar:1.0-SNAPSHOT (runtime)\n",
53+
"\u001b[0m\u001b[0m - \u001b[1m\u001b[32mdev.langchain4j:langchain4j:jar:0.34.0 (runtime)\n",
54+
"\u001b[0m\u001b[0m - \u001b[1m\u001b[32mdev.langchain4j:langchain4j-open-ai:jar:0.34.0 (runtime)\n",
55+
"\u001b[0mResolved dependencies count: 0\n",
56+
"Solving dependencies\n",
57+
"Resolved artifacts count: 25\n",
58+
"Add to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/langgraph4j/langgraph4j-core-jdk8/1.0-SNAPSHOT/langgraph4j-core-jdk8-1.0-SNAPSHOT.jar\u001b[0m\n",
59+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/bsc/async/async-generator-jdk8/2.0.1/async-generator-jdk8-2.0.1.jar\u001b[0m\n",
60+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar\u001b[0m\n",
61+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j/0.34.0/langchain4j-0.34.0.jar\u001b[0m\n",
62+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-core/0.34.0/langchain4j-core-0.34.0.jar\u001b[0m\n",
63+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar\u001b[0m\n",
64+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/apache/opennlp/opennlp-tools/1.9.4/opennlp-tools-1.9.4.jar\u001b[0m\n",
65+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jsoup/jsoup/1.16.1/jsoup-1.16.1.jar\u001b[0m\n",
66+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/langchain4j/langchain4j-open-ai/0.34.0/langchain4j-open-ai-0.34.0.jar\u001b[0m\n",
67+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/dev/ai4j/openai4j/0.20.0/openai4j-0.20.0.jar\u001b[0m\n",
68+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/retrofit2/retrofit/2.9.0/retrofit-2.9.0.jar\u001b[0m\n",
69+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/retrofit2/converter-jackson/2.9.0/converter-jackson-2.9.0.jar\u001b[0m\n",
70+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-databind/2.17.2/jackson-databind-2.17.2.jar\u001b[0m\n",
71+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-annotations/2.17.2/jackson-annotations-2.17.2.jar\u001b[0m\n",
72+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/fasterxml/jackson/core/jackson-core/2.17.2/jackson-core-2.17.2.jar\u001b[0m\n",
73+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okhttp3/okhttp/4.12.0/okhttp-4.12.0.jar\u001b[0m\n",
74+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okio/okio/3.6.0/okio-3.6.0.jar\u001b[0m\n",
75+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okio/okio-jvm/3.6.0/okio-jvm-3.6.0.jar\u001b[0m\n",
76+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/squareup/okhttp3/okhttp-sse/4.12.0/okhttp-sse-4.12.0.jar\u001b[0m\n",
77+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.10/kotlin-stdlib-jdk8-1.9.10.jar\u001b[0m\n",
78+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib/1.9.10/kotlin-stdlib-1.9.10.jar\u001b[0m\n",
79+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.10/kotlin-stdlib-common-1.9.10.jar\u001b[0m\n",
80+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/annotations/13.0/annotations-13.0.jar\u001b[0m\n",
81+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.10/kotlin-stdlib-jdk7-1.9.10.jar\u001b[0m\n",
82+
"\u001b[0mAdd to classpath: \u001b[0m\u001b[32m/Users/bsorrentino/Library/Jupyter/kernels/rapaio-jupyter-kernel/mima_cache/com/knuddels/jtokkit/1.1.0/jtokkit-1.1.0.jar\u001b[0m\n",
83+
"\u001b[0m"
84+
]
85+
}
86+
],
87+
"source": [
88+
"%dependency /add org.bsc.langgraph4j:langgraph4j-core-jdk8:1.0-SNAPSHOT\n",
89+
"%dependency /add dev.langchain4j:langchain4j:\\{langchain4jVersion}\n",
90+
"%dependency /add dev.langchain4j:langchain4j-open-ai:\\{langchain4jVersion}\n",
91+
"%dependency /list-dependencies\n",
92+
"%dependency /resolve"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 12,
98+
"metadata": {},
99+
"outputs": [
100+
{
101+
"data": {
102+
"text/plain": [
103+
"---\n",
104+
"title: Graph Diagram\n",
105+
"---\n",
106+
"flowchart TD\n",
107+
"\tstart((start))\n",
108+
"\tstop((stop))\n",
109+
"\tstep_1(\"step_1\")\n",
110+
"\thuman_feedback(\"human_feedback\")\n",
111+
"\tstep_3(\"step_3\")\n",
112+
"\tstart:::start --> step_1:::step_1\n",
113+
"\tstep_1:::step_1 --> human_feedback:::human_feedback\n",
114+
"\thuman_feedback:::human_feedback --> step_3:::step_3\n",
115+
"\tstep_3:::step_3 --> __END__:::__END__\n"
116+
]
117+
},
118+
"execution_count": 12,
119+
"metadata": {},
120+
"output_type": "execute_result"
121+
}
122+
],
123+
"source": [
124+
"import org.bsc.langgraph4j.*;\n",
125+
"import org.bsc.langgraph4j.state.AgentState;\n",
126+
"import org.bsc.langgraph4j.state.Channel;\n",
127+
"import org.bsc.langgraph4j.state.AppenderChannel;\n",
128+
"import dev.langchain4j.data.message.AiMessage;\n",
129+
"import dev.langchain4j.data.message.ChatMessage;\n",
130+
"import org.bsc.langgraph4j.action.AsyncNodeAction;\n",
131+
"import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;\n",
132+
"import static org.bsc.langgraph4j.utils.CollectionsUtils.mapOf;\n",
133+
"import org.bsc.langgraph4j.checkpoint.MemorySaver;\n",
134+
"import org.bsc.langgraph4j.CompileConfig;\n",
135+
"import static org.bsc.langgraph4j.StateGraph.END;\n",
136+
"import static org.bsc.langgraph4j.StateGraph.START;\n",
137+
"\n",
138+
"public class State extends AgentState {\n",
139+
"\n",
140+
" static Map<String, Channel<?>> SCHEMA = Map.of(\n",
141+
" \"messages\", AppenderChannel.<AiMessage>of(ArrayList::new)\n",
142+
" );\n",
143+
"\n",
144+
" public State(Map<String, Object> initData) {\n",
145+
" super( initData );\n",
146+
" }\n",
147+
"\n",
148+
" Optional<String> input() { return value(\"input\"); } \n",
149+
" Optional<String> userFeedback() { return value(\"user_feedback\"); } \n",
150+
"\n",
151+
"}\n",
152+
"\n",
153+
"AsyncNodeAction<State> step1 = node_async(state -> {\n",
154+
" System.out.println( \"---Step 1---\" );\n",
155+
" return mapOf();\n",
156+
"});\n",
157+
"\n",
158+
"AsyncNodeAction<State> humanFeedback = node_async(state -> {\n",
159+
" System.out.println( \"---human_feedback---\" );\n",
160+
" return mapOf();\n",
161+
"});\n",
162+
"\n",
163+
"AsyncNodeAction<State> step3 = node_async(state -> {\n",
164+
" System.out.println( \"---Step 3---\" );\n",
165+
" return mapOf();\n",
166+
"});\n",
167+
"\n",
168+
"var builder = new StateGraph<>(State.SCHEMA, State::new);\n",
169+
"builder.addNode(\"step_1\", step1);\n",
170+
"builder.addNode(\"human_feedback\", humanFeedback);\n",
171+
"builder.addNode(\"step_3\", step3);\n",
172+
"builder.addEdge(START, \"step_1\");\n",
173+
"builder.addEdge(\"step_1\", \"human_feedback\");\n",
174+
"builder.addEdge(\"human_feedback\", \"step_3\");\n",
175+
"builder.addEdge(\"step_3\", END);\n",
176+
"\n",
177+
"// Set up memory\n",
178+
"var saver = new MemorySaver();\n",
179+
"\n",
180+
"// Add\n",
181+
"var compileConfig = CompileConfig.builder().checkpointSaver(saver).interruptBefore(\"human_feedback\").build();\n",
182+
"var graph = builder.compile(compileConfig);\n",
183+
"\n",
184+
"// View as Mermaid with ```mermaid ... ```\n",
185+
"graph.getGraph(GraphRepresentation.Type.MERMAID).getContent();"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 13,
191+
"metadata": {},
192+
"outputs": [
193+
{
194+
"name": "stdout",
195+
"output_type": "stream",
196+
"text": [
197+
"---Step 1---\n",
198+
"NodeOutput(node=__START__, state={input=hello world, messages=[]})\n",
199+
"NodeOutput(node=step_1, state={input=hello world, messages=[]})\n"
200+
]
201+
}
202+
],
203+
"source": [
204+
"// Input\n",
205+
"var initialInput = mapOf(\"input\", (Object) \"hello world\");\n",
206+
"\n",
207+
"// Thread\n",
208+
"var invokeConfig = RunnableConfig.builder().threadId(\"Thread1\").build();\n",
209+
"\n",
210+
"// Run the graph until the first interruption\n",
211+
"for (var event : graph.stream(initialInput, invokeConfig)) {\n",
212+
" System.out.println(event);\n",
213+
"}\n"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 14,
219+
"metadata": {},
220+
"outputs": [
221+
{
222+
"name": "stdout",
223+
"output_type": "stream",
224+
"text": [
225+
"Tell me how you want to update the state: go to step 3!\n",
226+
"--State after update--\n",
227+
"StateSnapshot(state={user_feedback=go to step 3!, input=hello world, messages=[]}, config=RunnableConfig(threadId=Thread1, checkPointId=9cc155ba-c7a2-4b8a-81a0-fda9240e4bdd, nextNode=human_feedback))\n",
228+
"getNext with invokeConfig: human_feedback\n",
229+
"getNext with updateConfig: human_feedback\n"
230+
]
231+
}
232+
],
233+
"source": [
234+
"// Get user input\n",
235+
"//String userInput = new Scanner(System.in).nextLine();\n",
236+
"String userInput = \"go to step 3!\";\n",
237+
"System.out.println(\"Tell me how you want to update the state: \" + userInput);\n",
238+
"\n",
239+
"// We now update the state as if we are the human_feedback node\n",
240+
"//var updateConfig = graph.updateState(invokeConfig, mapOf(\"user_feedback\", userInput), \"human_feedback\");\n",
241+
"var updateConfig = graph.updateState(invokeConfig, mapOf(\"user_feedback\", userInput), null);\n",
242+
"\n",
243+
"// We can check the state\n",
244+
"System.out.println(\"--State after update--\");\n",
245+
"System.out.println(graph.getState(invokeConfig));\n",
246+
"\n",
247+
"// We can check the next node, showing that it is node 3 (which follows human_feedback)\n",
248+
"System.out.println(\"getNext with invokeConfig: \" + graph.getState(invokeConfig).getNext());\n",
249+
"System.out.println(\"getNext with updateConfig: \" + graph.getState(updateConfig).getNext());"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": 15,
255+
"metadata": {},
256+
"outputs": [
257+
{
258+
"name": "stdout",
259+
"output_type": "stream",
260+
"text": [
261+
"---human_feedback---\n",
262+
"---Step 3---\n",
263+
"NodeOutput(node=human_feedback, state={user_feedback=go to step 3!, input=hello world, messages=[]})\n",
264+
"NodeOutput(node=step_3, state={user_feedback=go to step 3!, input=hello world, messages=[]})\n",
265+
"NodeOutput(node=__END__, state={user_feedback=go to step 3!, input=hello world, messages=[]})\n"
266+
]
267+
}
268+
],
269+
"source": [
270+
"// Continue the graph execution\n",
271+
"for (var event : graph.stream(null, updateConfig)) {\n",
272+
" System.out.println(event);\n",
273+
"}"
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"execution_count": 16,
279+
"metadata": {},
280+
"outputs": [
281+
{
282+
"data": {
283+
"text/plain": [
284+
"{user_feedback=go to step 3!, input=hello world, messages=[]}"
285+
]
286+
},
287+
"execution_count": 16,
288+
"metadata": {},
289+
"output_type": "execute_result"
290+
}
291+
],
292+
"source": [
293+
"graph.getState(updateConfig).getState();"
294+
]
295+
}
296+
],
297+
"metadata": {
298+
"kernelspec": {
299+
"display_name": "Java (rjk 2.1.0)",
300+
"language": "java",
301+
"name": "rapaio-jupyter-kernel"
302+
},
303+
"language_info": {
304+
"codemirror_mode": "java",
305+
"file_extension": ".jshell",
306+
"mimetype": "text/x-java-source",
307+
"name": "java",
308+
"nbconvert_exporter": "script",
309+
"pygments_lexer": "java",
310+
"version": "22.0.2"
311+
}
312+
},
313+
"nbformat": 4,
314+
"nbformat_minor": 2
315+
}

0 commit comments

Comments
 (0)