zyx 2 éve
szülő
commit
9824892a15

+ 1 - 0
build.gradle

@@ -20,6 +20,7 @@ repositories {
 }
 
 dependencies {
+    implementation 'commons-io:commons-io:2.11.0'
     implementation 'com.aliyun.openservices:aliyun-log-producer:0.3.11'
     implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
     implementation 'org.apache.httpcomponents.client5:httpclient5-fluent:5.2.1'

+ 29 - 10
src/main/java/com/sxtvs/api/chatgpt/controller/ChatGptController.java

@@ -6,12 +6,22 @@ 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 lombok.Cleanup;
 import lombok.SneakyThrows;
+import org.apache.commons.io.IOUtils;
 import org.apache.hc.client5.http.classic.HttpClient;
 import org.apache.hc.client5.http.fluent.Request;
 import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
-import org.apache.hc.core5.http.ContentType;
 import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -29,6 +39,8 @@ public class ChatGptController {
     @Autowired
     private AliyunLogger logger;
 
+    private final CloseableHttpClient client = HttpClients.createDefault();
+
     @SneakyThrows
     @RequestMapping("completions")
     public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
@@ -37,18 +49,25 @@ public class ChatGptController {
                 .map(CompletionsRequestDto.Prompt::getText)
                 .collect(Collectors.joining("\n")));
         var params = objectMapper.writeValueAsString(paramsDto);
-        logger.info("request", params,"dto",objectMapper.writeValueAsString(dto));
-        var response = Request.post("https://api.openai.com/v1/completions")
-                .setHeader("Authorization", "Bearer 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")
-                .bodyString(params, ContentType.APPLICATION_JSON)
-                .execute()
-                .returnContent()
-                .asString(StandardCharsets.UTF_8);
-        var gptResponse = objectMapper.readValue(response, GptResponse.class);
+        logger.info("request", params, "dto", objectMapper.writeValueAsString(dto));
+
+        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));
+        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, "服务器走丢了 请重试");
+        }
+        var gptResponse = objectMapper.readValue(result, GptResponse.class);
+
         var completionsResponseDto = new CompletionsResponseDto();
         var text = gptResponse.getChoices().stream().map(GptResponse.ChoicesDTO::getText).findFirst().orElse("").trim();
         completionsResponseDto.setResult(text);
-        logger.info("prompt", dto.getPrompt(), "result", text, "response", response);
         return completionsResponseDto;
     }
 

+ 1 - 1
src/main/java/com/sxtvs/api/chatgpt/dto/CompletionsParamsDto.java

@@ -29,6 +29,6 @@ public class CompletionsParamsDto {
     public CompletionsParamsDto(String prompt) {
         this.prompt = prompt;
         this.model = "text-davinci-003";
-        this.maxTokens = 4000;
+        this.maxTokens = 2048;
     }
 }