Skip to content

Commit

Permalink
Merge pull request #203 from ku-ring/develop
Browse files Browse the repository at this point in the history
version 2.7.2
  • Loading branch information
zbqmgldjfh authored Aug 1, 2024
2 parents bc6909c + 6f85152 commit 935c6bd
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 43 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ dependencies {
testImplementation 'org.testcontainers:junit-jupiter:1.19.8'
testImplementation 'org.testcontainers:mariadb:1.19.8'
testImplementation 'org.testcontainers:chromadb:1.19.8'
testImplementation 'io.projectreactor:reactor-test'
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.kustacks.kuring.ai.adapter.out.model;

import com.kustacks.kuring.ai.application.port.out.QueryAiModelPort;
import com.kustacks.kuring.common.exception.InvalidStateException;
import com.kustacks.kuring.common.exception.code.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.prompt.Prompt;
Expand All @@ -28,10 +26,6 @@ public Flux<String> call(Prompt prompt) {
);
}

if (prompt.getContents().contains("잘못된 질문")) {
throw new InvalidStateException(ErrorCode.AI_SIMILAR_DOCUMENTS_NOT_FOUND);
}

return Flux.just("미", "리", " ", "준", "비", "된", " ",
"테", "스", "트", "질", "문", "이", " ", "아", "닙", "니", "다");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -22,6 +23,8 @@ public class InMemoryVectorStoreAdapter implements QueryVectorStorePort, Command

