|
@@ -51,29 +51,21 @@ public class ChatGptService {
|
|
}
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
- private synchronized String request(String params) {
|
|
|
|
- while (true) {
|
|
|
|
- String result;
|
|
|
|
- var httpPost = new HttpPost("https://api.openai.com/v1/completions");
|
|
|
|
- httpPost.setHeader("Authorization", "Bearer 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");
|
|
|
|
- httpPost.setEntity(new StringEntity(params, ContentType.APPLICATION_JSON));
|
|
|
|
- @Cleanup
|
|
|
|
- var response = client.execute(httpPost);
|
|
|
|
- @Cleanup
|
|
|
|
- var content = response.getEntity().getContent();
|
|
|
|
- result = IOUtils.toString(content, StandardCharsets.UTF_8);
|
|
|
|
- if (result.contains("The server had an error while processing your request")) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- logger.info("response", result);
|
|
|
|
- if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_REDIRECTION) {
|
|
|
|
- logger.error("error", result);
|
|
|
|
- throw new BizException(1000, "当前访问人数过多,请重试。");
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
-
|
|
|
|
|
|
+ private String request(String params) {
|
|
|
|
+ var httpPost = new HttpPost("https://api.openai.com/v1/completions");
|
|
|
|
+ httpPost.setHeader("Authorization", "Bearer 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");
|
|
|
|
+ httpPost.setEntity(new StringEntity(params, ContentType.APPLICATION_JSON));
|
|
|
|
+ @Cleanup
|
|
|
|
+ var response = client.execute(httpPost);
|
|
|
|
+ @Cleanup
|
|
|
|
+ var content = response.getEntity().getContent();
|
|
|
|
+ var result = IOUtils.toString(content, StandardCharsets.UTF_8);
|
|
|
|
+ logger.info("response", result);
|
|
|
|
+ if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_REDIRECTION) {
|
|
|
|
+ logger.error("error", result);
|
|
|
|
+ throw new BizException(1000, "当前访问人数过多,请重试。");
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|