Skip to content

Commit

Permalink
Fix: Chat-300-BE-이미지-처리-수정 (#54)
Browse files Browse the repository at this point in the history
* Fix: Chat 테이블에서 최근 채팅 가져오는 로직 수정

* Chore: 최근 채팅 처리 중 에러 발생 시 디버깅을 위한 로그 추가

* Fix: getRecentChats 이미지일경우 무시

* Fix: getRecentChats 이미지 오류 수정

recentchats 에 이미지는 무시.
단, 가장 마지막 채팅이 이미지인경우(지금 보낸게 이미지인경우)는 읽음

---------

Co-authored-by: dainshon <[email protected]>
Co-authored-by: dainshon <[email protected]>
  • Loading branch information
3 people authored Feb 16, 2024
1 parent d083e7a commit f54d717
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface ChatRepository extends JpaRepository<Chat, Long> {

List<Chat> findTop10ByMember_UserIdOrderByChatIdAsc(Long userId);
List<Chat> findTop10ByMember_UserIdOrderByChatIdDesc(Long userId);
List<Chat> findTop10ByChatIdGreaterThan(Long lastChatId);
List<Chat> findTopByMember_UserIdOrderByChatIdDesc(Long userId);
}
27 changes: 23 additions & 4 deletions src/main/java/com/kuit/chatdiary/service/OpenAIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
Expand Down Expand Up @@ -71,6 +72,8 @@ public String getCompletion(Long userId, Integer model, Member member) {

HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);

log.info(request.toString());

try {
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
return response.getBody();
Expand All @@ -80,8 +83,18 @@ public String getCompletion(Long userId, Integer model, Member member) {
}
}

public List<Map<String, Object>> getRecentMessages(Long userId) {
List<Chat> recentChats = chatRepository.findTop10ByMember_UserIdOrderByChatIdAsc(userId);

public List<Map<String, Object>> getRecentChats(Long userId) {
List<Chat> recentChats = chatRepository.findTop10ByMember_UserIdOrderByChatIdDesc(userId);

Collections.reverse(recentChats);

String ChatIdsFromRecentChats = recentChats.stream()
.map(chat -> String.valueOf(chat.getChatId()))
.collect(Collectors.joining(", "));

log.info("OpenAIService.getRecentChats, recentChats chatIds: {}", ChatIdsFromRecentChats);


List<Map<String, Object>> previousMessages = new ArrayList<>();
for (Chat chat : recentChats) {
Expand All @@ -93,8 +106,14 @@ public List<Map<String, Object>> getRecentMessages(Long userId) {
if (ChatType.CHAT.equals(chat.getChatType())) {
message.put("content", List.of(getTextPart(chat.getContent())));
} else if (ChatType.IMG.equals(chat.getChatType())) {
log.info("OpenAIService.getRecentChats, IMG");
message.put("content", List.of(getImagePart(chat.getContent())));
log.info("OpenAIService.getRecentChats, IMG chatId: {}", chat.getChatId());
if(chat.getContent().equals(recentChats.get(recentChats.size()-1).getContent())){
message.put("content", List.of(getImagePart(chat.getContent())));
}else{
continue;
}


}
} else {
message.put("role", "assistant");
Expand Down

0 comments on commit f54d717

Please sign in to comment.