Skip to content

Commit 6b01ce9

Browse files
authored
Merge pull request #241 from prgrms-web-devcourse-final-project/ref/chat
refactor[chat]: 채팅 응답 형식 변경
2 parents 5f77afb + 63842fd commit 6b01ce9

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

backend/src/main/java/com/ai/lawyer/domain/chatbot/dto/ChatDto.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.springframework.ai.document.Document;
99

1010
import java.time.LocalDateTime;
11-
import java.util.ArrayList;
12-
import java.util.List;
1311

1412
@Schema(description = "채팅 관련 DTO")
1513
public class ChatDto {
@@ -42,11 +40,10 @@ public static class ChatResponse {
4240
@Schema(description = "AI 챗봇의 응답 메시지", example = "네, 관련 법령과 판례를 바탕으로 답변해 드리겠습니다.")
4341
private String message;
4442

45-
@Schema(description = "응답 생성에 참고한 유사 판례 정보 목록")
46-
private List<Document> similarCases;
43+
private ChatPrecedentDto precedent;
44+
45+
private ChatLawDto law;
4746

48-
@Schema(description = "응답 생성에 참고한 유사 법령 정보 목록")
49-
private List<Document> similarLaws;
5047
}
5148

5249
@Getter
@@ -73,6 +70,14 @@ public static ChatPrecedentDto from(ChatPrecedent cp) {
7370
.caseName(cp.getCaseName())
7471
.build();
7572
}
73+
74+
public static ChatPrecedentDto from(Document doc) {
75+
return ChatPrecedentDto.builder()
76+
.precedentContent(doc.getText())
77+
.caseNumber(doc.getMetadata().get("caseNumber").toString())
78+
.caseName(doc.getMetadata().get("caseName").toString())
79+
.build();
80+
}
7681
}
7782

7883
@Data
@@ -93,6 +98,13 @@ public static ChatLawDto from(ChatLaw cl) {
9398
.lawName(cl.getLawName())
9499
.build();
95100
}
101+
102+
public static ChatLawDto from(Document doc) {
103+
return ChatLawDto.builder()
104+
.content(doc.getText())
105+
.lawName(doc.getMetadata().get("lawName").toString())
106+
.build();
107+
}
96108
}
97109

98110
@Getter
@@ -109,31 +121,31 @@ public static class ChatHistoryDto {
109121
@Schema(description = "메시지 내용", example = "안녕하세요~~")
110122
private String message;
111123

112-
private List<ChatPrecedentDto> precedents;
124+
private ChatPrecedentDto precedent;
113125

114-
private List<ChatLawDto> laws;
126+
private ChatLawDto law;
115127

116128
@Schema(description = "생성 시간")
117129
private LocalDateTime createdAt;
118130

119131
public static ChatHistoryDto from(Chat chat) {
120132

121-
List<ChatPrecedentDto> precedentDtos = new ArrayList<>();
122-
for (ChatPrecedent cp : chat.getChatPrecedents()) {
123-
precedentDtos.add(ChatPrecedentDto.from(cp));
133+
ChatPrecedentDto precedentDto = null;
134+
if (chat.getChatPrecedents() != null && !chat.getChatPrecedents().isEmpty()) {
135+
precedentDto = ChatPrecedentDto.from(chat.getChatPrecedents().get(0));
124136
}
125137

126-
List<ChatLawDto> lawDtos = new ArrayList<>();
127-
for (ChatLaw cl : chat.getChatLaws()) {
128-
lawDtos.add(ChatLawDto.from(cl));
138+
ChatLawDto lawDto = null;
139+
if (chat.getChatLaws() != null && !chat.getChatLaws().isEmpty()) {
140+
lawDto = ChatLawDto.from(chat.getChatLaws().get(0));
129141
}
130142

131143
return ChatHistoryDto.builder()
132144
.type(chat.getType().toString())
133145
.message(chat.getMessage())
134146
.createdAt(chat.getCreatedAt())
135-
.precedents(precedentDtos)
136-
.laws(lawDtos)
147+
.precedent(precedentDto)
148+
.law(lawDto)
137149
.build();
138150
}
139151
}

backend/src/main/java/com/ai/lawyer/domain/chatbot/service/ChatBotService.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.ai.lawyer.domain.chatbot.service;
22

3+
import com.ai.lawyer.domain.chatbot.dto.ChatDto.ChatLawDto;
4+
import com.ai.lawyer.domain.chatbot.dto.ChatDto.ChatPrecedentDto;
35
import com.ai.lawyer.domain.chatbot.dto.ChatDto.ChatRequest;
46
import com.ai.lawyer.domain.chatbot.dto.ChatDto.ChatResponse;
57
import com.ai.lawyer.domain.chatbot.dto.ExtractionDto.KeywordExtractionDto;
@@ -93,12 +95,25 @@ public Flux<ChatResponse> sendMessage(Long memberId, ChatRequest chatChatRequest
9395
}
9496

9597
private ChatResponse ChatResponse(History history, String fullResponse, List<Document> cases, List<Document> laws) {
98+
99+
ChatPrecedentDto precedentDto = null;
100+
if (cases != null && !cases.isEmpty()) {
101+
Document firstCase = cases.get(0);
102+
precedentDto = ChatPrecedentDto.from(firstCase);
103+
}
104+
105+
ChatLawDto lawDto = null;
106+
if (laws != null && !laws.isEmpty()) {
107+
Document firstLaw = laws.get(0);
108+
lawDto = ChatLawDto.from(firstLaw);
109+
}
110+
96111
return ChatResponse.builder()
97112
.roomId(history.getHistoryId())
98113
.title(history.getTitle())
99114
.message(fullResponse)
100-
.similarCases(cases)
101-
.similarLaws(laws)
115+
.precedent(precedentDto)
116+
.law(lawDto)
102117
.build();
103118
}
104119

0 commit comments

Comments
 (0)