|
1 |
| -# langgraph4j |
2 |
| -🚀 LangGraph for Java. A library for building stateful, multi-actor applications with LLMs, built for work with langchain4j |
| 1 | +# LangGraph for Java |
| 2 | +🚀 LangGraph for Java. A library for building stateful, multi-actor applications with LLMs, built for work with [langchain4j] |
| 3 | +> It is a porting of original [LangGraph] from [LangChain AI project][langchain.ai] in Java fashion |
| 4 | +
|
| 5 | + |
| 6 | +## Quick Start |
| 7 | + |
| 8 | +### Adding LangGraph dependency |
| 9 | + |
| 10 | +Currently are available only the developer SNAPSHOTs |
| 11 | + |
| 12 | +**Maven** |
| 13 | + |
| 14 | +** JDK8 compliant ** |
| 15 | +```xml |
| 16 | +<dependency> |
| 17 | + <groupId>org.bsc.langgraph4j</groupId> |
| 18 | + <artifactId>langgraph4j</artifactId> |
| 19 | + <version>1.0-SNAPSHOT</version> |
| 20 | + <classifier>jdk8<classifier> |
| 21 | +<dependency> |
| 22 | +``` |
| 23 | + |
| 24 | +** JDK17 compliant ** |
| 25 | +```xml |
| 26 | +<dependency> |
| 27 | + <groupId>org.bsc.langgraph4j</groupId> |
| 28 | + <artifactId>langgraph4j</artifactId> |
| 29 | + <version>1.0-SNAPSHOT</version> |
| 30 | + <classifier>jdk17<classifier> |
| 31 | +<dependency> |
| 32 | +``` |
| 33 | + |
| 34 | +### Define the agent state |
| 35 | + |
| 36 | +The main type of graph in `langgraph` is the `StatefulGraph`. 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 denoted by initialize the property with a `AppendableValue`. The State must be compliant with `AgentState` interface that essentially is a `Map` wrapper. |
| 37 | + |
| 38 | +```java |
| 39 | +public interface AgentState { |
| 40 | + |
| 41 | + java.util.Map<String,Object> data(); |
| 42 | + |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Define the nodes |
| 47 | + |
| 48 | +We now need to define a few different nodes in our graph. In `langgraph`, a node is a function that accept an `AgentState` as argument. There are two main nodes we need for this: |
| 49 | + |
| 50 | +1. **The agent**: responsible for deciding what (if any) actions to take. |
| 51 | +1. **A function to invoke tools**: if the agent decides to take an action, this node will then execute that action. |
| 52 | + |
| 53 | +### Define Edges |
| 54 | + |
| 55 | +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). |
| 56 | + |
| 57 | +1. **Conditional Edge**: after the agent is called, we should either: |
| 58 | + * If the agent said to take an action, then the function to invoke tools should be called |
| 59 | + * If the agent said that it was finished, then it should finish |
| 60 | +1. **Normal Edge**: after the tools are invoked, it should always go back to the agent to decide what to do next |
| 61 | + |
| 62 | +### Define the graph |
| 63 | + |
| 64 | +We can now put it all together and define the graph! (see example below) |
| 65 | + |
| 66 | +## Integrate with LangChain4j |
| 67 | + |
| 68 | +Like default use case proposed in [LangGraph blog][langgraph.blog], We have ported [AgentExecutor] implementation from [langchain] using LangGraph4j. In the [agents](agents) project's module, you can the complete working code with tests. Feel free to checkout and use it as a reference. |
| 69 | +Below you can find a piece of code of the `AgentExecutor` to give you an idea of how is has built in langgraph style. |
| 70 | + |
| 71 | + |
| 72 | +```java |
| 73 | + |
| 74 | + public static class State implements AgentState { |
| 75 | + |
| 76 | + private final Map<String,Object> data; |
| 77 | + |
| 78 | + public State( Map<String,Object> initData ) { |
| 79 | + this.data = new HashMap<>(initData); |
| 80 | + this.data.putIfAbsent("intermediate_steps", |
| 81 | + new AppendableValue<IntermediateStep>()); |
| 82 | + } |
| 83 | + |
| 84 | + public Map<String,Object> data() { return Map.copyOf(data); } |
| 85 | + |
| 86 | + Optional<String> input() { return value("input"); } |
| 87 | + Optional<AgentOutcome> agentOutcome() { return value("agent_outcome"); } |
| 88 | + Optional<List<IntermediateStep>> intermediateSteps() { return appendableValue("intermediate_steps"); } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + var toolInfoList = ToolInfo.fromList( objectsWithTools ); |
| 93 | + |
| 94 | + final List<ToolSpecification> toolSpecifications = toolInfoList.stream() |
| 95 | + .map(ToolInfo::specification) |
| 96 | + .toList(); |
| 97 | + |
| 98 | + var agentRunnable = Agent.builder() |
| 99 | + .chatLanguageModel(chatLanguageModel) |
| 100 | + .tools( toolSpecifications ) |
| 101 | + .build(); |
| 102 | + |
| 103 | + var workflow = new GraphState<>(State::new); |
| 104 | + |
| 105 | + workflow.setEntryPoint("agent"); |
| 106 | + |
| 107 | + workflow.addNode( "agent", node_async( state -> |
| 108 | + runAgent(agentRunnable, state)) // see implementation in the repo code |
| 109 | + ); |
| 110 | + |
| 111 | + workflow.addNode( "action", node_async( state -> |
| 112 | + executeTools(toolInfoList, state)) // see implementation in the repo code |
| 113 | + ); |
| 114 | + |
| 115 | + workflow.addConditionalEdge( |
| 116 | + "agent", |
| 117 | + edge_async( state -> { |
| 118 | + if (state.agentOutcome().map(AgentOutcome::finish).isPresent()) { |
| 119 | + return "end"; |
| 120 | + } |
| 121 | + return "continue"; |
| 122 | + }), |
| 123 | + Map.of("continue", "action", "end", END) |
| 124 | + ); |
| 125 | + |
| 126 | + workflow.addEdge("action", "agent"); |
| 127 | + |
| 128 | + var app = workflow.compile(); |
| 129 | + |
| 130 | + return app.stream( inputs ); |
| 131 | + |
| 132 | +``` |
| 133 | + |
| 134 | +# References |
| 135 | + |
| 136 | +* [LangGraph - LangChain Blog][langgraph.blog] |
| 137 | + |
| 138 | +[langgraph.blog]: https://blog.langchain.dev/langgraph/ |
| 139 | +[langchain4j]: https://github.com/langchain4j/langchain4j |
| 140 | +[langchain.ai]: https://github.com/langchain-ai |
| 141 | +[langchain]: https://github.com/langchain-ai/langchain/ |
| 142 | +[langgraph]: https://github.com/langchain-ai/langgraph |
| 143 | +[langchain.agents]: https://python.langchain.com/docs/modules/agents/ |
| 144 | +[AgentExecutor]: https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/agents/agent.py |
0 commit comments