supeng пре 1 месец
родитељ
комит
77fe762dda

+ 5 - 0
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/model/param/ActionParam.java

@@ -7,6 +7,11 @@ import lombok.Data;
  */
 @Data
 public class ActionParam {
+    /**
+     * 类型
+     * @see com.tzld.piaoquan.content.understanding.common.enums.ContentTypeEnum
+     */
+    private Integer type;
     /**
      * 输入参数
      */

+ 5 - 1
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/model/param/ContentAnalyseParam.java

@@ -20,5 +20,9 @@ public class ContentAnalyseParam {
     /**
      *
      */
-    private Integer stragrey;
+    private Long pipelineId;
+    /**
+     *
+     */
+    private Long videoId;
 }

+ 3 - 1
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/PipelineService.java

@@ -1,9 +1,11 @@
 package com.tzld.piaoquan.content.understanding.service;
 
+import com.tzld.piaoquan.content.understanding.model.param.ContentAnalyseParam;
+
 /**
  * @author supeng
  */
 public interface PipelineService {
 
-    String execute(Long pipelineId);
+    void execute(ContentAnalyseParam param);
 }

+ 10 - 3
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/impl/ContentServiceImpl.java

@@ -1,6 +1,8 @@
 package com.tzld.piaoquan.content.understanding.service.impl;
 
 import com.tzld.piaoquan.content.understanding.common.base.CommonRequest;
+import com.tzld.piaoquan.content.understanding.common.enums.ExceptionEnum;
+import com.tzld.piaoquan.content.understanding.common.exception.CommonException;
 import com.tzld.piaoquan.content.understanding.model.param.ContentAnalyseParam;
 import com.tzld.piaoquan.content.understanding.service.ContentService;
 import com.tzld.piaoquan.content.understanding.service.LoghubService;
@@ -9,6 +11,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.Objects;
+
 /**
  * @author supeng
  */
@@ -24,8 +28,11 @@ public class ContentServiceImpl implements ContentService {
 
     @Override
     public void analyse(CommonRequest<ContentAnalyseParam> request) {
-        //TODO
-        Long pipelineId = 0L;
-        pipelineService.execute(pipelineId);
+        if (Objects.isNull(request) || Objects.isNull(request.getParams())
+                || Objects.isNull(request.getParams().getPipelineId()) || Objects.isNull(request.getParams().getType()) {
+            throw new CommonException(ExceptionEnum.PARAMS_INVALID);
+        }
+        ContentAnalyseParam params = request.getParams();
+        pipelineService.execute(params);
     }
 }

+ 27 - 2
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/impl/GeminiGenerateContentAction.java

@@ -1,13 +1,19 @@
 package com.tzld.piaoquan.content.understanding.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.tzld.piaoquan.content.understanding.common.enums.ContentTypeEnum;
 import com.tzld.piaoquan.content.understanding.model.param.ActionParam;
+import com.tzld.piaoquan.content.understanding.model.param.GeminiParam;
 import com.tzld.piaoquan.content.understanding.service.Action;
 import com.tzld.piaoquan.content.understanding.util.HttpClientUtil;
 import com.tzld.piaoquan.content.understanding.util.HttpPoolClient;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.ThreadLocalRandom;
 
 /**
  * @author supeng
@@ -18,15 +24,34 @@ public class GeminiGenerateContentAction implements Action {
     @Value("${native.openai.url:http://openai.piaoquantv.com/aigc-server/gemini/generateContent}")
     private String geminiApiUrl;
 
+    @Value("#{'${gemini:apikey:list}'.split(',')}")
+    private List<String> apiKeyList;
+
     private static final HttpPoolClient httpPoolClient = HttpClientUtil.create(3000, 600000, 50, 200, 0, 3000);
 
+    private static final String MODEL = "gemini-2.0-flash";
+    private static final Double TEMPERATURE = 1.0;
+
     @Override
     public String execute(ActionParam param) {
-        Optional<String> optionalS = httpPoolClient.postJson(geminiApiUrl, "");
+        Integer type = param.getType();
+        ThreadLocalRandom random = ThreadLocalRandom.current();
+        int index = random.nextInt(apiKeyList.size());
+        String apiKey = apiKeyList.get(index);
+        GeminiParam geminiParam = new GeminiParam();
+        geminiParam.setType(type);
+        geminiParam.setPrompt(param.getPrompt());
+        if (Objects.equals(ContentTypeEnum.IMAGE.getValue(), type) || Objects.equals(ContentTypeEnum.VIDEO.getValue(), type)) {
+            geminiParam.setMediaUrl(param.getInput());
+        }
+        geminiParam.setApiKey(apiKey);
+        geminiParam.setModel(MODEL);
+        geminiParam.setTemperature(TEMPERATURE);
+        Optional<String> optionalS = httpPoolClient.postJson(geminiApiUrl, JSON.toJSONString(geminiParam));
         if (optionalS.isPresent()) {
             String content = optionalS.get();
             //过滤一些特殊格式
-            content = content.replace("```json","").replace("```","");
+            content = content.replace("```json", "").replace("```", "");
             return content;
         }
         return null;

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

@@ -7,6 +7,7 @@ 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.param.ContentAnalyseParam;
 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;
@@ -38,8 +39,9 @@ public class PipelineServiceImpl implements PipelineService {
     private Map<String, Action> actionMap;
 
     @Override
-    public String execute(Long pipelineId) {
+    public void execute(ContentAnalyseParam param) {
         // 1.获取pipeline配置
+        Long pipelineId = param.getPipelineId();
         CuPipelineStepExample example = new CuPipelineStepExample();
         example.createCriteria().andPipelineIdEqualTo(pipelineId);
         List<CuPipelineStep> stepList = cuPipelineStepMapper.selectByExample(example);
@@ -49,23 +51,10 @@ public class PipelineServiceImpl implements PipelineService {
         // 2.构建tree结构
         PipelineTreeNode root = buildTree(stepList);
         // 3.按照步骤执行pipeline每一步动作
-        String result = null;
-        for (CuPipelineStep step : stepList) {
-            if (Objects.isNull(step)) {
-                throw new CommonException(ExceptionEnum.DATA_ERROR, "step is null pipelineId=" + pipelineId);
-            }
-            Action action = actionMap.get(step);
-            if (Objects.isNull(action)) {
-                throw new CommonException(ExceptionEnum.DATA_ERROR, "action is null pipelineId=" + pipelineId);
-            }
-            ActionParam actionParam = new ActionParam();
-            actionParam.setInput(result);
-            result = action.execute(actionParam);
-        }
-        return result;
+        executeTreeNodeBFS(root);
     }
 
-    public static PipelineTreeNode buildTree(List<CuPipelineStep> stepList) {
+    public PipelineTreeNode buildTree(List<CuPipelineStep> stepList) {
         Map<Long, PipelineTreeNode> nodeMap = new HashMap<>();
         PipelineTreeNode root = null;
 
@@ -95,7 +84,7 @@ public class PipelineServiceImpl implements PipelineService {
         return root;
     }
 
-    public void processTreeNodeBFS(PipelineTreeNode root) {
+    public void executeTreeNodeBFS(PipelineTreeNode root) {
         if (root == null) {
             return;
         }
@@ -105,26 +94,38 @@ public class PipelineServiceImpl implements PipelineService {
         while (!queue.isEmpty()) {
             PipelineTreeNode currentNode = queue.poll();
             CuPipelineStep step = currentNode.getStep();
-            String input = currentNode.getInput();
+            String input = currentNode.getInput() == null ? "" : currentNode.getInput();
             // 执行action
-//            String result = executeStep(step, input);
-
+            String result = executeStep(step, input);
+            if (Objects.isNull(currentNode.getChildren()) || currentNode.getChildren().isEmpty()) {
+                //TODO 保存 result
+                continue;
+            }
             for (PipelineTreeNode child : currentNode.getChildren()) {
+                child.setInput(result);
                 queue.offer(child);
             }
         }
     }
 
-    // 模拟执行 Pipeline Step 的方法
-    public Object executeStep(CuPipelineStep step, String input) {
-        // 这里可以根据 step 的信息 (例如 promptId) 执行实际的操作
-        // 并将 inputParameter 作为输入
-        // 返回当前步骤的执行结果
+    /**
+     * 执行step 动作
+     *
+     * @param step
+     * @param input
+     * @return
+     */
+    public String executeStep(CuPipelineStep step, String input) {
         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;
+        String prompt = String.format(cuPrompt.getPrompt(), input);
+
+        Action action = actionMap.get(step.getAction());
+        ActionParam actionParam = new ActionParam();
+        actionParam.setInput(prompt);
+        actionParam.setPrompt(prompt);
+        return action.execute(actionParam);
     }
 }