|
@@ -8,6 +8,7 @@ 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 com.tzld.piaoquan.content.understanding.util.RetryUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -53,18 +54,45 @@ public class GeminiGenerateContentAction implements Action {
|
|
|
geminiParam.setApiKey(apiKey);
|
|
|
geminiParam.setModel(MODEL);
|
|
|
geminiParam.setTemperature(TEMPERATURE);
|
|
|
- log.info("geminiGenerateContentAction geminiParam = {}", JSON.toJSONString(geminiParam));
|
|
|
- Optional<String> optionalS = httpPoolClient.postJson(geminiApiUrl, JSON.toJSONString(geminiParam));
|
|
|
- log.info("geminiGenerateContentAction 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("```", "");
|
|
|
- return content;
|
|
|
+ return RetryUtil.executeWithRetry(() -> {
|
|
|
+ try {
|
|
|
+ log.info("geminiGenerateContentAction geminiParam = {}", JSON.toJSONString(geminiParam));
|
|
|
+ Optional<String> optionalS = httpPoolClient.postJson(geminiApiUrl, JSON.toJSONString(geminiParam));
|
|
|
+ log.info("geminiGenerateContentAction 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("```", "");
|
|
|
+ return content;
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("geminiGenerateContentAction request failure geminiParam = " + JSON.toJSONString(geminiParam));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("geminiGenerateContentAction optionalS is null geminiParam = " + JSON.toJSONString(geminiParam));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("geminiGenerateContentAction error param = {}", e, JSON.toJSONString(geminiParam));
|
|
|
+ throw new RuntimeException("geminiGenerateContentAction request error", e);
|
|
|
}
|
|
|
- }
|
|
|
- return null;
|
|
|
+ }, "geminiGenerateContentAction");
|
|
|
+// try {
|
|
|
+// log.info("geminiGenerateContentAction geminiParam = {}", JSON.toJSONString(geminiParam));
|
|
|
+// Optional<String> optionalS = httpPoolClient.postJson(geminiApiUrl, JSON.toJSONString(geminiParam));
|
|
|
+// log.info("geminiGenerateContentAction 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("```", "");
|
|
|
+// return content;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("geminiGenerateContentAction error param = {}", e, JSON.toJSONString(geminiParam));
|
|
|
+// }
|
|
|
+// return null;
|
|
|
}
|
|
|
}
|