zyx 2 年之前
父節點
當前提交
c86a6a3ade
共有 2 個文件被更改,包括 14 次插入9 次删除
  1. 2 1
      build.gradle
  2. 12 8
      src/main/java/com/sxtvs/chatgpt/api/chatgpt/controller/ChatGptController.java

+ 2 - 1
build.gradle

@@ -19,7 +19,8 @@ repositories {
 }
 
 dependencies {
-    implementation 'cn.hutool:hutool-all:5.8.12'
+    implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
+    implementation 'org.apache.httpcomponents.client5:httpclient5-fluent:5.2.1'
     implementation 'org.springframework.boot:spring-boot-starter-data-redis'
     implementation 'org.springframework.boot:spring-boot-starter-web'
     compileOnly 'org.projectlombok:lombok'

+ 12 - 8
src/main/java/com/sxtvs/chatgpt/api/chatgpt/controller/ChatGptController.java

@@ -1,28 +1,32 @@
 package com.sxtvs.chatgpt.api.chatgpt.controller;
 
 import cn.hutool.http.HttpUtil;
+import lombok.SneakyThrows;
+import org.apache.hc.client5.http.fluent.Request;
+import org.apache.hc.core5.http.ContentType;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 public class ChatGptController {
 
+    @SneakyThrows
     @RequestMapping("hello")
     public void hello() {
-        var response = HttpUtil.createGet("https://api.openai.com/v1/completions")
-                .header("Content-Type", "application/json")
-                .header("Authorization", "Bearer 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")
-                .body("""
+        var response = Request.post("https://api.openai.com/v1/completions")
+                .setHeader("Authorization", "Bearer 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")
+                .bodyString("""
                         {
                           "model": "text-davinci-003",
                           "prompt": "Say this is a test",
                           "max_tokens": 7,
                           "temperature": 0
                         }
-                        """)
-                .execute();
-        System.out.println(response.body());
-        response.close();
+                        """, ContentType.APPLICATION_JSON)
+                .execute()
+                .returnContent()
+                .toString();
+        System.out.println(response);
     }
 
 }