-
Notifications
You must be signed in to change notification settings - Fork 9
[고객지원 챗봇 만들기] 김수민 제출합니다. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
boyekim
wants to merge
7
commits into
cho-log:main
Choose a base branch
from
boyekim:boyekim
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee29856
feat: 1차 구현
boyekim a5c216b
feat: RAG 검색 구조 추가와 평가 스크립트 개선
boyekim c6f3000
refactor: ObjectMapper로 chatlog 파싱 처리 및 RAG 검색 topK 설정값 조정
boyekim 69e58b0
refactor: RAG 문서 로딩 책임 분리
boyekim 572c726
docs: wall report 작성
boyekim ad01fbf
feat: wall report에 작성한 평가 기록 추가
boyekim 8ae5af6
refactor: dto 네이밍 수정
boyekim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "total": 150, | ||
| "correct": 66, | ||
| "incorrect": 84, | ||
| "error": 0, | ||
| "accuracy": 0.44, | ||
| "tier_results": { | ||
| "easy": { | ||
| "correct": 13, | ||
| "total": 30 | ||
| }, | ||
| "medium": { | ||
| "correct": 47, | ||
| "total": 94 | ||
| }, | ||
| "hard": { | ||
| "correct": 6, | ||
| "total": 26 | ||
| } | ||
| }, | ||
| "elapsed_seconds": 468.1384799480438 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.cholog.bootcamp; | ||
|
|
||
| import org.springframework.ai.chat.client.ChatClient; | ||
| import org.springframework.ai.embedding.EmbeddingModel; | ||
| import org.springframework.ai.vectorstore.SimpleVectorStore; | ||
| import org.springframework.ai.vectorstore.VectorStore; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class AiConfig { | ||
|
|
||
| @Bean | ||
| VectorStore vectorStore(EmbeddingModel embeddingModel) { | ||
| return SimpleVectorStore.builder(embeddingModel).build(); | ||
| } | ||
|
|
||
| @Bean | ||
| ChatClient chatClient(ChatClient.Builder chatClientBuilder) { | ||
| return chatClientBuilder.build(); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/cholog/bootcamp/chat/ChatController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.cholog.bootcamp.chat; | ||
|
|
||
| import com.cholog.bootcamp.chat.dto.ChatRequest; | ||
| import com.cholog.bootcamp.chat.dto.ChatAnswerResponse; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| public class ChatController { | ||
|
|
||
| private final ChatService chatService; | ||
|
|
||
| public ChatController(ChatService chatService) { | ||
| this.chatService = chatService; | ||
| } | ||
|
|
||
| @PostMapping("/api/chat") | ||
| public ChatAnswerResponse chat(@RequestBody ChatRequest request) { | ||
| return chatService.ask(request.question()); | ||
| } | ||
| } |
103 changes: 103 additions & 0 deletions
103
src/main/java/com/cholog/bootcamp/chat/ChatService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package com.cholog.bootcamp.chat; | ||
|
|
||
| import com.cholog.bootcamp.chat.dto.ChatAnswerResponse; | ||
| import jakarta.annotation.PostConstruct; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.ai.chat.client.ChatClient; | ||
| import org.springframework.ai.chat.metadata.Usage; | ||
| import org.springframework.ai.chat.model.ChatResponse; | ||
| import org.springframework.ai.document.Document; | ||
| import org.springframework.ai.vectorstore.SearchRequest; | ||
| import org.springframework.ai.vectorstore.VectorStore; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class ChatService { | ||
|
|
||
| private final ChatClient chatClient; | ||
| private final VectorStore vectorStore; | ||
| private final RagProperties ragProperties; | ||
| private final DocumentLoader documentLoader; | ||
|
|
||
| @PostConstruct | ||
| void loadFaqContext() { | ||
| vectorStore.add(documentLoader.load()); | ||
| } | ||
|
|
||
| public ChatAnswerResponse ask(String question) { | ||
| List<Document> retrievedDocuments = vectorStore.similaritySearch( | ||
| SearchRequest.builder() | ||
| .query(question) | ||
| .topK(ragProperties.getTopK()) | ||
| .build() | ||
| ); | ||
|
|
||
| logSearchResults(question, retrievedDocuments); | ||
|
|
||
| String supportContext = retrievedDocuments | ||
| .stream() | ||
| .map(Document::getText) | ||
| .collect(Collectors.joining("\n\n===\n\n")); | ||
|
|
||
| ChatResponse response = chatClient.prompt() | ||
| .system(""" | ||
| - 당신은 Cholog Corporation의 고객 전용 챗봇 서비스이다. | ||
| - 제공된 컨텍스트만을 활용하라. | ||
| - 제공된 컨텍스트로 답할 수 없다면, '고객센터에 문의해주세요'라고 답하라. | ||
| - 한국어로 답하라. | ||
| """) | ||
| .user(""" | ||
| Customer question: | ||
| %s | ||
|
|
||
| Support context: | ||
| %s | ||
| """.formatted(question, supportContext)) | ||
| .call() | ||
| .chatResponse(); | ||
|
|
||
| Usage usage = response.getMetadata().getUsage(); | ||
|
|
||
| return new ChatAnswerResponse( | ||
| response.getResult().getOutput().getText(), | ||
| new ChatAnswerResponse.TokenUsage( | ||
| usage == null || usage.getPromptTokens() == null ? 0 : usage.getPromptTokens(), | ||
| usage == null || usage.getCompletionTokens() == null ? 0 : usage.getCompletionTokens(), | ||
| usage == null || usage.getTotalTokens() == null ? 0 : usage.getTotalTokens() | ||
| ) | ||
| ); | ||
|
Comment on lines
+67
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLM 응답 결과가 비어있을 경우 String answer = (response.getResult() != null && response.getResult().getOutput() != null)
? response.getResult().getOutput().getText()
: "고객센터에 문의해주세요.";
return new ChatAnswerResponse(
answer,
new ChatAnswerResponse.TokenUsage(
usage == null || usage.getPromptTokens() == null ? 0 : usage.getPromptTokens(),
usage == null || usage.getCompletionTokens() == null ? 0 : usage.getCompletionTokens(),
usage == null || usage.getTotalTokens() == null ? 0 : usage.getTotalTokens()
)
); |
||
| } | ||
|
|
||
| private void logSearchResults(String question, List<Document> documents) { | ||
| String resultSummary = documents.isEmpty() | ||
| ? "no documents retrieved" | ||
| : documents.stream() | ||
| .map(this::formatDocumentSummary) | ||
| .collect(Collectors.joining(" | ")); | ||
|
|
||
| log.info("RAG search question='{}' topK={} results={}", | ||
| question, | ||
| ragProperties.getTopK(), | ||
| resultSummary | ||
| ); | ||
| } | ||
|
|
||
| private String formatDocumentSummary(Document document) { | ||
| Map<String, Object> metadata = document.getMetadata(); | ||
| String sourceType = String.valueOf(metadata.getOrDefault("sourceType", "UNKNOWN")); | ||
| String source = String.valueOf(metadata.getOrDefault("source", "UNKNOWN")); | ||
| Object sectionTitle = metadata.get("sectionTitle"); | ||
|
|
||
| if (sectionTitle == null) { | ||
| return "%s/%s".formatted(sourceType, source); | ||
| } | ||
|
|
||
| return "%s/%s#%s".formatted(sourceType, source, sectionTitle); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재 구조는 애플리케이션이 시작될 때마다 모든 문서를 다시 읽고 임베딩하여
VectorStore에 추가합니다. 이는 시작 시간을 지연시키고 불필요한 API 비용을 발생시킵니다.SimpleVectorStore의save/load기능을 사용하여 벡터 데이터를 파일로 캐싱하거나, 영구 저장소 기반의VectorStore도입을 고려해 보세요.