Skip to content

Commit

Permalink
Merge pull request #190 from SPACE-FOR-SPACE/feat/#174
Browse files Browse the repository at this point in the history
힌트 채팅 추가 및 수정
  • Loading branch information
YunChan-Oh authored Nov 28, 2024
2 parents c052264 + 2f2a888 commit 7ab9d61
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,14 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.*;

@Log4j2
@Service
@Transactional
public class ChatTuner {
@Value("${gpt.api.key}")
private String apiKey;

private final String apiKey;
private final RestTemplate restTemplate;
private final String baseUrl = "https://api.openai.com/v1/chat/completions";

Expand Down Expand Up @@ -100,7 +96,7 @@ public ChatTuner(@Value("${gpt.api.key}") String apiKey,
this.systemInstruction = loadSystemInstruction();
}

public AiResponse chatTuneCreator(Long quizId, CreateChatRequest request, Long userId) throws IOException {
public AiResponse chatTuneCreator(Long quizId, CreateChatRequest request, Long userId) {
Quiz quiz = quizReader.findById(quizId);
Users user = userReader.findById(userId);
List<Checklist> checklists = checklistReader.findByQuiz(quiz);
Expand All @@ -112,7 +108,7 @@ public AiResponse chatTuneCreator(Long quizId, CreateChatRequest request, Long u
chatValidator.validateEnglish(userChat);

PromptCreator promptCreator = new PromptCreator();
AiChat aiChat = new AiChat("user", promptCreator.create(request.type(), quiz, checklists, chapter, request.userChat()));
AiChat aiChat = new AiChat("user", promptCreator.create(request.type(), quiz, checklists, chapter, request.userChat(), user.getId()));

HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
Expand All @@ -133,7 +129,6 @@ public AiResponse chatTuneCreator(Long quizId, CreateChatRequest request, Long u
String.class
);

//completion.choices[0].message.content
String responseBody = responseEntity.getBody();
log.info("ResponseBody : " + responseBody);

Expand All @@ -145,10 +140,20 @@ public AiResponse chatTuneCreator(Long quizId, CreateChatRequest request, Long u
totalMapObject.putAll(quiz.getMapObject());
totalMapObject.putAll(chapter.getMapObject());

log.info("bot : " + responseMap.choices().get(0).message().content());
AiResponse botChat = aiResponseJsonParsing.jsonCreator(String.valueOf(responseMap.choices().get(0).message().content()), totalMapObject);
String content = responseMap.choices().get(0).message().content();
log.info("bot : " + content);
AiResponse botChat = aiResponseJsonParsing.jsonCreator(content, totalMapObject);

if(state.isPresent()){
if(request.type().toString().equals("HINT")) {
chatCreator.create(Chat.builder()
.state(state.get())
.userChat(request.userChat())
.botChat(botChat.feedback())
.type(Type.HINT)
.request_order(chatReader.findMaxOrderByState(state.get()) + 1)
.build());
}
else if(state.isPresent()){
chatCreator.create(Chat.builder()
.state(state.get())
.userChat(request.userChat())
Expand All @@ -169,7 +174,6 @@ public AiResponse chatTuneCreator(Long quizId, CreateChatRequest request, Long u
Integer successCount = chatReader.countChatByQuiz(state.get());
stateUpdater.updateSuccess(state.get(), firstChatTime, lastChatTime, successCount);
}

} else {
stateCreator.create(State.createBuilder()
.user(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@RequiredArgsConstructor
public class PromptCreator {

public String create(Type type, Quiz quiz, List<Checklist> checklists, Chapter chapter, String chat) {
public String create(Type type, Quiz quiz, List<Checklist> checklists, Chapter chapter, String chat, Long userAge) {
List<String> checklist = new ArrayList<>();
for (Checklist cl : checklists) {
checklist.add(cl.getContent());
Expand All @@ -30,7 +30,7 @@ public String create(Type type, Quiz quiz, List<Checklist> checklists, Chapter c
"맵은 '"+ Arrays.deepToString(quiz.getMap()) +"', 맵은 무조건 7*7 2차원 배열이야." +
"맵의 오브젝트는 '" + totalMapObject + "', " +
"캐릭터 방향은 '"+ quiz.getCharacterDirection() +"', " +
"입력은 '" + chat + "'" +
"입력은 '" + chat + "', 유저의 나이는 '" + userAge + "'" +
"일 때 정답인지 아닌지와 문제 조건들 중에서 어떤 것들이 맞는지 리스트로 나타내고," +
"입력에 대한 피드백과 움직임을 JSON 형태로 만들어. " +
"결과를 ```json```으로 감싸. 그리고 응답이 왜 그런지도 설명해. 응답이 왜 그런지 설명할 때 처음부터 캐릭터의 방향을 계산하는 것을 먼저 말해." +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public AiResponse create(Long quizId, CreateChatRequest request, Long userId) {
chatValidator.validateEnglish(userChat);

PromptCreator promptCreator = new PromptCreator();
AiChat aiChat = new AiChat("user", promptCreator.create(request.type(), quiz, checklists, chapter, request.userChat()));
AiChat aiChat = new AiChat("user", promptCreator.create(request.type(), quiz, checklists, chapter, request.userChat(), user.getId()));
AiResponse botChat = null;

// state 있다면 대화 로직
Expand Down
Loading

0 comments on commit 7ab9d61

Please sign in to comment.