supeng há 3 semanas atrás
pai
commit
043f569262

+ 21 - 0
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/common/enums/ResponseFormatTypeEnum.java

@@ -0,0 +1,21 @@
+package com.tzld.piaoquan.content.understanding.common.enums;
+
+import lombok.Getter;
+
+/**
+ * @author supeng
+ */
+@Getter
+public enum ResponseFormatTypeEnum {
+
+    TEXT(0, "text格式"),
+    JSON(1, "json格式");
+
+    private final Integer value;
+    private final String desc;
+
+    ResponseFormatTypeEnum(Integer value, String desc) {
+        this.value = value;
+        this.desc = desc;
+    }
+}

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

@@ -0,0 +1,33 @@
+package com.tzld.piaoquan.content.understanding.model.param;
+
+import lombok.Data;
+
+/**
+ *
+ * @author supeng
+ */
+@Data
+public class DeepSeekParam {
+    /**
+     * prompt
+     */
+    private String prompt;
+    /**
+     * api key
+     */
+    private String apiKey;
+    /**
+     * 模型
+     */
+    private String model;
+    /**
+     * 温度
+     */
+    private Double temperature = 1.0;
+    /**
+     * format类型
+     * @see com.tzld.piaoquan.content.understanding.common.enums.ResponseFormatTypeEnum
+     */
+    private Integer responseFormat;
+
+}

+ 16 - 11
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/impl/DeepSeekGenerateContentAction.java

@@ -3,7 +3,9 @@ package com.tzld.piaoquan.content.understanding.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.tzld.piaoquan.content.understanding.common.base.CommonResponse;
 import com.tzld.piaoquan.content.understanding.common.enums.ContentTypeEnum;
+import com.tzld.piaoquan.content.understanding.common.enums.ResponseFormatTypeEnum;
 import com.tzld.piaoquan.content.understanding.model.param.ActionParam;
+import com.tzld.piaoquan.content.understanding.model.param.DeepSeekParam;
 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;
