supeng 1 mese fa
parent
commit
9e4b6fed2e

+ 27 - 0
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/model/dto/PipelineTreeNode.java

@@ -0,0 +1,27 @@
+package com.tzld.piaoquan.content.understanding.model.dto;
+
+import com.tzld.piaoquan.content.understanding.model.po.CuPipelineStep;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * pipeline 树结构
+ *
+ * @author supeng
+ */
+@Data
+public class PipelineTreeNode {
+    /**
+     * 要执行的步骤
+     */
+    private CuPipelineStep step;
+    /**
+     * 上一步执行结果
+     */
+    private String input;
+    /**
+     * 子节点
+     */
+    private List<PipelineTreeNode> children;
+}

+ 90 - 7
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/impl/PipelineServiceImpl.java

@@ -2,17 +2,21 @@ package com.tzld.piaoquan.content.understanding.service.impl;
 
 import com.tzld.piaoquan.content.understanding.common.enums.ExceptionEnum;
 import com.tzld.piaoquan.content.understanding.common.exception.CommonException;
+import com.tzld.piaoquan.content.understanding.dao.mapper.CuPipelineMapper;
+import com.tzld.piaoquan.content.understanding.dao.mapper.CuPipelineStepMapper;
+import com.tzld.piaoquan.content.understanding.dao.mapper.CuPromptMapper;
+import com.tzld.piaoquan.content.understanding.model.dto.PipelineTreeNode;
 import com.tzld.piaoquan.content.understanding.model.param.ActionParam;
+import com.tzld.piaoquan.content.understanding.model.po.CuPipelineStep;
+import com.tzld.piaoquan.content.understanding.model.po.CuPipelineStepExample;
+import com.tzld.piaoquan.content.understanding.model.po.CuPrompt;
 import com.tzld.piaoquan.content.understanding.service.Action;
 import com.tzld.piaoquan.content.understanding.service.PipelineService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 
 /**
  * @author supeng
@@ -21,16 +25,32 @@ import java.util.Objects;
 @Service
 public class PipelineServiceImpl implements PipelineService {
 
+    @Autowired
+    private CuPipelineMapper cuPipelineMapper;
+
+    @Autowired
+    private CuPipelineStepMapper cuPipelineStepMapper;
+
+    @Autowired
+    private CuPromptMapper cuPromptMapper;
+
     @Autowired
     private Map<String, Action> actionMap;
 
     @Override
     public String execute(Long pipelineId) {
         // 1.获取pipeline配置
-        List<String> stepList = new ArrayList<>();
-        // 2.按照步骤执行pipeline每一步动作
+        CuPipelineStepExample example = new CuPipelineStepExample();
+        example.createCriteria().andPipelineIdEqualTo(pipelineId);
+        List<CuPipelineStep> stepList = cuPipelineStepMapper.selectByExample(example);
+        if (Objects.isNull(stepList) || stepList.isEmpty()) {
+            throw new CommonException(ExceptionEnum.CONFIG_ERROR, "无配置 pipelineId:" + pipelineId);
+        }
+        // 2.构建tree结构
+        PipelineTreeNode root = buildTree(stepList);
+        // 3.按照步骤执行pipeline每一步动作
         String result = null;
-        for (String step : stepList) {
+        for (CuPipelineStep step : stepList) {
             if (Objects.isNull(step)) {
                 throw new CommonException(ExceptionEnum.DATA_ERROR, "step is null pipelineId=" + pipelineId);
             }
@@ -44,4 +64,67 @@ public class PipelineServiceImpl implements PipelineService {
         }
         return result;
     }
+
+    public static PipelineTreeNode buildTree(List<CuPipelineStep> stepList) {
+        Map<Long, PipelineTreeNode> nodeMap = new HashMap<>();
+        PipelineTreeNode root = null;
+
+        // 将所有节点放入 Map 中,方便后续查找
+        for (CuPipelineStep step : stepList) {
+            PipelineTreeNode node = new PipelineTreeNode();
+            node.setStep(step);
+            nodeMap.put(step.getId(), node);
+        }
+
+        // 构建父子关系
+        for (CuPipelineStep step : stepList) {
+            Long parentId = step.getParentId();
+            if (Objects.isNull(parentId) || parentId == 0) {
+                root = nodeMap.get(step.getId());
+            } else if (nodeMap.containsKey(parentId)) {
+                PipelineTreeNode parentNode = nodeMap.get(parentId);
+                PipelineTreeNode currentNode = nodeMap.get(step.getId());
+                List<PipelineTreeNode> children = parentNode.getChildren();
+                if (Objects.isNull(children)) {
+                    children = new ArrayList<>();
+                    parentNode.setChildren(children);
+                }
+                children.add(currentNode);
+            }
+        }
+        return root;
+    }
+
+    public void processTreeNodeBFS(PipelineTreeNode root) {
+        if (root == null) {
+            return;
+        }
+        Queue<PipelineTreeNode> queue = new LinkedList<>();
+        queue.offer(root);
+
+        while (!queue.isEmpty()) {
+            PipelineTreeNode currentNode = queue.poll();
+            CuPipelineStep step = currentNode.getStep();
+            String input = currentNode.getInput();
+            // 执行action
+//            String result = executeStep(step, input);
+
+            for (PipelineTreeNode child : currentNode.getChildren()) {
+                queue.offer(child);
+            }
+        }
+    }
+
+    // 模拟执行 Pipeline Step 的方法
+    public Object executeStep(CuPipelineStep step, String input) {
+        // 这里可以根据 step 的信息 (例如 promptId) 执行实际的操作
+        // 并将 inputParameter 作为输入
+        // 返回当前步骤的执行结果
+        CuPrompt cuPrompt = cuPromptMapper.selectByPrimaryKey(step.getPromptId());
+        if (Objects.isNull(cuPrompt) || Objects.isNull(cuPrompt.getId())) {
+            return null;
+        }
+//        String prompt = String.format(cuPrompt.getPromptId(),input);
+        return "执行步骤 " + step.getId() + " (Prompt ID: " + step.getPromptId() + "), 输入: " + input;
+    }
 }