|
|
@@ -1,8 +1,7 @@
|
|
|
package space.anyi.springAiAlibabaLearn.config;
|
|
|
|
|
|
import com.alibaba.cloud.ai.graph.*;
|
|
|
-import com.alibaba.cloud.ai.graph.action.AsyncNodeAction;
|
|
|
-import com.alibaba.cloud.ai.graph.action.NodeAction;
|
|
|
+import com.alibaba.cloud.ai.graph.action.*;
|
|
|
import com.alibaba.cloud.ai.graph.exception.GraphStateException;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -12,6 +11,9 @@ import org.springframework.ai.chat.model.ChatModel;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import space.anyi.springAiAlibabaLearn.nodeAction.GenerationAsyncNodeAction;
|
|
|
+import space.anyi.springAiAlibabaLearn.nodeAction.ImproveJokeAsyncNodeAction;
|
|
|
+import space.anyi.springAiAlibabaLearn.nodeAction.JudgeJokeAsyncNodeAction;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -150,4 +152,49 @@ public class GraphConfig {
|
|
|
//4.编译图,最终使用的graph
|
|
|
return stateGraph.compile();
|
|
|
}
|
|
|
+ @Bean
|
|
|
+ @Qualifier("conditionGraph")
|
|
|
+ public CompiledGraph conditionGraph(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
|
|
|
+ );
|
|
|
+ }
|
|
|
+ };
|
|
|
+ //1.创建状态图,用于存储工作流中的数据
|
|
|
+ StateGraph stateGraph = new StateGraph(keyStrategyFactory);
|
|
|
+
|
|
|
+ //2.向工作流添加节点
|
|
|
+ stateGraph.addNode("generation", new GenerationAsyncNodeAction(chatModel));
|
|
|
+ stateGraph.addNode("judge", new JudgeJokeAsyncNodeAction(chatModel));
|
|
|
+ stateGraph.addNode("improve", new ImproveJokeAsyncNodeAction(chatModel));
|
|
|
+ //3.定义图中的边,边的作用是指明节点间的流转关系
|
|
|
+ stateGraph.addEdge(StateGraph.START,"generation");
|
|
|
+ stateGraph.addEdge("generation","judge");
|
|
|
+ /**
|
|
|
+ * 条件边,根据是上一个节点的数据条件连接下一个节点
|
|
|
+ */
|
|
|
+ stateGraph.addConditionalEdges("judge",
|
|
|
+ //条件结果的处理
|
|
|
+ AsyncEdgeAction.edge_async(new EdgeAction() {
|
|
|
+ @Override
|
|
|
+ public String apply(OverAllState state) throws Exception {
|
|
|
+ //取笑话的评分作为条件
|
|
|
+ return state.value("result","优秀");
|
|
|
+ }
|
|
|
+ }),
|
|
|
+ //根据条件值如何进行工作流节点流转
|
|
|
+ Map.of(
|
|
|
+ //key为条件值,value为node的id
|
|
|
+ "优秀",StateGraph.END,
|
|
|
+ "不优秀","improve"
|
|
|
+ ));
|
|
|
+ stateGraph.addEdge("improve",StateGraph.END);
|
|
|
+ //4.编译图,最终使用的graph
|
|
|
+ return stateGraph.compile();
|
|
|
+ }
|
|
|
}
|