Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import MathCaptain.weakness.domain.Chat.dto.request.LLMRequest;
import MathCaptain.weakness.domain.Chat.dto.response.ChatResponse;
import MathCaptain.weakness.domain.Chat.service.ChatService;
import MathCaptain.weakness.domain.User.entity.Users;
import MathCaptain.weakness.global.annotation.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
Expand Down Expand Up @@ -41,9 +39,7 @@ public void handleChat(ChatRequest request) {
template.convertAndSend("/sub/" + request.getUserId(), chatResponse);

// 2) LLM 호출 & 응답 저장 + 브로드캐스트 (동기 방식)
List<ChatResponse> llmResponses = chatService.askAI(request);
llmResponses.forEach(aiResp ->
template.convertAndSend("/sub/" + request.getUserId(), aiResp)
);
ChatResponse llmResponse = chatService.askAI(request);
template.convertAndSend("/sub/" + request.getUserId(), llmResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Prompt {
private List<ChatResponse> history;

private Prompt(Long userId, String message, List<ChatResponse> history) {
this.template = "다음 내용을 참고하여 답변해줘";
this.template = "질문의 내용에 답변을 할 때 사용자의 활동기록과 문서에서 검색된 내용이 필요하면 참고해서 답변해줘";
this.inputs = Map.of(
"user_id", userId.toString(),
"user_question", message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Chat {
@Enumerated(EnumType.STRING)
private ChatRole role;

@Column(columnDefinition = "LONGTEXT")
private String message;

@CreatedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Transactional
public class ChatService {

private final ChatRepository chatRepository;
Expand All @@ -28,15 +28,13 @@ public ChatResponse saveUserMessage(ChatRequest request) {
return ChatResponse.of(saved);
}

public List<ChatResponse> askAI(ChatRequest request) {
public ChatResponse askAI(ChatRequest request) {
List<ChatResponse> history = chatRepository.findAllByUserIdOrderBySendTimeAsc(request.getUserId()).stream()
.map(ChatResponse::of)
.toList();

List<Chat> aiChats = llm.call(history, request);
return aiChats.stream()
.map(this::storeAndTransform)
.toList();
Chat aiChats = llm.call(history, request);
return storeAndTransform(aiChats);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
Expand All @@ -31,33 +32,31 @@ public class LLMClient {
@Value("${llm.server.url}")
private String baseUrl;

public List<Chat> call(List<ChatResponse> history, ChatRequest request) {
public Chat call(List<ChatResponse> history, ChatRequest request) {
try {
LLMRequest llmRequest = LLMRequest.of(request, history);

ResponseEntity<ChatResponse[]> response = restTemplate.postForEntity(
ResponseEntity<ChatResponse> response = restTemplate.postForEntity(
// TODO : 엔드포인트 수정
baseUrl + "/v1/chat",
baseUrl + "/mcp/query",
llmRequest,
// 응답을 ChatResponse[] (배열)로 역직렬화 (수정 필요)
ChatResponse[].class
ChatResponse.class
);

ChatResponse[] body = response.getBody();
ChatResponse body = response.getBody();
if (body == null) {
return Collections.emptyList();
throw new HttpServerErrorException(HttpStatus.NOT_ACCEPTABLE, "LLM이 응답하지 않습니다");
}
return Arrays.stream(body)
.map(Chat::of)
.collect(Collectors.toList());
return Chat.of(body);

} catch (ResourceAccessException timeoutEx) {
log.error("LLM 서버 응답 지연", timeoutEx);
return List.of();
throw new ResourceAccessException("LLM 서버 응답 지연");
} catch (HttpClientErrorException | HttpServerErrorException httpEx) {
// 4XX/5XX 응답
log.error("LLM 호출 실패: {}", httpEx.getStatusCode(), httpEx);
return List.of();
throw new HttpClientErrorException(HttpStatus.NOT_ACCEPTABLE, "LLM 호출 실패");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package MathCaptain.weakness.domain.common.enums;

import com.fasterxml.jackson.annotation.JsonCreator;

public enum ChatRole {
USER,
ASSISTANT
ASSISTANT;

@JsonCreator
public static ChatRole fromValue(String value) {
return ChatRole.valueOf(value.toUpperCase());
}
}