Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🚀 LangGraph for Java. A library for building stateful, multi-actor applications with LLMs, built for work jointly with langchain4j

License

NotificationsYou must be signed in to change notification settings

bsorrentino/langgraph4j

Repository files navigation

JavadocStatic BadgeMaven Central

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

Features

  • 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
  • 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

Releases

Note:‼️

From release 1.2.x the miminum supported Java version is theJava 17 andthe artifactlanggraph4j-core-jdk8 is replaced bylanggraph4j-core

DateReleaseinfo
Mar 21, 20251.4.4official release

How To -(Java Notebook)

Samples

ProjectIntegrated With
Agent ExecutorSpringAI
Agent ExecutorLangchain4j
Image To PlantUML DiagramLangchain4j
Adaptive RAGLangchain4j

Quick Start

Adding LangGraph dependency

Last stable version

Maven

<dependency>    <groupId>org.bsc.langgraph4j</groupId>    <artifactId>langgraph4j-core</artifactId>    <version>1.4.4</version></dependency>

Development Version

Maven

<dependency>    <groupId>org.bsc.langgraph4j</groupId>    <artifactId>langgraph4j-core</artifactId>    <version>1.4-SNAPSHOT</version></dependency>

Define the agent state

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 ) { ... }    }

Define the nodes

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:

  1. The agent: responsible for deciding what (if any) actions to take.
  2. 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) { ... }}

Define Edges

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).

  1. 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
  2. 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 ) { ... }}

Define the graph

We can now put it all together and define the graph! (see example below)

Integrate with LangChain4j

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 );

References

About

🚀 LangGraph for Java. A library for building stateful, multi-actor applications with LLMs, built for work jointly with langchain4j

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp