Graph Core 快速开始
ARGI Graph Core 是状态图工作流运行时。它将工作流建模为节点、边和共享状态:节点执行工作并返回状态更新,边决定下一步执行哪个节点,状态在图执行过程中持续合并和传递。
添加依赖
使用 BOM 管理版本后,引入 Graph Core:
<dependency>
<groupId>io.github.agentic-ai</groupId>
<artifactId>argi-graph-core</artifactId>
</dependency>
创建并执行 StateGraph
下面示例定义一个只有一个业务节点的图。图从 START 进入 greet 节点,节点读取输入状态并写入 message,最后流转到 END。
import io.github.agentic.ai.graph.CompiledGraph;
import io.github.agentic.ai.graph.KeyStrategyFactory;
import io.github.agentic.ai.graph.OverAllState;
import io.github.agentic.ai.graph.StateGraph;
import io.github.agentic.ai.graph.state.strategy.ReplaceStrategy;
import java.util.Map;
import java.util.Optional;
import static io.github.agentic.ai.graph.StateGraph.END;
import static io.github.agentic.ai.graph.StateGraph.START;
import static io.github.agentic.ai.graph.action.AsyncNodeAction.node_async;
KeyStrategyFactory strategies = () -> Map.of(
"message", new ReplaceStrategy()
);
StateGraph graph = new StateGraph(strategies)
.addNode("greet", node_async(state -> {
String name = (String) state.value("name").orElse("World");
return Map.of("message", "Hello " + name);
}))
.addEdge(START, "greet")
.addEdge("greet", END);
CompiledGraph compiledGraph = graph.compile();
Optional<OverAllState> result = compiledGraph.invoke(Map.of("name", "ARGI"));
String message = (String) result.orElseThrow().value("message").orElseThrow();
System.out.println(message);