heyudev 2 mesiacov pred
rodič
commit
f5d449a6e8

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

@@ -55,20 +55,7 @@ public class GeminiGenerateContentAction implements Action {
 //        int index = random.nextInt(apiKeyList.size());
 //        String apiKey = apiKeyList.get(index);
         int size = apiKeyList.size();
-        int index = 0;
-        Long value = redisUtil.incr(GEMINI_API_KEY_INDEX);
-        if (Objects.nonNull(value)) {
-            long indexValue = value - 1;
-            index = (int) (indexValue % size);
-            if (value > 0 && value % size == 0) {
-                try {
-                    redisUtil.deleteKey(GEMINI_API_KEY_INDEX);
-                } catch (Exception e) {
-                    // 重置失败不影响当前获取元素的操作,只记录错误
-                    log.error(e.getMessage(), e);
-                }
-            }
-        }
+        int index = getIndex(size);
         String apiKey = apiKeyList.get(index);
         GeminiParam geminiParam = new GeminiParam();
         geminiParam.setType(type);
@@ -95,16 +82,43 @@ public class GeminiGenerateContentAction implements Action {
                         content = content.replace("```json", "").replace("```", "");
                         return content;
                     } else {
+                        int index1 = getIndex(size);
+                        String apiKey1 = apiKeyList.get(index1);
+                        geminiParam.setApiKey(apiKey1);
                         throw new RuntimeException("geminiGenerateContentAction request failure geminiParam = " + JSON.toJSONString(geminiParam));
                     }
                 } else {
+                    int index1 = getIndex(size);
+                    String apiKey1 = apiKeyList.get(index1);
+                    geminiParam.setApiKey(apiKey1);
                     throw new RuntimeException("geminiGenerateContentAction optionalS is null geminiParam = " + JSON.toJSONString(geminiParam));
                 }
             } catch (Exception e) {
+                int index1 = getIndex(size);
+                String apiKey1 = apiKeyList.get(index1);
+                geminiParam.setApiKey(apiKey1);
                 log.error("geminiGenerateContentAction error param = {}", JSON.toJSONString(geminiParam), e);
                 throw new RuntimeException("geminiGenerateContentAction request error", e);
             }
         }, RETRY_TIMES, "geminiGenerateContentAction", RetryUtil::retryInterval);
     }
 
+    private int getIndex(int size) {
+        int index = 0;
+        Long value = redisUtil.incr(GEMINI_API_KEY_INDEX);
+        if (Objects.nonNull(value)) {
+            long indexValue = value - 1;
+            index = (int) (indexValue % size);
+            if (value > 0 && value % size == 0) {
+                try {
+                    redisUtil.deleteKey(GEMINI_API_KEY_INDEX);
+                } catch (Exception e) {
+                    // 重置失败不影响当前获取元素的操作,只记录错误
+                    log.error(e.getMessage(), e);
+                }
+            }
+        }
+        return index;
+    }
+
 }