@Override
public List<String> findSimilarityContents(String question) {
if(question.equals("잘못된 질문")) return Collections.emptyList();

HashMap<String, Object> metadata = createMetaData();

Document document = createDocument(metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public class RAGQueryService implements RAGQueryUseCase {

@Override
public Flux<String> askAiModel(String question, String id) {
ragEventPort.userDecreaseQuestionCountEvent(id);
Prompt completePrompt = buildCompletePrompt(question);
return ragChatModel.call(completePrompt);
try {
ragEventPort.userDecreaseQuestionCountEvent(id);
Prompt completePrompt = buildCompletePrompt(question);
return ragChatModel.call(completePrompt);
} catch (InvalidStateException e) {
final String separator = "";
return Flux.fromArray(e.getErrorCode().getMessage().split(separator));
}
}

@PostConstruct
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ spring:
api-key: ${AI_API_KEY}
chat:
options:
model: gpt-3.5-turbo
model: ${AI_CHAT_MODEL}
temperature: 0.0
maxTokens: 1000
embedding:
options:
model: text-embedding-3-small
model: ${AI_EMBEDDING_MODEL}
vectorstore:
chroma:
client:
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ spring:
api-key: ${AI_API_KEY}
chat:
options:
model: gpt-3.5-turbo
model: ${AI_CHAT_MODEL}
temperature: 0.0
maxTokens: 1000
embedding:
options:
model: text-embedding-3-small
model: ${AI_EMBEDDING_MODEL}
vectorstore:
chroma:
client:
Expand Down
37 changes: 25 additions & 12 deletions src/test/java/com/kustacks/kuring/acceptance/AiAcceptanceTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package com.kustacks.kuring.acceptance;

import com.kustacks.kuring.support.IntegrationTestSupport;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.WebTestClient;

import static com.kustacks.kuring.acceptance.AiStep.*;
import static org.assertj.core.api.Assertions.assertThat;
import static com.kustacks.kuring.acceptance.AiStep.모델_응답_확인;
import static com.kustacks.kuring.acceptance.AiStep.사용자_질문_요청;

@DisplayName("인수 : 인공지능")
class AiAcceptanceTest extends IntegrationTestSupport {

WebTestClient client;

@Override
@BeforeEach
public void setUp() {
super.setUp();
client = WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build();
}

/**
* Given : 쿠링앱이 실행중이다
* When : 교내,외 장학금 및 학자금 대출 관련 전화번호들을 묻는다
Expand All @@ -21,14 +30,18 @@ class AiAcceptanceTest extends IntegrationTestSupport {
@Test
void ask_to_open_ai() {
// given
WebTestClient client = WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build();
String question = "교내,외 장학금 및 학자금 대출 관련 전화번호들을 안내를 해줘";

// when
var 모델_응답 = 사용자_질문_요청(client, question, USER_FCM_TOKEN);

// then
모델_응답_검증(모델_응답, HttpStatus.OK.value());
모델_응답_확인(모델_응답, "학", "생", "복", "지", "처", "장", "학", "복", "지", "팀", "의",
"전", "화", "번", "호", "는", "0", "2", "-", "4", "5", "0", "-", "3", "2", "1",
"1", "~", "2", "이", "며", ",", "건", "국", "사", "랑", "/", "장", "학", "사", "정",
"관", "장", "학", "/", "기", "금", "장", "학", "과", "관", "련", "된", "문", "의",
"는", "0", "2", "-", "4", "5", "0", "-", "3", "9", "6", "7", "로", "하", "시",
"면", "됩", "니", "다", ".");
}

/**
Expand All @@ -41,15 +54,14 @@ void ask_to_open_ai() {
void ask_to_open_ai_overflow_count() {
// given
String question = "교내,외 장학금 및 학자금 대출 관련 전화번호들을 안내를 해줘";
사용자_질문_요청_REST(question, USER_FCM_TOKEN);
사용자_질문_요청_REST(question, USER_FCM_TOKEN);
사용자_질문_요청_REST(question, USER_FCM_TOKEN);
사용자_질문_요청(client, question, USER_FCM_TOKEN);
사용자_질문_요청(client, question, USER_FCM_TOKEN);

// when
var 모델_응답 = 사용자_질문_요청_REST(question, USER_FCM_TOKEN);
var 모델_응답 = 사용자_질문_요청(client, question, USER_FCM_TOKEN);

// then
assertThat(모델_응답.statusCode()).isEqualTo(HttpStatus.TOO_MANY_REQUESTS.value());
모델_응답_확인(모델_응답, "남", "은", "질", "문", "횟", "수", "가", "부", "족", "합", "니", "다", ".");
}

/**
Expand All @@ -64,10 +76,11 @@ void ask_to_open_ai_no_info() {
String question = "잘못된 질문";

// when
var 모델_응답 = 사용자_질문_요청_REST(question, USER_FCM_TOKEN);
var 모델_응답 = 사용자_질문_요청(client, question, USER_FCM_TOKEN);

// then
assertThat(모델_응답.statusCode()).isEqualTo(HttpStatus.NOT_FOUND.value());
모델_응답_확인(모델_응답, "죄", "송", "합", "니", "다", ",", "관", "련", "된", "내",
"용", "에", "대", "하", "여", "알", "지", "못", "합", "니", "다", ".");
}
}

26 changes: 8 additions & 18 deletions src/test/java/com/kustacks/kuring/acceptance/AiStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.FluxExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.test.StepVerifier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.springframework.http.HttpStatus.OK;

public class AiStep {

Expand All @@ -22,25 +24,13 @@ public class AiStep {
.returnResult(String.class);
}

// REST 버전이 따로 있는 이유는 예외가 발생하는 경우에는 Json 응답이 오기 때문이다
public static ExtractableResponse<Response> 사용자_질문_요청_REST(String question, String userToken) {
return RestAssured
.given().log().all()
.header("User-Token", userToken)
.when().get("/api/v2/ai/messages?question={question}", question)
.then().log().all()
.extract();
}

public static void 모델_응답_검증(FluxExchangeResult<String> response, int statusCode) {
String bodyAsString = response.getResponseBody()
.reduce(new StringBuilder(), StringBuilder::append)
.map(StringBuilder::toString)
.block();

public static void 모델_응답_확인(FluxExchangeResult<String> 모델_응답, String... expected) {
assertAll(
() -> assertThat(response.getStatus().value()).isEqualTo(statusCode),
() -> assertThat(bodyAsString).contains("02-450-3211~2이며", "02-450-3967")
() -> assertThat(모델_응답.getStatus()).isEqualTo(OK),
() -> StepVerifier.create(모델_응답.getResponseBody())
.expectNext(expected)
.expectComplete()
.verify()
);
}
}

0 comments on commit 935c6bd

Please sign in to comment.