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 @@ -23,14 +23,11 @@ public class ChatBotController {
@Operation(summary = "01. 새로운 채팅", description = "첫 메시지 전송으로 새로운 채팅방을 생성하고 챗봇과 대화를 시작")
@PostMapping(value = "/message")
public Flux<ChatResponse> postNewMessage(@RequestBody ChatRequest chatRequest) {
// SecurityContext에서 memberId를 미리 추출 (컨트롤러 진입 시점)

Long memberId = AuthUtil.getAuthenticatedMemberId();
if (memberId == null) {
throw new IllegalStateException("인증된 사용자가 아닙니다.");
}

log.info("새로운 채팅 요청: memberId={}", memberId);

// memberId를 Flux에 전달 (SecurityContext 전파 문제 방지)
return chatBotService.sendMessage(memberId, chatRequest, null);
}

Expand All @@ -39,11 +36,11 @@ public Flux<ChatResponse> postNewMessage(@RequestBody ChatRequest chatRequest) {
public Flux<ChatResponse> postMessage(
@RequestBody ChatRequest chatRequest,
@PathVariable(value = "roomId", required = false) Long roomId) {
// SecurityContext에서 memberId를 미리 추출 (컨트롤러 진입 시점)

Long memberId = AuthUtil.getAuthenticatedMemberId();

log.info("기존 채팅 요청: memberId={}, roomId={}", memberId, roomId);

// memberId를 Flux에 전달 (SecurityContext 전파 문제 방지)
return chatBotService.sendMessage(memberId, chatRequest, roomId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,22 @@ public class ChatBotService {
@Transactional
public Flux<ChatResponse> sendMessage(Long memberId, ChatRequest chatRequestDto, Long roomId) {

// 벡터 검색 (판례, 법령) (블로킹)
// 벡터 검색 (판례, 법령)
List<Document> similarCaseDocuments = qdrantService.searchDocument(chatRequestDto.getMessage(), "type", "판례");
List<Document> similarLawDocuments = qdrantService.searchDocument(chatRequestDto.getMessage(), "type", "법령");

String caseContext = formatting(similarCaseDocuments);
String lawContext = formatting(similarLawDocuments);

// 채팅방 조회 또는 생성 (블로킹)
// 채팅방 조회 또는 생성
History history = getOrCreateRoom(memberId, roomId);

// 메시지 기억 관리 (User 메시지 추가)
// 메시지 기억 관리
ChatMemory chatMemory = saveChatMemory(chatRequestDto, history);

// 프롬프트 생성
Prompt prompt = getPrompt(caseContext, lawContext, chatMemory, history);

// 준비된 데이터를 담은 컨텍스트 객체 반환
//return new PreparedChatContext(prompt, history, similarCaseDocuments, similarLawDocuments);

return chatClient.prompt(prompt)
.stream()
.content()
Expand Down Expand Up @@ -176,23 +173,4 @@ private ChatResponse handleError(History history) {
.build();
}

/**
* 블로킹 작업에서 준비된 데이터를 담는 컨텍스트 클래스
* 리액티브 체인에서 데이터를 전달하기 위한 내부 클래스
*/
private static class PreparedChatContext {
final Prompt prompt;
final History history;
final List<Document> similarCaseDocuments;
final List<Document> similarLawDocuments;

PreparedChatContext(Prompt prompt, History history,
List<Document> similarCaseDocuments,
List<Document> similarLawDocuments) {
this.prompt = prompt;
this.history = history;
this.similarCaseDocuments = similarCaseDocuments;
this.similarLawDocuments = similarLawDocuments;
}
}
}