-
Notifications
You must be signed in to change notification settings - Fork 0
채팅방 제목 생성 AI 서버 연동 #156
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
Merged
Merged
채팅방 제목 생성 AI 서버 연동 #156
Changes from all commits
Commits
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
10 changes: 0 additions & 10 deletions
10
src/main/java/com/sofa/linkiving/domain/chat/ai/AiTitleClient.java
This file was deleted.
Oops, something went wrong.
8 changes: 4 additions & 4 deletions
8
...ing/domain/chat/ai/MockAiTitleClient.java → ...iving/domain/chat/ai/MockTitleClient.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 |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| package com.sofa.linkiving.domain.chat.ai; | ||
|
|
||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @Primary | ||
| public class MockAiTitleClient implements AiTitleClient { | ||
| @Profile("test") | ||
| public class MockTitleClient implements TitleClient { | ||
| @Override | ||
| public String generateSummary(String firstChat) { | ||
| public String generateTitle(String firstChat) { | ||
| return String.format("임시 제목[%s]", firstChat); | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/sofa/linkiving/domain/chat/ai/RagTitleClient.java
Goder-0 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,46 @@ | ||
| package com.sofa.linkiving.domain.chat.ai; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import com.sofa.linkiving.domain.chat.dto.request.TitleGenerateReq; | ||
| import com.sofa.linkiving.domain.chat.dto.response.TitleGenerateRes; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Component | ||
| @Slf4j | ||
| @Profile("!test") | ||
| @RequiredArgsConstructor | ||
| public class RagTitleClient implements TitleClient { | ||
|
|
||
| private static final int MAX_TITLE_LENGTH = 100; | ||
| private final RagTitleFeign ragTitleFeign; | ||
|
|
||
| @Override | ||
| public String generateTitle(String firstChat) { | ||
| try { | ||
| List<TitleGenerateRes> response = ragTitleFeign.generateTitle(new TitleGenerateReq(firstChat)); | ||
|
|
||
| if (response == null || response.isEmpty()) { | ||
| return truncateTitle(firstChat); | ||
| } | ||
|
|
||
| return response.get(0).title(); | ||
|
|
||
| } catch (Exception e) { | ||
| log.error("AI 서버 통신 실패. 기본 제목으로 대체합니다. error={}", e.getMessage()); | ||
| return truncateTitle(firstChat); | ||
| } | ||
| } | ||
|
|
||
| private String truncateTitle(String originalTitle) { | ||
| if (originalTitle.length() <= MAX_TITLE_LENGTH) { | ||
| return originalTitle; | ||
| } | ||
| return originalTitle.substring(0, MAX_TITLE_LENGTH); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/sofa/linkiving/domain/chat/ai/RagTitleFeign.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,17 @@ | ||
| package com.sofa.linkiving.domain.chat.ai; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.cloud.openfeign.FeignClient; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
|
|
||
| import com.sofa.linkiving.domain.chat.dto.request.TitleGenerateReq; | ||
| import com.sofa.linkiving.domain.chat.dto.response.TitleGenerateRes; | ||
| import com.sofa.linkiving.infra.feign.GlobalFeignConfig; | ||
|
|
||
| @FeignClient(name = "ai-title-client", url = "${ai.server.url}", configuration = GlobalFeignConfig.class) | ||
| public interface RagTitleFeign { | ||
| @PostMapping("/webhook/title-generate") | ||
| List<TitleGenerateRes> generateTitle(@RequestBody TitleGenerateReq request); | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sofa/linkiving/domain/chat/ai/TitleClient.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,10 @@ | ||
| package com.sofa.linkiving.domain.chat.ai; | ||
|
|
||
| public interface TitleClient { | ||
| /** | ||
| * AI 서버에 첫 채팅 내용을 토대로 채팅방 제목 생성 요청을 보냅니다. | ||
| * @param firstChat 채팅 시작 대화 | ||
| * @return 제목 | ||
| */ | ||
| String generateTitle(String firstChat); | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/sofa/linkiving/domain/chat/dto/request/TitleGenerateReq.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,6 @@ | ||
| package com.sofa.linkiving.domain.chat.dto.request; | ||
|
|
||
| public record TitleGenerateReq( | ||
| String firstMessage | ||
| ) { | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/sofa/linkiving/domain/chat/dto/response/TitleGenerateRes.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,6 @@ | ||
| package com.sofa.linkiving.domain.chat.dto.response; | ||
|
|
||
| public record TitleGenerateRes( | ||
| String title | ||
| ) { | ||
| } |
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
96 changes: 96 additions & 0 deletions
96
src/test/java/com/sofa/linkiving/domain/chat/ai/RagTitleClientTest.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,96 @@ | ||
| package com.sofa.linkiving.domain.chat.ai; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
| import static org.mockito.BDDMockito.*; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import com.sofa.linkiving.domain.chat.dto.request.TitleGenerateReq; | ||
| import com.sofa.linkiving.domain.chat.dto.response.TitleGenerateRes; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class RagTitleClientTest { | ||
|
|
||
| @InjectMocks | ||
| private RagTitleClient ragTitleClient; | ||
|
|
||
| @Mock | ||
| private RagTitleFeign ragTitleFeign; | ||
|
|
||
| @Test | ||
| @DisplayName("AI 서버 통신 성공 시 생성된 제목을 반환한다") | ||
| void shouldReturnGeneratedTitleWhenApiCallSucceeds() { | ||
| // given | ||
| String firstChat = "안녕하세요"; | ||
| String generatedTitle = "인사말"; | ||
|
|
||
| TitleGenerateRes resDto = mock(TitleGenerateRes.class); | ||
| given(resDto.title()).willReturn(generatedTitle); | ||
|
|
||
| given(ragTitleFeign.generateTitle(any(TitleGenerateReq.class))) | ||
| .willReturn(List.of(resDto)); | ||
|
|
||
| // when | ||
| String result = ragTitleClient.generateTitle(firstChat); | ||
|
|
||
| // then | ||
| assertThat(result).isEqualTo(generatedTitle); | ||
| verify(ragTitleFeign).generateTitle(any(TitleGenerateReq.class)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("AI 서버 응답이 빈 리스트일 경우 요청한 첫 메시지(firstChat)를 그대로 반환한다") | ||
| void shouldReturnFirstChatWhenResponseIsEmpty() { | ||
| // given | ||
| String firstChat = "안녕하세요"; | ||
|
|
||
| given(ragTitleFeign.generateTitle(any(TitleGenerateReq.class))) | ||
| .willReturn(Collections.emptyList()); | ||
|
|
||
| // when | ||
| String result = ragTitleClient.generateTitle(firstChat); | ||
|
|
||
| // then | ||
| assertThat(result).isEqualTo(firstChat); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("AI 서버 응답이 null일 경우 요청한 첫 메시지(firstChat)를 그대로 반환한다") | ||
| void shouldReturnFirstChatWhenResponseIsNull() { | ||
| // given | ||
| String firstChat = "안녕하세요"; | ||
|
|
||
| given(ragTitleFeign.generateTitle(any(TitleGenerateReq.class))) | ||
| .willReturn(null); | ||
|
|
||
| // when | ||
| String result = ragTitleClient.generateTitle(firstChat); | ||
|
|
||
| // then | ||
| assertThat(result).isEqualTo(firstChat); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("AI 서버 통신 중 예외 발생 시 로그를 남기고 첫 메시지(firstChat)를 그대로 반환한다") | ||
| void shouldReturnFirstChatWhenExceptionOccurs() { | ||
| // given | ||
| String firstChat = "안녕하세요"; | ||
|
|
||
| given(ragTitleFeign.generateTitle(any(TitleGenerateReq.class))) | ||
| .willThrow(new RuntimeException("API Connection Failed")); | ||
|
|
||
| // when | ||
| String result = ragTitleClient.generateTitle(firstChat); | ||
|
|
||
| // then | ||
| assertThat(result).isEqualTo(firstChat); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.