diff --git a/.gradle/8.5/fileHashes/fileHashes.lock b/.gradle/8.5/fileHashes/fileHashes.lock index ce1ce011..a9343789 100644 Binary files a/.gradle/8.5/fileHashes/fileHashes.lock and b/.gradle/8.5/fileHashes/fileHashes.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index dd3234cb..73412621 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/build.gradle b/build.gradle index b02048c5..27eb0012 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ repositories { } dependencies { + implementation 'org.json:json:20220320' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/build/classes/java/main/com/kuit/chatdiary/controller/UserController.class b/build/classes/java/main/com/kuit/chatdiary/controller/UserController.class index 0f15d2cd..fc8ce2e8 100644 Binary files a/build/classes/java/main/com/kuit/chatdiary/controller/UserController.class and b/build/classes/java/main/com/kuit/chatdiary/controller/UserController.class differ diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index 2d83a78c..4cd66a17 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/main/java/com/kuit/chatdiary/controller/ChatController.java b/src/main/java/com/kuit/chatdiary/controller/ChatController.java new file mode 100644 index 00000000..cde5ec09 --- /dev/null +++ b/src/main/java/com/kuit/chatdiary/controller/ChatController.java @@ -0,0 +1,50 @@ +package com.kuit.chatdiary.controller; + +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import java.util.Map; + +@RestController +public class ChatController { + + @Value("${OPEN_AI_KEY}") + private String OPEN_AI_KEY; + @PostMapping("/ask") + public ResponseEntity ask(@RequestBody Map prompt) { + RestTemplate restTemplate = new RestTemplate(); + String apiUrl = "https://api.openai.com/v1/chat/completions"; + + // OpenAI 요청 본문 구성 + JSONObject body = new JSONObject(); + body.put("model", "gpt-3.5-turbo"); + body.put("messages", new JSONObject[] { + new JSONObject().put("role", "system").put("content", "You are a playful and curious teenager. No matter the topic, you find everything fascinating and are always eager to ask lots of questions. korean"), + new JSONObject().put("role", "user").put("content", prompt.get("prompt")) + }); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.setBearerAuth(OPEN_AI_KEY); + + + HttpEntity entity = new HttpEntity<>(body.toString(), headers); + + try { + ResponseEntity response = restTemplate.postForEntity(apiUrl, entity, String.class); + System.out.println("API Response: " + response.getBody()); + return response; + } catch (Exception e) { + return ResponseEntity.status(500).body("Internal Server Error"); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/kuit/chatdiary/controller/UserController.java b/src/main/java/com/kuit/chatdiary/controller/UserController.java index e23ad3bc..541a3a1a 100644 --- a/src/main/java/com/kuit/chatdiary/controller/UserController.java +++ b/src/main/java/com/kuit/chatdiary/controller/UserController.java @@ -1,12 +1,17 @@ package com.kuit.chatdiary.controller; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -@RestController +@Controller public class UserController { + @GetMapping("/chat") + public String chat() { + return "redirect:/chat.html"; + } @GetMapping("/user") public String user() { - return "user"; + return "redirect:/user.html"; } } diff --git a/src/main/resources/static/chat.html b/src/main/resources/static/chat.html new file mode 100644 index 00000000..1e753808 --- /dev/null +++ b/src/main/resources/static/chat.html @@ -0,0 +1,147 @@ + + + + + + user + + + + + +



+



+
+
+ +
+ + +
+ + + + + + + \ No newline at end of file