|
@@ -1,9 +1,12 @@
|
|
|
package com.sxtvs.api.chatgpt.service;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.sxtvs.api.chatgpt.dto.*;
|
|
|
+import com.sxtvs.api.chatgpt.dto.CompletionsParamsDto;
|
|
|
+import com.sxtvs.api.chatgpt.dto.CompletionsRequestDto;
|
|
|
+import com.sxtvs.api.chatgpt.dto.CompletionsResponseDto;
|
|
|
+import com.sxtvs.api.chatgpt.dto.GptResponse;
|
|
|
import com.sxtvs.core.sls.AliyunLogger;
|
|
|
-import com.sxtvs.core.sls.advice.BizException;
|
|
|
+import com.sxtvs.core.advice.BizException;
|
|
|
import lombok.Cleanup;
|
|
|
import lombok.SneakyThrows;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
@@ -18,13 +21,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
-import java.util.concurrent.SynchronousQueue;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-import java.util.concurrent.locks.Lock;
|
|
|
-import java.util.concurrent.locks.ReentrantLock;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -38,6 +35,7 @@ public class ChatGptService {
|
|
|
|
|
|
private final CloseableHttpClient client = HttpClients.createDefault();
|
|
|
|
|
|
+
|
|
|
private final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
|
|
|
|
|
|
{
|
|
@@ -50,7 +48,7 @@ public class ChatGptService {
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
|
- private String request(String params) {
|
|
|
+ private synchronized String request(String params) {
|
|
|
while (true) {
|
|
|
// var token = queue.poll(1, TimeUnit.DAYS);
|
|
|
String result;
|
|
@@ -81,7 +79,7 @@ public class ChatGptService {
|
|
|
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
|
|
|
+ public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
|
|
|
var paramsDto = new CompletionsParamsDto(dto.getPrompt()
|
|
|
.stream()
|
|
|
.map(CompletionsRequestDto.Prompt::getText)
|