Skip to content

Commit

Permalink
Refactor: transformQuestion 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
RinRinPARK committed Aug 29, 2024
1 parent 7cbc06c commit ac2a80a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies {
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")
implementation("org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.9.0-RC")

// querydsl
implementation("com.querydsl:querydsl-jpa:5.0.0:jakarta")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ class QuestionController(
@Operation(summary = "transformQuestion", description = "문제 변환")
@SecurityRequirement(name = "bearer Auth")
@PostMapping("question-transform")
fun transformQuestion(
suspend fun transformQuestion(
@RequestPart image: MultipartFile?,
@RequestPart imageCoordinates: List<List<Int>>? = null,
): BaseResponse<TransformQuestionResponse> {

val imageUrl = image?.let { s3Service.uploadChatGptImage(it) }
val response: TransformQuestionResponse = questionService.transformQuestion(imageUrl!!, imageCoordinates)

Expand Down
48 changes: 24 additions & 24 deletions src/main/kotlin/com/swm_standard/phote/service/QuestionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.swm_standard.phote.entity.Tag
import com.swm_standard.phote.repository.MemberRepository
import com.swm_standard.phote.repository.QuestionRepository
import com.swm_standard.phote.repository.TagRepository
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
Expand Down Expand Up @@ -122,40 +121,28 @@ class QuestionService(
return DeleteQuestionResponse(id, LocalDateTime.now())
}

fun transformQuestion(
suspend fun transformQuestion(
imageUrl: String,
imageCoordinates: List<List<Int>>?,
): TransformQuestionResponse {

val (transformedImageUrl, chatGPTResponse) = runBlocking {
val transformedImageUrl = CoroutineScope(Dispatchers.IO).async {

// 문제 그림 추출
val transformedImageUrlDeferred = CoroutineScope(Dispatchers.IO).async {
imageCoordinates?.let { transformImage(imageUrl, it) }
}.await()
imageCoordinates?.let { transformImage(imageUrl, it) }
}.await()

val chatGPTResponseSplit = CoroutineScope(Dispatchers.IO).async {
// openAI로 메시지 전송
val chatGPTResponseDeferred = CoroutineScope(Dispatchers.IO).async {
val request = ChatGPTRequest(model, imageUrl)
template.postForObject(url, request, ChatGPTResponse::class.java)
}.await()
val request = ChatGPTRequest(model, imageUrl)
val chatGPTResponse = template.postForObject(url, request, ChatGPTResponse::class.java)

Pair(transformedImageUrlDeferred, chatGPTResponseDeferred)
}

// openAI로부터 메시지 수신
val split: List<String> =
chatGPTResponse!!
.choices[0]
.message.content
.split("#")

if (split[0] == "") {
throw ChatGptErrorException(fieldName = "chatGPT")
}
// openAI로부터 메시지 수신
splitChatGPTResponse(chatGPTResponse)
}.await()

// 문제 문항과 객관식을 분리해서 dto에 저장
return TransformQuestionResponse(split[0], split.drop(1), transformedImageUrl)
return TransformQuestionResponse(chatGPTResponseSplit[0], chatGPTResponseSplit.drop(1), transformedImageUrl)
}

suspend fun transformImage(imageUrl: String, imageCoordinates: List<List<Int>>?): String? {
Expand All @@ -178,4 +165,17 @@ class QuestionService(
}
return lambdaResponse.body?.split("\"")?.get(1)
}

fun splitChatGPTResponse(chatGPTResponse: ChatGPTResponse?): List<String> {
val split: List<String> =
chatGPTResponse!!
.choices[0]
.message.content
.split("#")

if (split[0] == "") {
throw ChatGptErrorException(fieldName = "chatGPT")
}
return split
}
}

0 comments on commit ac2a80a

Please sign in to comment.