- Notifications
You must be signed in to change notification settings - Fork52
🚀 LangGraph for Java. A library for building stateful, multi-actor applications with LLMs, built for work jointly with langchain4j
License
bsorrentino/langgraph4j
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
LangGraph for Java. A library for building stateful, multi-agents applications with LLMs, built for work withlangchain4j
It is a porting of originalLangGraph fromLangChain AI project in Java fashion
- StateGraph
- Nodes
- Edges
- Conditional Edges
- Entry Points
- Conditional Entry Points
- State
- Schema (a series of Channels)
- Reducer (how apply updates to the state attributes)
- Default provider
- AppenderChannel (values accumulator)
- delete messages
- Schema (a series of Channels)
- Compiling graph
- Async support (throughtCompletableFuture)
- Streaming support (throughtjava-async-generator)
- Checkpoints (save and replay feature)
- Graph visualization
- Playground (Embeddable Webapp that plays with LangGraph4j)
- Threads (checkpointing of multiple different runs)
- Update state (interact with the state directly and update it)
- Breakpoints (pause and resume feature)
- Studio (Playground Webapp)
- Streaming response from LLM results
- Child Graphs
- Parallel Node Execution
- With some constraints
Note:
From release 1.2.x the miminum supported Java version is the
Java 17
andthe artifactlanggraph4j-core-jdk8
is replaced bylanggraph4j-core
Date | Release | info |
---|---|---|
Mar 21, 2025 | 1.4.4 | official release |
- How to add persistence ("memory") to your graph
- How to view and update past graph state
- How to parallel branch
- How to wait for user input
- How to sub-graph
- Use Case
Project | Integrated With |
---|---|
Agent Executor | SpringAI |
Agent Executor | Langchain4j |
Image To PlantUML Diagram | Langchain4j |
Adaptive RAG | Langchain4j |
Maven
<dependency> <groupId>org.bsc.langgraph4j</groupId> <artifactId>langgraph4j-core</artifactId> <version>1.4.4</version></dependency>
Maven
<dependency> <groupId>org.bsc.langgraph4j</groupId> <artifactId>langgraph4j-core</artifactId> <version>1.4-SNAPSHOT</version></dependency>
The main type of graph inlanggraph
is theStatefulGraph
. This graph is parameterized by a state object that it passes around to each node.Each node then returns operations to update that state. These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.Whether to set or add is described in the state's schema provided to the graph. The schema is a Map of Channels, each Channel represent an attribute in the state. If an attribute is described with anAppendeChannel
it will be a List and each element referring the attribute will be automaically added by graph during processing. The State must inherit fromAgentState
base class (that essentially is aMap
wrapper).
publicclassAgentState {publicAgentState(Map<String,Object>initData ) { ... }publicfinaljava.util.Map<String,Object>data() { ... }publicfinal <T>Optional<T>value(Stringkey) { ... }publicfinal <T>Tvalue(Stringkey,TdefaultValue ) { ... }publicfinal <T>Tvalue(Stringkey,Supplier<T>defaultProvider ) { ... } }
We now need to define a few different nodes in our graph. Inlanggraph
, a node is an async/sync function that accept anAgentState
as argument and returns a (partial) state update. There are two main nodes we need for this:
- The agent: responsible for deciding what (if any) actions to take.
- A function to invoke tools: if the agent decides to take an action, this node will then execute that action.
/** * Represents an asynchronous node action that operates on an agent state and returns state update. * * @param <S> the type of the agent state */@FunctionalInterfacepublicinterfaceAsyncNodeAction<SextendsAgentState>extendsFunction<S,CompletableFuture<Map<String,Object>>> {CompletableFuture<Map<String,Object>>apply(St);/** * Creates an asynchronous node action from a synchronous node action. */static <SextendsAgentState>AsyncNodeAction<S>node_async(NodeAction<S>syncAction) { ... }}
We will also need to define some edges. Some of these edges may be conditional. The reason they are conditional is that based on the output of a node, one of several paths may be taken. The path that is taken is not known until that node is run (the LLM decides).
- Conditional Edge: after the agent is called, we should either:
- If the agent said to take an action, then the function to invoke tools should be called
- If the agent said that it was finished, then it should finish
- Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next
/** * Represents an asynchronous edge action that operates on an agent state and returns a new route. * * @param <S> the type of the agent state */publicinterfaceAsyncEdgeAction<SextendsAgentState>extendsFunction<S,CompletableFuture<String>> {CompletableFuture<String>apply(St);/** * Creates an asynchronous edge action from a synchronous edge action. */static <SextendsAgentState>AsyncEdgeAction<S>edge_async(EdgeAction<S>syncAction ) { ... }}
We can now put it all together and define the graph! (see example below)
Like default use case proposed inLangGraph blog, We have convertedAgentExecutor implementation fromlangchain using LangGraph4j. In theagent-executor project's sample, there is the complete working code with tests. Feel free to checkout and use it as a reference.Below you can find a piece of code of theAgentExecutor
to give you an idea of how is has built in langgraph style.
/** * Represents the state of an agent. */classStateextendsMessagesState<ChatMessage> {publicState(Map<String,Object>initData) {super(initData); }publicOptional<String>finalResponse() {returnvalue("agent_response"); }}vartoolNode =ToolNode.builder() .toolSpecification(tools ) .build();varagent =Agent.builder() .chatLanguageModel(chatLanguageModel) .tools(toolNode.toolSpecifications()) .build();varcallAgent =newCallAgent(agent);varexecuteTools =newExecuteTools(agent,toolNode);// Fluent Interfacevarapp =newStateGraph<>(State.SCHEMA,State::new) .addEdge(START,"agent") .addNode("agent",node_async(callAgent)) .addNode("action",node_async(executeTools)) .addConditionalEdges("agent",edge_async(state ->state.finalResponse() .map(res ->"end") .orElse("continue"); ),Map.of("continue","action","end",END) ) .addEdge("action","agent") .compile();returnapp.stream(inputs );
About
🚀 LangGraph for Java. A library for building stateful, multi-actor applications with LLMs, built for work jointly with langchain4j