Skip to content

Commit ca19941

Browse files
authored
[fix] 채팅 - LLM 서버 연결 및 오류 수정 (#60)
2 parents a710b6a + f236511 commit ca19941

6 files changed

Lines changed: 26 additions & 25 deletions

File tree

MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Chat/controller/ChatController.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import MathCaptain.weakness.domain.Chat.dto.request.LLMRequest;
55
import MathCaptain.weakness.domain.Chat.dto.response.ChatResponse;
66
import MathCaptain.weakness.domain.Chat.service.ChatService;
7-
import MathCaptain.weakness.domain.User.entity.Users;
8-
import MathCaptain.weakness.global.annotation.LoginUser;
97
import lombok.RequiredArgsConstructor;
108
import org.springframework.messaging.handler.annotation.MessageMapping;
119
import org.springframework.messaging.simp.SimpMessagingTemplate;
@@ -41,9 +39,7 @@ public void handleChat(ChatRequest request) {
4139
template.convertAndSend("/sub/" + request.getUserId(), chatResponse);
4240

4341
// 2) LLM 호출 & 응답 저장 + 브로드캐스트 (동기 방식)
44-
List<ChatResponse> llmResponses = chatService.askAI(request);
45-
llmResponses.forEach(aiResp ->
46-
template.convertAndSend("/sub/" + request.getUserId(), aiResp)
47-
);
42+
ChatResponse llmResponse = chatService.askAI(request);
43+
template.convertAndSend("/sub/" + request.getUserId(), llmResponse);
4844
}
4945
}

MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Chat/dto/request/Prompt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Prompt {
1818
private List<ChatResponse> history;
1919

2020
private Prompt(Long userId, String message, List<ChatResponse> history) {
21-
this.template = "다음 내용을 참고하여 답변해줘";
21+
this.template = "질문의 내용에 답변을 할 때 사용자의 활동기록과 문서에서 검색된 내용이 필요하면 참고해서 답변해줘";
2222
this.inputs = Map.of(
2323
"user_id", userId.toString(),
2424
"user_question", message

MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Chat/entity/Chat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Chat {
2929
@Enumerated(EnumType.STRING)
3030
private ChatRole role;
3131

32+
@Column(columnDefinition = "LONGTEXT")
3233
private String message;
3334

3435
@CreatedDate

MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Chat/service/ChatService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@Slf4j
1616
@Service
1717
@RequiredArgsConstructor
18-
@Transactional(readOnly = true)
18+
@Transactional
1919
public class ChatService {
2020

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

31-
public List<ChatResponse> askAI(ChatRequest request) {
31+
public ChatResponse askAI(ChatRequest request) {
3232
List<ChatResponse> history = chatRepository.findAllByUserIdOrderBySendTimeAsc(request.getUserId()).stream()
3333
.map(ChatResponse::of)
3434
.toList();
3535

36-
List<Chat> aiChats = llm.call(history, request);
37-
return aiChats.stream()
38-
.map(this::storeAndTransform)
39-
.toList();
36+
Chat aiChats = llm.call(history, request);
37+
return storeAndTransform(aiChats);
4038
}
4139

4240
@Transactional

MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Chat/service/LLMClient.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import groovy.util.logging.Slf4j;
99
import lombok.RequiredArgsConstructor;
1010
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.http.HttpStatus;
1112
import org.springframework.http.ResponseEntity;
1213
import org.springframework.stereotype.Component;
1314
import org.springframework.web.client.HttpClientErrorException;
@@ -31,33 +32,31 @@ public class LLMClient {
3132
@Value("${llm.server.url}")
3233
private String baseUrl;
3334

34-
public List<Chat> call(List<ChatResponse> history, ChatRequest request) {
35+
public Chat call(List<ChatResponse> history, ChatRequest request) {
3536
try {
3637
LLMRequest llmRequest = LLMRequest.of(request, history);
3738

38-
ResponseEntity<ChatResponse[]> response = restTemplate.postForEntity(
39+
ResponseEntity<ChatResponse> response = restTemplate.postForEntity(
3940
// TODO : 엔드포인트 수정
40-
baseUrl + "/v1/chat",
41+
baseUrl + "/mcp/query",
4142
llmRequest,
4243
// 응답을 ChatResponse[] (배열)로 역직렬화 (수정 필요)
43-
ChatResponse[].class
44+
ChatResponse.class
4445
);
4546

46-
ChatResponse[] body = response.getBody();
47+
ChatResponse body = response.getBody();
4748
if (body == null) {
48-
return Collections.emptyList();
49+
throw new HttpServerErrorException(HttpStatus.NOT_ACCEPTABLE, "LLM이 응답하지 않습니다");
4950
}
50-
return Arrays.stream(body)
51-
.map(Chat::of)
52-
.collect(Collectors.toList());
51+
return Chat.of(body);
5352

5453
} catch (ResourceAccessException timeoutEx) {
5554
log.error("LLM 서버 응답 지연", timeoutEx);
56-
return List.of();
55+
throw new ResourceAccessException("LLM 서버 응답 지연");
5756
} catch (HttpClientErrorException | HttpServerErrorException httpEx) {
5857
// 4XX/5XX 응답
5958
log.error("LLM 호출 실패: {}", httpEx.getStatusCode(), httpEx);
60-
return List.of();
59+
throw new HttpClientErrorException(HttpStatus.NOT_ACCEPTABLE, "LLM 호출 실패");
6160
}
6261
}
6362
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package MathCaptain.weakness.domain.common.enums;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
35
public enum ChatRole {
46
USER,
5-
ASSISTANT
7+
ASSISTANT;
8+
9+
@JsonCreator
10+
public static ChatRole fromValue(String value) {
11+
return ChatRole.valueOf(value.toUpperCase());
12+
}
613
}

0 commit comments

Comments
 (0)