|
|
@@ -2,6 +2,8 @@ package space.anyi.springAiAlibabaLearn.config;
|
|
|
|
|
|
import com.alibaba.cloud.ai.graph.*;
|
|
|
import com.alibaba.cloud.ai.graph.action.*;
|
|
|
+import com.alibaba.cloud.ai.graph.checkpoint.config.SaverConfig;
|
|
|
+import com.alibaba.cloud.ai.graph.checkpoint.savers.MemorySaver;
|
|
|
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -19,6 +21,8 @@ import space.anyi.springAiAlibabaLearn.nodeAction.LoopJudgeAsyncNodeAction;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.function.Supplier;
|
|
|
|
|
|
@Configuration
|
|
|
public class GraphConfig {
|
|
|
@@ -267,4 +271,36 @@ public class GraphConfig {
|
|
|
//4.编译图,最终使用的graph
|
|
|
return stateGraph.compile();
|
|
|
}
|
|
|
+ @Bean
|
|
|
+ @Qualifier("saveGraph")
|
|
|
+ public CompiledGraph saveGraph() throws GraphStateException {
|
|
|
+ StateGraph stateGraph = new StateGraph(() -> Map.of(
|
|
|
+ "msg",KeyStrategy.REPLACE,
|
|
|
+ "history",KeyStrategy.APPEND
|
|
|
+ ));
|
|
|
+ stateGraph.addNode("save", state -> CompletableFuture.supplyAsync(() -> {
|
|
|
+ //取出msg
|
|
|
+ String msg = state.value("msg", "");
|
|
|
+ log.debug("msg:{}",msg);
|
|
|
+ //更新历史消息
|
|
|
+ return Map.of(
|
|
|
+ "history",msg
|
|
|
+ );
|
|
|
+ }));
|
|
|
+ stateGraph.addEdge(StateGraph.START,"save");
|
|
|
+ stateGraph.addEdge("save",StateGraph.END);
|
|
|
+ //获取图的信息,mermaid格式,可以在markdown中渲染出来
|
|
|
+ GraphRepresentation graphRepresentation = stateGraph.getGraph(GraphRepresentation.Type.MERMAID);
|
|
|
+ log.info("graphRepresentation:{}",graphRepresentation.content());
|
|
|
+ //图的状态存储配置,使用内存的策略
|
|
|
+ SaverConfig saverConfig = SaverConfig.builder()
|
|
|
+ .register(new MemorySaver())
|
|
|
+ .build();
|
|
|
+ //图的编译配置
|
|
|
+ CompileConfig compileConfig = CompileConfig.builder()
|
|
|
+ .saverConfig(saverConfig)
|
|
|
+ .build();
|
|
|
+ //使用配置进行图的编译
|
|
|
+ return stateGraph.compile(compileConfig);
|
|
|
+ }
|
|
|
}
|