|
@@ -5,6 +5,7 @@ import com.sxtvs.api.chatgpt.dto.CompletionsParamsDto;
|
|
import com.sxtvs.api.chatgpt.dto.CompletionsRequestDto;
|
|
import com.sxtvs.api.chatgpt.dto.CompletionsRequestDto;
|
|
import com.sxtvs.api.chatgpt.dto.CompletionsResponseDto;
|
|
import com.sxtvs.api.chatgpt.dto.CompletionsResponseDto;
|
|
import com.sxtvs.api.chatgpt.dto.GptResponse;
|
|
import com.sxtvs.api.chatgpt.dto.GptResponse;
|
|
|
|
+import com.sxtvs.api.youmei.service.YoumeiAccountServiceImpl;
|
|
import com.sxtvs.core.advice.BizException;
|
|
import com.sxtvs.core.advice.BizException;
|
|
import com.sxtvs.core.sls.AliyunLogger;
|
|
import com.sxtvs.core.sls.AliyunLogger;
|
|
import lombok.Cleanup;
|
|
import lombok.Cleanup;
|
|
@@ -32,6 +33,9 @@ public class ChatGptService {
|
|
@Autowired
|
|
@Autowired
|
|
private ObjectMapper objectMapper;
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private YoumeiAccountServiceImpl youmeiAccountService;
|
|
|
|
+
|
|
private final CloseableHttpClient client = HttpClients.createDefault();
|
|
private final CloseableHttpClient client = HttpClients.createDefault();
|
|
|
|
|
|
|
|
|
|
@@ -49,9 +53,7 @@ public class ChatGptService {
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
private synchronized String request(String params) {
|
|
private synchronized String request(String params) {
|
|
while (true) {
|
|
while (true) {
|
|
-// var token = queue.poll(1, TimeUnit.DAYS);
|
|
|
|
String result;
|
|
String result;
|
|
-// try {
|
|
|
|
var httpPost = new HttpPost("https://api.openai.com/v1/completions");
|
|
var httpPost = new HttpPost("https://api.openai.com/v1/completions");
|
|
httpPost.setHeader("Authorization", "Bearer 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");
|
|
httpPost.setHeader("Authorization", "Bearer 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");
|
|
httpPost.setEntity(new StringEntity(params, ContentType.APPLICATION_JSON));
|
|
httpPost.setEntity(new StringEntity(params, ContentType.APPLICATION_JSON));
|
|
@@ -66,12 +68,10 @@ public class ChatGptService {
|
|
logger.info("response", result);
|
|
logger.info("response", result);
|
|
if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_REDIRECTION) {
|
|
if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_REDIRECTION) {
|
|
logger.error("error", result);
|
|
logger.error("error", result);
|
|
- throw new BizException(1000, "服务器走丢了 请重试");
|
|
|
|
|
|
+ throw new BizException(1000, "当前访问人数过多,请重试。");
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|
|
-// } finally {
|
|
|
|
-// queue.put(token);
|
|
|
|
-// }
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -79,7 +79,8 @@ public class ChatGptService {
|
|
|
|
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
|
|
public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
|
|
- var paramsDto = new CompletionsParamsDto(dto.toText());
|
|
|
|
|
|
+ var text = dto.toText();
|
|
|
|
+ var paramsDto = new CompletionsParamsDto(text);
|
|
var params = objectMapper.writeValueAsString(paramsDto);
|
|
var params = objectMapper.writeValueAsString(paramsDto);
|
|
logger.info("key", "completions",
|
|
logger.info("key", "completions",
|
|
"request", params,
|
|
"request", params,
|
|
@@ -88,13 +89,15 @@ public class ChatGptService {
|
|
"userName", dto.getUserName()
|
|
"userName", dto.getUserName()
|
|
);
|
|
);
|
|
|
|
|
|
- var result = request(params);
|
|
|
|
-
|
|
|
|
- var gptResponse = objectMapper.readValue(result, GptResponse.class);
|
|
|
|
|
|
+ if (youmeiAccountService.wordCheckCache(text)) {
|
|
|
|
+ throw new BizException(1001, "您的问题不符合相关法律法规,请修改后重试。");
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ var body = request(params);
|
|
|
|
+ var gptResponse = objectMapper.readValue(body, GptResponse.class);
|
|
var completionsResponseDto = new CompletionsResponseDto();
|
|
var completionsResponseDto = new CompletionsResponseDto();
|
|
- var text = gptResponse.getChoices().stream().map(GptResponse.ChoicesDTO::getText).findFirst().orElse("").trim();
|
|
|
|
- completionsResponseDto.setResult(text);
|
|
|
|
|
|
+ var result = gptResponse.getChoices().stream().map(GptResponse.ChoicesDTO::getText).findFirst().orElse("").trim();
|
|
|
|
+ completionsResponseDto.setResult(result);
|
|
return completionsResponseDto;
|
|
return completionsResponseDto;
|
|
}
|
|
}
|
|
}
|
|
}
|