@@ -22,16 +24,17 @@ import java.util.concurrent.ThreadLocalRandom;
 @Service(value = "deepSeekGenerateContentAction")
 public class DeepSeekGenerateContentAction implements Action {
 
-    @Value("${ai.deepSeekApi.url:http://ai-api-internal.piaoquantv.com/aigc-server/deepseek/native/chat/completions}")
+    @Value("${ai.deepSeekApi.url:http://ai-api-internal.piaoquantv.com/aigc-server/deepseek/generateContent}")
     private String deepSeekApiUrl;
 
-    @Value("#{'${deepseek.apikey.list}'.split(',')}")
+    @Value("#{'${deepseek.apikey.list:[]}'.split(',')}")
     private List<String> apiKeyList;
 
     private static final HttpPoolClient httpPoolClient = HttpClientUtil.create(3000, 900000, 50, 200, 0, 3000);
 
-    private static final String MODEL = "gemini-2.0-flash";
-    private static final Double TEMPERATURE = 1.0;
+    public static final String MODEL_CHAT = "deepseek-chat";
+    public static final String MODEL_REASONER = "deepseek-reasoner";
+    public static final Double TEMPERATURE = 1.0;
 
     @Override
     public String execute(ActionParam param) {
@@ -44,13 +47,15 @@ public class DeepSeekGenerateContentAction implements Action {
         ThreadLocalRandom random = ThreadLocalRandom.current();
         int index = random.nextInt(apiKeyList.size());
         String apiKey = apiKeyList.get(index);
-        Map<String, String> headers = new HashMap<>();
-        headers.put("Content-Type", "application/json");
-        Map<String, Object> paramMap = new HashMap<>();
-
-
-        log.info("deepSeekGenerateContentAction paramMap = {}", JSON.toJSONString(paramMap));
-        Optional<String> optionalS = httpPoolClient.postJson(deepSeekApiUrl, JSON.toJSONString(paramMap), headers);
+        DeepSeekParam deepSeekParam = new DeepSeekParam();
+        deepSeekParam.setApiKey(apiKey);
+        deepSeekParam.setPrompt(param.getPrompt());
+        deepSeekParam.setModel(MODEL_REASONER);
+        deepSeekParam.setTemperature(TEMPERATURE);
+        //reasonser模型不支持 json format
+//        deepSeekParam.setResponseFormat(ResponseFormatTypeEnum.JSON.getValue());
+        log.info("deepSeekGenerateContentAction deepSeekParam = {}", JSON.toJSONString(deepSeekParam));
+        Optional<String> optionalS = httpPoolClient.postJson(deepSeekApiUrl, JSON.toJSONString(deepSeekParam));
         log.info("deepSeekGenerateContentAction optionalS = {}", optionalS);
         if (optionalS.isPresent()) {
             CommonResponse<Map<String, Object>> commonResponse = JSON.parseObject(optionalS.get(), CommonResponse.class);

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

@@ -28,7 +28,7 @@ public class GeminiGenerateContentAction implements Action {
     @Value("${ai.geminiApi.url:http://ai-api.piaoquantv.com/aigc-server/gemini/generateContent}")
     private String geminiApiUrl;
 
-    @Value("#{'${gemini.apikey.list}'.split(',')}")
+    @Value("#{'${gemini.apikey.list:[]}'.split(',')}")
     private List<String> apiKeyList;
 
     private static final HttpPoolClient httpPoolClient = HttpClientUtil.create(3000, 1200000, 50, 200, 0, 3000);

+ 46 - 0
content-understanding-server/src/test/java/com/tzld/piaoquan/content/understanding/service/DeepSeekServiceTest.java

@@ -0,0 +1,46 @@
+package com.tzld.piaoquan.content.understanding.service;
+
+import com.alibaba.fastjson.JSON;
+import com.tzld.piaoquan.content.understanding.BaseTest;
+import com.tzld.piaoquan.content.understanding.common.base.CommonResponse;
+import com.tzld.piaoquan.content.understanding.common.enums.ResponseFormatTypeEnum;
+import com.tzld.piaoquan.content.understanding.model.param.DeepSeekParam;
+import com.tzld.piaoquan.content.understanding.service.impl.DeepSeekGenerateContentAction;
+import com.tzld.piaoquan.content.understanding.util.HttpClientUtil;
+import com.tzld.piaoquan.content.understanding.util.HttpPoolClient;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+@Slf4j
+public class DeepSeekServiceTest extends BaseTest {
+
+    private static final HttpPoolClient httpPoolClient = HttpClientUtil.create(3000, 900000, 50, 200, 0, 3000);
+
+    @Test
+    public void test(){
+        String apiKey = "sk-4d57c097379348fb86c73e9d063e23c7";
+        DeepSeekParam deepSeekParam = new DeepSeekParam();
+        deepSeekParam.setApiKey(apiKey);
+        deepSeekParam.setPrompt("帮我随机生成一些能够吸引中老年人的视频标题,结果以json格式返回");
+        deepSeekParam.setModel(DeepSeekGenerateContentAction.MODEL_CHAT);
+        deepSeekParam.setModel(DeepSeekGenerateContentAction.MODEL_REASONER);
+        deepSeekParam.setTemperature(DeepSeekGenerateContentAction.TEMPERATURE);
+        //reasoner模型不支持 json format
+//        deepSeekParam.setResponseFormat(ResponseFormatTypeEnum.JSON.getValue());
+        log.info("deepSeekGenerateContentAction deepSeekParam = {}", JSON.toJSONString(deepSeekParam));
+        Optional<String> optionalS = httpPoolClient.postJson("http://ai-api-internal.piaoquantv.com/aigc-server/deepseek/generateContent", JSON.toJSONString(deepSeekParam));
+        log.info("deepSeekGenerateContentAction optionalS = {}", optionalS);
+        if (optionalS.isPresent()) {
+            CommonResponse<Map<String, Object>> commonResponse = JSON.parseObject(optionalS.get(), CommonResponse.class);
+            if (commonResponse.isSuccess() && Objects.nonNull(commonResponse.getData()) && Objects.nonNull(commonResponse.getData().get("result"))) {
+                String content = commonResponse.getData().get("result").toString();
+                //过滤一些特殊格式
+                content = content.replace("```json", "").replace("```", "");
+                log.info("deepSeekGenerateContentAction content = {}", content);
+            }
+        }
+    }
+}