supeng 2 weeks ago
parent
commit
c69138a4ad

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

@@ -12,10 +12,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Optional;
+import java.util.*;
 import java.util.concurrent.ThreadLocalRandom;
 
 /**
@@ -40,25 +37,20 @@ public class DeepSeekGenerateContentAction implements Action {
     public String execute(ActionParam param) {
         Integer type = param.getType();
         //类型校验
-        if (Objects.isNull(type)) {
-
+        if (!Objects.equals(ContentTypeEnum.TITLE.getValue(), type) && !Objects.equals(ContentTypeEnum.INTRODUCTION.getValue(), type)) {
+            log.error("deepseek 不支持非文本类型 param = {}", param);
+            return null;
         }
         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.COVER.getValue(), type) || Objects.equals(ContentTypeEnum.VIDEO.getValue(), type)
-                || Objects.equals(ContentTypeEnum.AUDIO.getValue(), type) || Objects.equals(ContentTypeEnum.SRT.getValue(), type)
-                || Objects.equals(ContentTypeEnum.VTT.getValue(), type)) {
-            geminiParam.setMediaUrl(param.getInput());
-        }
-        geminiParam.setApiKey(apiKey);
-        geminiParam.setModel(MODEL);
-        geminiParam.setTemperature(TEMPERATURE);
-        log.info("geminiGenerateContentAction geminiParam = {}", JSON.toJSONString(geminiParam));
-        Optional<String> optionalS = httpPoolClient.postJson(deepSeekApiUrl, JSON.toJSONString(geminiParam));
+        Map<String, String> headers = new HashMap<>();
+        headers.put("Content-Type", "application/json");
+        Map<String, Object> paramMap = new HashMap<>();
+
+
+        log.info("geminiGenerateContentAction paramMap = {}", JSON.toJSONString(paramMap));
+        Optional<String> optionalS = httpPoolClient.postJson(deepSeekApiUrl, JSON.toJSONString(paramMap), headers);
         log.info("geminiGenerateContentAction optionalS = {}", optionalS);
         if (optionalS.isPresent()) {
             CommonResponse<Map<String, Object>> commonResponse = JSON.parseObject(optionalS.get(), CommonResponse.class);

+ 25 - 3
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/util/HttpPoolClient.java

@@ -17,6 +17,7 @@ import org.apache.http.conn.socket.ConnectionSocketFactory;
 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
 import org.apache.http.conn.ssl.NoopHostnameVerifier;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
@@ -31,10 +32,12 @@ import org.slf4j.MDC;
 import javax.net.ssl.SSLContext;
 import java.net.SocketTimeoutException;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.security.KeyManagementException;
 import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -94,14 +97,33 @@ public class HttpPoolClient {
 
 
     public Optional<String> postJson(String url, String json) {
+//        HttpPost httpPost = new HttpPost(url);
+//        if (StringUtils.isBlank(json)) {
+//            return request(httpPost);
+//        }
+//        StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
+//        entity.setContentEncoding("UTF-8");
+//        entity.setContentType("application/json");
+//        httpPost.setEntity(entity);
+//        return request(httpPost);
+        return postJson(url, json, null);
+    }
+
+    public Optional<String> postJson(String url, String json, Map<String, String> headers) {
         HttpPost httpPost = new HttpPost(url);
+        // 添加自定义请求头
+        if (headers != null && !headers.isEmpty()) {
+            for (Map.Entry<String, String> entry : headers.entrySet()) {
+                httpPost.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
         if (StringUtils.isBlank(json)) {
             return request(httpPost);
         }
-        StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
-        entity.setContentEncoding("UTF-8");
-        entity.setContentType("application/json");
+        // 正确设置 Content-Type 和字符集
+        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8));
         httpPost.setEntity(entity);
+
         return request(httpPost);
     }