|
@@ -7,7 +7,9 @@ import com.sxtvs.api.chatgpt.dto.CompletionsResponseDto;
|
|
import com.sxtvs.api.chatgpt.dto.GptResponse;
|
|
import com.sxtvs.api.chatgpt.dto.GptResponse;
|
|
import com.sxtvs.core.sls.AliyunLogger;
|
|
import com.sxtvs.core.sls.AliyunLogger;
|
|
import lombok.SneakyThrows;
|
|
import lombok.SneakyThrows;
|
|
|
|
+import org.apache.hc.client5.http.classic.HttpClient;
|
|
import org.apache.hc.client5.http.fluent.Request;
|
|
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.ContentType;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
@@ -25,13 +27,17 @@ public class ChatGptController {
|
|
@Autowired
|
|
@Autowired
|
|
private AliyunLogger logger;
|
|
private AliyunLogger logger;
|
|
|
|
|
|
|
|
+ private HttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
|
+
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
@RequestMapping("completions")
|
|
@RequestMapping("completions")
|
|
public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
|
|
public CompletionsResponseDto completions(@RequestBody CompletionsRequestDto dto) {
|
|
var paramsDto = new CompletionsParamsDto(dto.getPrompt());
|
|
var paramsDto = new CompletionsParamsDto(dto.getPrompt());
|
|
|
|
+ var params = objectMapper.writeValueAsString(paramsDto);
|
|
|
|
+ logger.info("prompt", dto.getPrompt(), "request", params);
|
|
var response = Request.post("https://api.openai.com/v1/completions")
|
|
var response = Request.post("https://api.openai.com/v1/completions")
|
|
.setHeader("Authorization", "Bearer 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")
|
|
.setHeader("Authorization", "Bearer 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")
|
|
- .bodyString(objectMapper.writeValueAsString(paramsDto), ContentType.APPLICATION_JSON)
|
|
|
|
|
|
+ .bodyString(params, ContentType.APPLICATION_JSON)
|
|
.execute()
|
|
.execute()
|
|
.returnContent()
|
|
.returnContent()
|
|
.asString(StandardCharsets.UTF_8);
|
|
.asString(StandardCharsets.UTF_8);
|