Skip to content

Commit

Permalink
Merge pull request #58 from IT-Cotato/feat/chat-bot-#24
Browse files Browse the repository at this point in the history
Feat : 챗봇 기능을 수정합니다 (#24)
  • Loading branch information
chaen-ing authored Feb 6, 2025
2 parents 4a1db23 + f79ae73 commit f05467d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ResponseEntity<ApiResponse<Object>> sendMessage(

@Operation(
summary = "대화 내역 조회",
description = "챗봇과의 대화 내역을 조회합니다. 페이지네이션을 지원합니다. 페이지당 10개의 대화 내역을 반환합니다.")
description = "챗봇과의 대화 내역을 조회합니다. 페이지네이션을 지원합니다. 페이지당 10개의 대화 내역을 반환합니다. 최신 순으로 정렬됩니다.")
@GetMapping("/list")
public ResponseEntity<ApiResponse<Object>> getMessages(
final @AuthenticationPrincipal CustomUserDetails currentUser,
Expand All @@ -66,4 +66,13 @@ public ResponseEntity<ApiResponse<Object>> clearMessages(

return ResponseEntity.status(HttpStatus.OK).body(ApiResponse.from(ApiResponse.EMPTY_RESPONSE));
}

@Operation(summary = "이용 꿀팁 조회", description = "이용 꿀팁을 조회합니다.")
@GetMapping("/tips")
public ResponseEntity<ApiResponse<Object>> getTips(
final @AuthenticationPrincipal CustomUserDetails currentUser) {

return ResponseEntity.status(HttpStatus.OK)
.body(ApiResponse.from(chatbotService.getTips(currentUser.getId())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ChatMessage extends BaseEntity {
private Long id;

@Size(min = 1)
@Column(name = "message", nullable = false)
@Column(name = "message", nullable = false, columnDefinition = "TEXT")
private String message;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.ripple.BE.chatbot.repository;

import com.ripple.BE.chatbot.domain.ChatMessage;
import java.time.LocalDateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChatbotRepository extends JpaRepository<ChatMessage, Long> {
Page<ChatMessage> findAllByUserIdOrderByCreatedDate(Long userId, Pageable pageable);
Page<ChatMessage> findAllByUserIdOrderByCreatedDateDesc(Long userId, Pageable pageable);

void deleteAllByUserId(Long userId);

void deleteAllByCreatedDateBefore(LocalDateTime createdDate);
}
47 changes: 45 additions & 2 deletions src/main/java/com/ripple/BE/chatbot/service/ChatbotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import static com.ripple.BE.user.exception.errorcode.UserErrorCode.*;

import java.time.LocalDateTime;

import com.ripple.BE.chatbot.domain.ChatMessage;
import com.ripple.BE.chatbot.domain.type.Sender;
import com.ripple.BE.chatbot.dto.ChatDTO;
import com.ripple.BE.chatbot.dto.ChatListDTO;
import com.ripple.BE.chatbot.dto.response.ChatListResponse;
import com.ripple.BE.chatbot.dto.response.ChatResponse;
import com.ripple.BE.chatbot.repository.ChatbotRepository;
import com.ripple.BE.user.domain.User;
Expand All @@ -20,6 +21,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -35,6 +37,19 @@ public class ChatbotService {

private static final int PAGE_SIZE = 10;

private static final String TIPS = """
리플의 AI 챗봇을 200% 활용하는 프롬프트 꿀팁
1. 명확하고 구체적으로 작성하기
- 무엇을 원하는지 구체적으로 설명해 보세요!
- 예시 : "복리를 이해하기 위해, 연 5% 이자율로 3년 동안 100만 원이 어떻게 증가하는지 구체적으로 계산해줘."
2. 배경 정보 제공하기
- 질문이나 질문자의 배경 정보를 알려주세요!
- 예시 : "경제를 공부하는 대학생인데, 단리와 복리의 차이를 쉽게 이해할 수 있도록 설명해줘."
3. 결과물 형식 명시하기
- 결과물을 어떤 형태로 제공받고 싶은지 알려주세요!
- 예시 : “단리와 복리의 차이를 표로 정리하고, 간단한 계산 예를 포함해 설명해줘.\"""";

@Transactional
public ChatResponse sendMessage(final ChatDTO chatDTO, final Long userId) {
User user = userRepository.findById(userId)
Expand Down Expand Up @@ -77,7 +92,7 @@ public ChatListDTO getChatList(final Long userId, final int page) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserException(USER_NOT_FOUND));

Page<ChatMessage> chatMessagePage = chatbotRepository.findAllByUserIdOrderByCreatedDate(
Page<ChatMessage> chatMessagePage = chatbotRepository.findAllByUserIdOrderByCreatedDateDesc(
user.getId(), pageable);

return ChatListDTO.toChatListDTO(chatMessagePage);
Expand All @@ -90,4 +105,32 @@ public void clearChat(final Long userId) {

chatbotRepository.deleteAllByUserId(user.getId());
}

@Transactional
public String getTips(final Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserException(USER_NOT_FOUND));

// 챗봇의 응답 저장
chatbotRepository.save(
ChatMessage.builder()
.user(user)
.message(TIPS)
.sender(Sender.CHATBOT)
.build());

return TIPS;
}

@Scheduled(cron = "0 0 4 * * ?") // 매일 4시에 실행
@Transactional
public void scheduledMessageCleanUp() {
deleteOldChatMessages();
log.info("✅ Chat messages older than 30 days have been deleted.");
}

private void deleteOldChatMessages() {
chatbotRepository.deleteAllByCreatedDateBefore(LocalDateTime.now().minusDays(30));
}

}

0 comments on commit f05467d

Please sign in to comment.