Skip to content

Commit

Permalink
feat: 첫문장 필드 추가, 길이 제한, 형식 변경 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
pingowl authored Jul 27, 2024
1 parent a7d3ec7 commit 37c5dd9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/main/java/mychat/controller/GPTController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import mychat.dto.request.GPTRequest;
import mychat.dto.response.GPTResponse;
import lombok.RequiredArgsConstructor;
import mychat.service.ChatRoomService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
Expand All @@ -14,6 +16,8 @@
@RequiredArgsConstructor
public class GPTController {

private final ChatRoomService chatRoomService;

@Value("${gpt.model}")
private String model;

Expand All @@ -22,9 +26,9 @@ public class GPTController {
private final RestTemplate restTemplate;


@GetMapping("/chat")
public String chat(){
String prompt = "릴레이로 소설 작성할건데 소설의 첫 줄 써줄래?";
@GetMapping("/chat/{roomId}")
public String chat(@PathVariable("roomId") String roomId){
String prompt = "릴레이로 소설 작성할건데 소설의 첫 줄 써줄래? 20자 이내로";

GPTRequest request = new GPTRequest(
model,prompt,1,256,1,2,2);
Expand All @@ -35,9 +39,21 @@ public String chat(){
, GPTResponse.class
);


return gptResponse.getChoices().get(0).getMessage().getContent();

String firstSentence = gptResponse.getChoices().get(0).getMessage().getContent();
// 입력 문자열을 10글자씩 잘라서 줄바꿈 추가
StringBuilder sb = new StringBuilder();
int length = firstSentence.length();
int chunkSize = 10; // 줄바꿈을 추가할 기준 길이
for (int i = 0; i < length; i += chunkSize) {
// 현재 위치에서 chunkSize만큼 잘라서 추가
int end = Math.min(length, i + chunkSize);
sb.append(firstSentence, i, end);
sb.append('\n'); // 줄바꿈 추가
}
firstSentence = sb.toString();

chatRoomService.setFirstSentence(roomId, firstSentence);
return firstSentence;

}
}
5 changes: 5 additions & 0 deletions src/main/java/mychat/domain/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ChatRoom {
private int maxUserCnt; // 채팅방 최대 인원 제한
@Builder.Default
private boolean done=false;
private String firstSentence;

// 채팅방 삭제시 방장이 비밀번호 입력해야함

Expand Down Expand Up @@ -56,4 +57,8 @@ public boolean existsMember(String nickname){
public void setAsDone(){
done = true;
}

public void setFirstSentence(String firstSentence){
this.firstSentence = firstSentence;
}
}
6 changes: 6 additions & 0 deletions src/main/java/mychat/service/ChatRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ public List<ChatRoom> getAllChatRooms() {
private void deleteChatRoom(String roomId){
chatRoomRepository.deleteById(roomId);
}

public void setFirstSentence(String roomId, String firstSentence){
ChatRoom room = chatRoomRepository.findById(roomId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CHATROOM));
room.setFirstSentence(firstSentence);
}
}

0 comments on commit 37c5dd9

Please sign in to comment.