|
@@ -14,6 +14,7 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import space.anyi.springAiAlibabaLearn.nodeAction.GenerationAsyncNodeAction;
|
|
import space.anyi.springAiAlibabaLearn.nodeAction.GenerationAsyncNodeAction;
|
|
|
import space.anyi.springAiAlibabaLearn.nodeAction.ImproveJokeAsyncNodeAction;
|
|
import space.anyi.springAiAlibabaLearn.nodeAction.ImproveJokeAsyncNodeAction;
|
|
|
import space.anyi.springAiAlibabaLearn.nodeAction.JudgeJokeAsyncNodeAction;
|
|
import space.anyi.springAiAlibabaLearn.nodeAction.JudgeJokeAsyncNodeAction;
|
|
|
|
|
+import space.anyi.springAiAlibabaLearn.nodeAction.LoopJudgeAsyncNodeAction;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -152,6 +153,18 @@ public class GraphConfig {
|
|
|
//4.编译图,最终使用的graph
|
|
//4.编译图,最终使用的graph
|
|
|
return stateGraph.compile();
|
|
return stateGraph.compile();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 有条件分支的工作流
|
|
|
|
|
+ * # 生成笑话的工作流
|
|
|
|
|
+ * 1.生成笑话
|
|
|
|
|
+ * 2.打分
|
|
|
|
|
+ * 2.1如果笑话不够优秀,则进行优化
|
|
|
|
|
+ * 3.结束
|
|
|
|
|
+ * @param chatModel
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws GraphStateException
|
|
|
|
|
+ */
|
|
|
@Bean
|
|
@Bean
|
|
|
@Qualifier("conditionGraph")
|
|
@Qualifier("conditionGraph")
|
|
|
public CompiledGraph conditionGraph(ChatModel chatModel) throws GraphStateException {
|
|
public CompiledGraph conditionGraph(ChatModel chatModel) throws GraphStateException {
|
|
@@ -197,4 +210,61 @@ public class GraphConfig {
|
|
|
//4.编译图,最终使用的graph
|
|
//4.编译图,最终使用的graph
|
|
|
return stateGraph.compile();
|
|
return stateGraph.compile();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 有循环的工作流
|
|
|
|
|
+ * # 生成笑话的工作流
|
|
|
|
|
+ * 1.生成笑话
|
|
|
|
|
+ * 2.打分
|
|
|
|
|
+ * 2.1笑话不够优秀则重新生成,然后再进行打分
|
|
|
|
|
+ * 3.结束
|
|
|
|
|
+ * @param chatModel
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws GraphStateException
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ @Qualifier("loopGraph")
|
|
|
|
|
+ public CompiledGraph loopGraph(ChatModel chatModel) throws GraphStateException{
|
|
|
|
|
+ KeyStrategyFactory keyStrategyFactory = new KeyStrategyFactory(){
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Map<String, KeyStrategy> apply() {
|
|
|
|
|
+ return Map.of(
|
|
|
|
|
+ "topic",KeyStrategy.REPLACE,
|
|
|
|
|
+ "joke",KeyStrategy.REPLACE,
|
|
|
|
|
+ "result",KeyStrategy.REPLACE,
|
|
|
|
|
+ "loopCount",KeyStrategy.REPLACE
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ //1.创建状态图,用于存储工作流中的数据
|
|
|
|
|
+ StateGraph stateGraph = new StateGraph(keyStrategyFactory);
|
|
|
|
|
+
|
|
|
|
|
+ //2.向工作流添加节点
|
|
|
|
|
+ stateGraph.addNode("generation", new GenerationAsyncNodeAction(chatModel));
|
|
|
|
|
+ stateGraph.addNode("loopJudge", new LoopJudgeAsyncNodeAction(chatModel,5));
|
|
|
|
|
+ //3.定义图中的边,边的作用是指明节点间的流转关系
|
|
|
|
|
+ stateGraph.addEdge(StateGraph.START,"generation");
|
|
|
|
|
+ stateGraph.addEdge("generation","loopJudge");
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 条件边
|
|
|
|
|
+ * 根据条件边构建一个环,这个环就是循环
|
|
|
|
|
+ */
|
|
|
|
|
+ stateGraph.addConditionalEdges("loopJudge",
|
|
|
|
|
+ //条件结果的处理
|
|
|
|
|
+ AsyncEdgeAction.edge_async(new EdgeAction() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String apply(OverAllState state) throws Exception {
|
|
|
|
|
+ //取笑话的评分作为条件
|
|
|
|
|
+ return state.value("result","break");
|
|
|
|
|
+ }
|
|
|
|
|
+ }),
|
|
|
|
|
+ //根据条件值如何进行工作流节点流转
|
|
|
|
|
+ Map.of(
|
|
|
|
|
+ //key为条件值,value为node的id
|
|
|
|
|
+ "break",StateGraph.END,
|
|
|
|
|
+ "loop","generation"
|
|
|
|
|
+ ));
|
|
|
|
|
+ //4.编译图,最终使用的graph
|
|
|
|
|
+ return stateGraph.compile();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|